2024/9/22 LeetCode Hot100 昨日贪心/动态规划
This commit is contained in:
parent
1bab87e60b
commit
515c15677d
|
@ -0,0 +1,32 @@
|
||||||
|
package com.cool.hot100.greedy;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/09/21/23:06
|
||||||
|
* DayNumber 3
|
||||||
|
* Hard 1
|
||||||
|
* Level 3
|
||||||
|
*/
|
||||||
|
public class Num121 {
|
||||||
|
|
||||||
|
public int maxProfit(int[] prices) {
|
||||||
|
int minPrice=prices[0];
|
||||||
|
int maxProfit=0;
|
||||||
|
for (int i = 1; i < prices.length; i++) {
|
||||||
|
if(prices[i]<minPrice){
|
||||||
|
minPrice=prices[i];
|
||||||
|
}else if(prices[i]-minPrice>maxProfit){
|
||||||
|
maxProfit=prices[i]-minPrice;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maxProfit;
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
maxProfit(new int[]{7,1,5,3,6,4});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue