File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ public int maxProfit(int[] prices) {
10
10
//All dp[i][0] = 0 due to if you do i transaction but you don't see any stock there, profit is 0
11
11
int res = 0 ;
12
12
for (int i = 1 ; i <= k ; i ++){
13
- int tmpMax = dp [i -1 ][0 ]-prices [0 ];
13
+ int tmpMax = dp [i -1 ][0 ]-prices [0 ];//tmpMax is the max value after last buy operation.
14
14
for (int j = 1 ; j < prices .length ; j ++){
15
15
dp [i ][j ] = Math .max (dp [i ][j -1 ], prices [j ]+tmpMax );
16
16
tmpMax = Math .max (tmpMax , dp [i -1 ][j ]-prices [j ]);
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int thirdMax (int [] nums ) {
3
+ if (nums == null || nums .length == 0 ) return 0 ;
4
+ PriorityQueue <Integer > pq = new PriorityQueue <Integer >();
5
+ HashSet <Integer > set = new HashSet <Integer >();
6
+ for (int n : nums ){
7
+ if (!set .contains (n )){
8
+ pq .offer (n );
9
+ set .add (n );
10
+ if (pq .size () > 3 ) pq .poll ();
11
+ }
12
+ }
13
+ if (pq .size () == 3 ) return pq .poll ();
14
+ while (pq .size () != 1 ){
15
+ pq .poll ();
16
+ }
17
+ return pq .poll ();
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments