[백준] 2217번: 로프

whitehousechef·2023년 9월 12일

https://www.acmicpc.net/problem/2217

initial

Well it is simple. If we want to pull as much weight, we want to iterate through every rope and since it is guaranteed that the longer (bigger value ropes) can withstand as much as the short rope, we can do like this

correct:

n=int(input())
lst=[]
ans=0
for _ in range(n):
    lst.append(int(input()))
lst.sort()
for i in range(len(lst)):
    ans = max(ans, (n-i)*lst[i])
print(ans)

revisited jan 23rd

yep well explained

complexity

Time Complexity:

  1. Input Reading: Reading the input values and populating the list lst takes O(n) time, where "n" is the number of elements in the list.

  2. Sorting: Sorting the list lst using lst.sort() takes O(n*log(n)) time in the worst case, as it involves comparing and rearranging the elements.

  3. Loop Iteration: The for loop iterates through the sorted list, and for each element, it calculates a value and updates the ans variable. This loop runs for all elements in the list, so it takes O(n) time.

The dominant factor in the time complexity is the sorting step, which is O(nlog(n)). Therefore, the overall time complexity of your code is O(nlog(n)).

Space Complexity:

  1. Input Storage: Storing the input values in the list lst takes O(n) space.

  2. Other Variables: The code uses a few integer variables (ans, i), which take constant space and don't depend on the input size.

The total space complexity is dominated by the storage of the list lst, resulting in O(n) space complexity.

In summary, your code has a time complexity of O(n*log(n)) due to sorting and a space complexity of O(n) due to the storage of the input list.

0개의 댓글