An operation "swap" means removing an element from the array and appending it at the back of the same array. Find the minimum number of "swaps" needed to sort that array. Eg :- 3124 Output: 2 (3124->1243->1234) 看到版上有人讨论这道题, 总觉得应该有更优解。 而且假设 数字是从 1-n 有些牵强 。昨天想到了一个解法,今天又稍稍更新了一下。个人的理解 大家看看有没有错。谢 谢! 1. 如果数组长度是n,最多就需要swap n-1次,就是每个元素 最多只需要swap一次。 我们并不需要关心哪个先哪个后swap。 那么 我们就一个一个元素的判断那些需要swap。 2. 首先 如果 给定元素的右边 有更小的值,这个元素就需要swap, 比如上例中的3 3. 然后,再看 剩下的没有swap的元素,如果值比 “在第二步中需要swap的所有元素 的最小值” 大, 那这个元素 就需要 swap, 比如上例,124 中的4 比 3大, 4 就 需要swap 这个解法 O(n)time 常数space, 附上 Pseudo Code Int minStep(Array A) swapMin = INT_MAX rightMin = INT_MAX for (int i = len(A)-1; i<=0; i--) if A[i] > rightMin && swapMin < A[i] swapMin = A[i] else rightMin = A[i] stepCnt = 0 for (int i = 0; i < len(A); i++ ) if A[i] > swapMin stepCnt++ return stepCnt+1
t*n
3 楼
link 呢
b*e
4 楼
incorrect. see example 3214 first round (1) 32 needs swap get 1432 second round (2) 4 need to swap get 1324 not sorted. I guess needs to use some advanced data structure like stack.... keep tracking the local min.... then pop us the elements larger than the local min and insert them to the end one by one. (1) push 32 into stack. (2) when hit 1, see 1 is local min (3) pop up 2,3 and insert them to the back of the array (4) now array is 1423. (5) the array pointer now go to 1 (note we can track this index with the pop of the stack). push 1 and 4 into stack. (6) now find 2 (index 2 in the arr) is a local min (7) pop 4 out stack (1 not pop out since it is smaller than 2). (8) insert 4 to the end of the arr. (9) now the arr is 1234. scanning from index 1 to index 3. no local min any more. the array is sorted. For this example, sorted in two steps. not sure sbout the complexity. space compexity is o(n) to maintain the stack . When the origniall array is ascent or descent, the time complexity are both o(n). not sure about general complexity.
l*y
5 楼
这个网站做的极为粗糙,感觉是假的?
b*e
6 楼
works for 3124 too (1) push 3 into stack (2) find 1 is a local min (3) pop out 3 from the stack and insert it to the back of the array (4) now array becomes 1243 and the pointer to the array go to 0. (5) now push 124 into stack. find 3 is a local min (6) pop out 4 (don't pop out 12 since they are smalelr than 3). (7) insert 4 to the end of the array. (8) sorted ast 1234. The number of swap can be decidied during process above. anyone can help to analyze the time complexity?
R*g
7 楼
这是什么阿
b*e
8 楼
don't work either. seems also need to track local high.
【在 b*******e 的大作中提到】 : works for 3124 too : (1) push 3 into stack : (2) find 1 is a local min : (3) pop out 3 from the stack and insert it to the back of the array : (4) now array becomes 1243 and the pointer to the array go to 0. : (5) now push 124 into stack. find 3 is a local min : (6) pop out 4 (don't pop out 12 since they are smalelr than 3). : (7) insert 4 to the end of the array. : (8) sorted ast 1234. : The number of swap can be decidied during process above. anyone can help to
c*d
11 楼
这个就是fedex reward program的网站
b*e
12 楼
don't work either. seems also need to track local high.
b*e
13 楼
it is just to show how to find the swapping steps (only swap to the end can be used to switch the locations)....
真不知道 您在说什么 first round (1) 32 needs swap then step = 2 second round (2) 4 need to swap then step += 1 so step = 3 why we care about how to sort?
【在 b*******e 的大作中提到】 : incorrect. : see example 3214 : first round (1) 32 needs swap : get 1432 : second round (2) 4 need to swap : get 1324 : not sorted. : I guess needs to use some advanced data structure like stack.... : keep tracking the local min.... then pop us the elements larger than the : local min and insert them to the end one by one.
【在 g********r 的大作中提到】 : An operation "swap" means removing an element from the array and appending : it at the back of the same array. Find the minimum number of "swaps" needed : to sort that array. : Eg :- 3124 : Output: 2 (3124->1243->1234) : 看到版上有人讨论这道题, 总觉得应该有更优解。 而且假设 数字是从 1-n 有些牵强 : 。昨天想到了一个解法,今天又稍稍更新了一下。个人的理解 大家看看有没有错。谢 : 谢! : 1. 如果数组长度是n,最多就需要swap n-1次,就是每个元素 最多只需要swap一次。 : 我们并不需要关心哪个先哪个后swap。 那么 我们就一个一个元素的判断那些需要swap。