Home > other >  LeetCode188 stock issue, they want a solution but don't know that kind of thinking...
LeetCode188 stock issue, they want a solution but don't know that kind of thinking...

Time:12-29

This button to get the brush force...

Prices given an integer array, it's the first I elements prices [I] is a given stock in the first day I price,
Design an algorithm to calculate you can obtain the biggest profit, you can complete most k deal,
(note: you cannot at the same time to participate in more deal before you have to buy again before the sell off the shares),

Example 1:
Input: k=2, prices=,4,1 [2]
Output:
2Explanation: on day 1=2 (stock price) when buying, in the second day (=4) stock price when sold, the exchange can profit=4 and 2=2,

Example 2:
Input: k=2, prices=,2,6,5,0,3 [3]
Output: 7
Explanation: on the second day (=2) stock price when buying, in 3 days (=6) stock price when sold, the exchange can profit=6-2=4,
Then, in the fifth day (=0) stock price when buying, in 6 days (=3) stock price when sold, the exchange can profit=3-0=3,

See a lot of their thinking, a see three-dimensional dp is stupid... Then oneself suppress a solution, use case is limited, also don't know right? Bosses, please give directions.

My solution is this:
1. Every element of the array gain the biggest difference between the forward;
2. Poor traverse value set for the reverse, if there is a reverse, in the reverse order to between truncation:
A. reverse against former elements for the biggest profit in the former period;
B. after reverse the elements for a ladder, after a period of a new reverse, profit for the current element - ladder;
C. if the tail does not appear reverse difference set, the tail element - ladder for the last one of the biggest profit;
3. Will be ordered each subsection maximum profit, limited before taking a sum is the results;

The overall complex to O (nlogn) time, space is O (n), the following is a Java code:

 
/* *
* : three-dimensional dp don't understand,,,
* 1, every element of the array gain the biggest difference between the forward;
* 2, traverse difference set, find the reverse, if there is a reverse, between the reverse of truncation:
* a. reverse against former elements for the biggest profit in the former period;
* b, reverse to the element as a ladder, after the back before a new reverse, profit for the current element - ladder;
* c, if not present reverse tail difference set, the tail element - ladder for the last one of the biggest profit;
* 3, will be ordered each subsection maximum profit, limited before taking a sum is the results;
*/
@ TestTimmer
Public static int method_0 (Integer [] prices, Integer limited) {
int result=0;
If (Objects. NonNull (prices) & amp; & Prices. The length & gt; 0 & amp; & Limited & gt; 0 {
//to calculate the maximum difference of all elements;
Int [] temp=new int [prices. Length];
//before the minimum element;
Int min=prices [0];
For (int I=1; I & lt; Prices. Length; I++) {
Min=Math. Min (min, prices [I]);
\ [I]=prices [I] - min;
}
//traverse the biggest difference, discovered the truncation in reverse order, a maximum will be included in the array;
Int [] results=new int [prices. Length & gt; & gt; 1];
Min=0;
Int step=0;
For (int j=1; J & lt; Temp. Length; J++) {
//in reverse;
If (temp [j] The results (min)=temp] [j - 1 - step;
Min++;
Step=temp [j];
} else if (j==temp. Length - 1) {
The results (min)=temp [j] - step;
break;
}
}
//calculate maximum;
If (limited & gt;=the length) {
Result=IntStream. Of (results). The sum ();
} else {
The Arrays. Sort (results);
For (int k=the length - 1; K & gt; Results. The length - limited - 1; K -) {
The result +=results [k].
}
}
}
return result;
}


The computational algorithms for the old white, places don't know all, bosses, please give directions.

@ TestTimer is my own writing test use annotations, mass comparison algorithm of time space consumption. Ignore!!!!!! Ignore!!!!!!
  • Related