-
Notifications
You must be signed in to change notification settings - Fork 10
/
Best Time to Buy and Sell Stock II.java
50 lines (43 loc) · 1.62 KB
/
Best Time to Buy and Sell Stock II.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
*/
public class Solution {
public int maxProfit(int[] prices) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
boolean bought = false;
int profit = 0;
if(prices.length == 0||prices.length == 1) {
return 0;
}
for(int i = 0; i < prices.length - 1; i++) {
if(!bought) {
if(prices[i] <= min) {
min = prices[i];
if(prices[i+1] > min) {
bought = true;
profit -= min;
}
}
}
else {
if(prices[i] >= max) {
max = prices[i];
if(prices[i+1] < max) {
bought = false;
profit += max;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
}
}
}
}
if(bought) {
max = prices[prices.length - 1];
profit += max;
}
return profit;
}
}