1- //Solution1: divide and conquer T: O(nlogn ) S: O(logn )
1+ // Solution: dp 状态机模型 T: O(n ) S: O(n )
22class Solution {
33 public int maxProfit (int [] prices ) {
4- if (prices .length <= 1 )
5- return 0 ;
6- int res = subMax (prices , 0 , prices .length - 1 );
7- return res ;
4+ if (prices == null || prices .length <= 1 ) return 0 ;
5+ int n = prices .length ;
6+ int [][] dp = new int [n ][2 ];
7+ dp [0 ][0 ] = 0 ;
8+ dp [0 ][1 ] = -prices [0 ];
9+ for (int i = 1 ; i < n ; i ++) {
10+ dp [i ][1 ] = Math .max (dp [i -1 ][1 ], -prices [i ]);
11+ dp [i ][0 ] = Math .max (dp [i -1 ][0 ], dp [i -1 ][1 ] + prices [i ]);
12+ }
13+ return dp [n -1 ][0 ];
814 }
9-
10- public int subMax (int [] prices , int start , int end ) {
11- if (start == end ) return 0 ;
15+ }
16+
17+ // 状态压缩优化 T: O(n) S: O(1)
18+ class Solution {
19+ public int maxProfit (int [] prices ) {
20+ if (prices == null || prices .length <= 1 ) return 0 ;
21+ int n = prices .length ;
1222
13- int mid = (start + end )/2 ;
14- int tmp = getMax (prices , mid + 1 , end ) - getMin (prices , start , mid );
15- if (tmp < 0 ) tmp = 0 ;
23+ int keep = 0 ;
24+ int buy = -prices [0 ];
1625
17- int tmp2 = Math .max (subMax (prices , start , mid ), subMax (prices , mid + 1 , end ));
18- return Math .max (tmp , tmp2 );
19- }
20-
21- //Get max in the array prices from index start to end
22- public int getMax (int [] prices , int start , int end ) {
23- int max = prices [start ];
24- for (int i = start ; i <= end ; i ++) {
25- if (prices [i ] > max )
26- max = prices [i ];
26+ for (int i = 1 ; i < n ; i ++) {
27+ keep = Math .max (keep , buy + prices [i ]);
28+ buy = Math .max (buy , -prices [i ]);
2729 }
28- return max ;
30+ return keep ;
2931 }
30-
31- //Get min in the array prices from index start to end
32- public int getMin (int [] prices , int start , int end ) {
33- int min = prices [start ];
34- for (int i = start ; i <= end ; i ++) {
35- if (prices [i ] < min )
36- min = prices [i ];
37- }
38- return min ;
39- }
40-
4132}
4233
4334
44- //Solution2: Kadane's Algorithm (dynamic programming) T: O(n) S: O(1)
35+ // Solution: dp 转换为求最大子数组和的问题 T: O(n) S: O(1)
4536class Solution {
4637 public int maxProfit (int [] prices ) {
47- int maxCur = 0 , maxSofar = 0 ;
48- for ( int i = 1 ; i < prices .length ; i ++) {
49- maxCur = Math . max ( 0 , maxCur += prices [ i ] - prices [ i - 1 ]);
50- maxSofar = Math . max ( maxCur , maxSofar ) ;
38+ if ( prices == null || prices . length <= 1 ) return 0 ;
39+ int [] diff = new int [ prices .length - 1 ];
40+ for ( int i = 0 ; i < diff . length ; i ++) {
41+ diff [ i ] = prices [ i + 1 ] - prices [ i ] ;
5142 }
52- return maxSofar ;
43+ int res = findMaxSubarray (diff );
44+ return res > 0 ? res : 0 ;
45+ }
46+
47+ int findMaxSubarray (int [] arr ) {
48+ int [] dp = new int [arr .length ];
49+ dp [0 ] = arr [0 ];
50+ int max = dp [0 ];
51+ for (int i = 1 ; i < arr .length ; i ++) {
52+ dp [i ] = Math .max (arr [i ], dp [i -1 ] + arr [i ]);
53+ max = Math .max (dp [i ], max );
54+ }
55+ return max ;
5356 }
5457}
0 commit comments