https://www.hackerrank.com/challenges/cut-the-sticks/problem?isFullScreen=true
Cut the sticks | HackerRank
Given the lengths of n sticks, print the number of sticks that are left before each cut operation.
www.hackerrank.com
나의 코드
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import deque
def cutTheSticks(arr):
answer = []
arr= deque(sorted(arr))
while arr:
answer.append(len(arr))
now_min_sk = arr.popleft()
now_queue = deque()
for i in arr:
if i-now_min_sk > 0:
now_queue.append(i-now_min_sk)
arr = now_queue
return answer
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
result = cutTheSticks(arr)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
시간 복잡도
sorted(arr) : 파이썬 정렬 알고리즘 ( 선형 로그 시간 복잡도 )
while arr : 조건 만족시까지 순회 ( 선형 시간 복잡도 )
for i in arr: arr을 배열을 순회하며 나무 길이 줄여주기 ( 선형 시간 복잡 )
해당 알고리즘 시간복잡도는 이차형 시간 복잡도 ( O(n**2) )'코딩테스트 파이썬 > hackerrank' 카테고리의 다른 글
| Cavity Map (0) | 2024.10.08 |
|---|---|
| Fair Rations (0) | 2024.10.07 |
| Sherlock and Squares (0) | 2024.09.23 |
| Append and Delete (0) | 2024.09.23 |
| Climbing the Leaderboard (0) | 2024.09.09 |