Skip to content

Commit d0b3a95

Browse files
committed
update
1 parent 31aea67 commit d0b3a95

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

123_maxProfit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public int maxProfit(int[] prices) {
1010
//All dp[i][0] = 0 due to if you do i transaction but you don't see any stock there, profit is 0
1111
int res = 0;
1212
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.
1414
for(int j = 1; j < prices.length; j++){
1515
dp[i][j] = Math.max(dp[i][j-1], prices[j]+tmpMax);
1616
tmpMax = Math.max(tmpMax, dp[i-1][j]-prices[j]);

414_thirdMax.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

0 commit comments

Comments
 (0)