比方说:0,2,0,4,3,1,5,0 =〉0,2,3,5 我的解法是sort this sequence X to Y, Get the LCS of X and Y. Time = O(nlogn + n^2) = O(N^2) 对方说还能更快。谁知道该怎么改进? Updated: 我忘了说了,是求*最长*单调上升子列
【在 r********7 的大作中提到】 : 比方说:0,2,0,4,3,1,5,0 =〉0,2,3,5 : 我的解法是sort this sequence X to Y, Get the LCS of X and Y. : Time = O(nlogn + n^2) = O(N^2) : 对方说还能更快。谁知道该怎么改进? : Updated: 我忘了说了,是求*最长*单调上升子列
j*h
21 楼
1. number all the element in the original array, in growing sequence (O(n)). e.g. original array is [3,2,1,0,1,2], after numbering, it became [(3,0), (2,1),(1,2),(0,3),(1,4),(2,5)]. the "number" of each element is the corresponding position it is in the original array. 2. after numbering, sort the new array according to their original value , using quicksort. (O(nlogn)) e.g. after sorting, [3,2,1,0,1,2]->[0,1,1,2,2,3]. ( I didn't list the " position" of each element here). 3. find the lo
【在 j*********h 的大作中提到】 : 1. number all the element in the original array, in growing sequence (O(n)). : e.g. original array is [3,2,1,0,1,2], after numbering, it became [(3,0), : (2,1),(1,2),(0,3),(1,4),(2,5)]. : the "number" of each element is the corresponding position it is in the : original array. : 2. after numbering, sort the new array according to their original value , : using quicksort. (O(nlogn)) : e.g. after sorting, [3,2,1,0,1,2]->[0,1,1,2,2,3]. ( I didn't list the " : position" of each element here). : 3. find the lo
no. in step3 , all elements have already been sorted. for example, [3,2,1,0,1,2]->[0,1,1,2,2,3] actually, if I write out each element in the format (value, position), the above will become [(3,1),(2,2) ,(1,3),(0,4),(1,5),(2,6)]->[(0,4),(1,3),(1,5),(2,2) ,(2,6),(3,1 )]. in the resulting array, when scanning, you will get (0,4) first, put it in resulting queue 1. then you get (1,3), since (3<4),you will dump (0,4), put (1,3) in queue 1 then you get (1,5), keep it in queue1.. since 5>3 then you
the reason not to dump (2,2) is that we already have 2 elements in queue2, in this case, if you dump 2 elements in exchange for 1 element, it would be worse.
the ,1 in
【在 j*********h 的大作中提到】 : no. : in step3 , all elements have already been sorted. : for example, : [3,2,1,0,1,2]->[0,1,1,2,2,3] : actually, if I write out each element in the format (value, position), the : above will become : [(3,1),(2,2) ,(1,3),(0,4),(1,5),(2,6)]->[(0,4),(1,3),(1,5),(2,2) ,(2,6),(3,1 : )]. : in the resulting array, when scanning, you will get (0,4) first, put it in : resulting queue 1.
z*y
29 楼
public static int MCSS(int [] a) { int max = 0, sum = 0, start = 0, end = 0, i=0; // Cycle through all possible end indexes. for (j = 0; j < a.length; j++) { sum += a[j]; // No need to re-add all values. if (sum > max) { max = sum; start = i; // Although method doesn't return these end = j; // they can be computed. } else if (sum < 0) { i = j+1; // Only possible MCSSs start with an index >j. s
【在 r********7 的大作中提到】 : 比方说:0,2,0,4,3,1,5,0 =〉0,2,3,5 : 我的解法是sort this sequence X to Y, Get the LCS of X and Y. : Time = O(nlogn + n^2) = O(N^2) : 对方说还能更快。谁知道该怎么改进? : Updated: 我忘了说了,是求*最长*单调上升子列
y*g
30 楼
你这个是什么?
【在 z********y 的大作中提到】 : public static int MCSS(int [] a) { : int max = 0, sum = 0, start = 0, end = 0, i=0; : // Cycle through all possible end indexes. : for (j = 0; j < a.length; j++) { : sum += a[j]; // No need to re-add all values. : if (sum > max) { : max = sum; : start = i; // Although method doesn't return these : end = j; // they can be computed. : }