File tree 1 file changed +10
-11
lines changed
src/main/java/com/leetcode/arrays 1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change @@ -39,20 +39,19 @@ public class BuySellStocks {
39
39
* @param prices
40
40
* @return
41
41
*/
42
- public static int maxProfit (int [] prices ) {
43
- int profit = 0 ;
44
- int buyPrice = Integer .MAX_VALUE ;
45
-
46
- for (int i = 0 ; i < prices .length ; i ++) {
47
- if (prices [i ] < buyPrice ) {
48
- buyPrice = prices [i ];
49
- } else if (prices [i ] - buyPrice > profit ) {
50
- profit = prices [i ] - buyPrice ;
42
+ public class Solution {
43
+ public int maxProfit (int prices []) {
44
+ int maxprofit = 0 ;
45
+ for (int i = 0 ; i < prices .length - 1 ; i ++) {
46
+ for (int j = i + 1 ; j < prices .length ; j ++) {
47
+ int profit = prices [j ] - prices [i ];
48
+ if (profit > maxprofit )
49
+ maxprofit = profit ;
51
50
}
52
51
}
53
-
54
- return profit ;
52
+ return maxprofit ;
55
53
}
54
+ }
56
55
57
56
public static void main (String [] args ) {
58
57
You can’t perform that action at this time.
0 commit comments