class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
s = nums.copy()
s.sort()
start = -1
end = -1
for i in range(len(s)):
if s[i] != nums[i] and start < 0:
start = i
end = i + 1
elif (s[i] != nums[i]):
end = i + 1
return end - start
Runtime: 204 ms, faster than 66.44% of Python3 online submissions for Shortest Unsorted Continuous Subarray.
Memory Usage: 15.3 MB, less than 64.31% of Python3 online submissions for Shortest Unsorted Continuous Subarray.
sort 한 다음에 처음 틀린 부분과 마지막 틀린 부분을 잡아줬읍니다
class Solution:
def pancakeSort(self, arr: List[int]) -> List[int]:
sort = arr.copy()
sort.sort()
sort_r = sort.copy()
sort_r.reverse()
mani = arr.copy()
result = []
i = len(mani) - 1
while i >= 0:
loc = arr.index(i)
result.append(loc)
mani = mani[:loc:-1] + mani[:loc]
i = i - 1
return result
대충 눈치로 4 3 2 1 있는 순서대로 위치 찾아서 그 만큼 sort 하면 될거 같은데...^^ 안된다는점~
def pancakeSort(self, A):
res = []
for x in range(len(A), 1, -1):
i = A.index(x)
res.extend([i + 1, x])
A = A[:i:-1] + A[:i]
return res
Runtime: 40 ms, faster than 83.07% of Python3 online submissions for Pancake Sorting.
Memory Usage: 14.3 MB, less than 57.94% of Python3 online submissions for Pancake Sorting.
이 원리를 인터뷰에서 어케 기억해서 풀라는거죠...^^