add
This commit is contained in:
parent
e6daa3aafa
commit
92e87cbf03
24
5.leetcode/src/com/fanxb/common/Q121.java
Normal file
24
5.leetcode/src/com/fanxb/common/Q121.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线性扫描,只需找到最大和最小数的差即可,假设第一个数为最小值,从第二个数开始遍历,如果大于最小值则计算利润,如果小于最小值就将最小值设置当前值,然后继续往后便利,知道遍历完毕
|
||||||
|
*
|
||||||
|
* @author FleyX
|
||||||
|
* date 2022/2/23 22:47
|
||||||
|
*/
|
||||||
|
public class Q121 {
|
||||||
|
|
||||||
|
public int maxProfit(int[] prices) {
|
||||||
|
int max = 0;
|
||||||
|
int minVal = prices[0];
|
||||||
|
for (int i = 1; i < prices.length; i++) {
|
||||||
|
if (prices[i] > minVal) {
|
||||||
|
max = Math.max(max, prices[i] - minVal);
|
||||||
|
} else {
|
||||||
|
minVal = prices[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -6,9 +6,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 题目地址: https://leetcode-cn.com/problems/partition-labels/
|
* 找到每次从低点到高点的变化即可
|
||||||
* 解题思路:首先遍历一次字符串,记录每个字母最后出现的位置
|
|
||||||
* 然后再遍历一遍,记录当前字符串的开始位置start,结束位置end. 当第i个字母的结束位置》end时end=第i个字母的结束位置,知道i=end说明当前位置为字符串的结束位置
|
|
||||||
*
|
*
|
||||||
* @author fanxb
|
* @author fanxb
|
||||||
* Date: 2020/6/9 15:10
|
* Date: 2020/6/9 15:10
|
||||||
@ -17,25 +15,19 @@ public class Q122 {
|
|||||||
|
|
||||||
public static int solution(int[] prices) {
|
public static int solution(int[] prices) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
int start = -1, end = -1;
|
int start = -1;
|
||||||
for (int i = 0; i < prices.length - 1; i++) {
|
for (int i = 0; i < prices.length - 1; i++) {
|
||||||
if (prices[i + 1] > prices[i]) {
|
if (prices[i + 1] > prices[i] && start == -1) {
|
||||||
//说明是上坡
|
//说明是首次上坡
|
||||||
if (start == -1) {
|
|
||||||
start = prices[i];
|
start = prices[i];
|
||||||
}
|
} else if (prices[i + 1] < prices[i] && start != -1) {
|
||||||
end = prices[i + 1];
|
res += prices[i] - start;
|
||||||
} else if (prices[i + 1] < prices[i]) {
|
|
||||||
//说明开始下坡了
|
|
||||||
if (end > start) {
|
|
||||||
res += end - start;
|
|
||||||
}
|
|
||||||
start = -1;
|
start = -1;
|
||||||
end = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (end > start) {
|
//判断最后一个能不能卖
|
||||||
res += end - start;
|
if (start != -1 && prices[prices.length - 1] > start) {
|
||||||
|
res += prices[prices.length - 1] - start;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
34
5.leetcode/src/com/fanxb/common/Q123.java
Normal file
34
5.leetcode/src/com/fanxb/common/Q123.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 题目地址: https://leetcode-cn.com/problems/partition-labels/
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* Date: 2020/6/9 15:10
|
||||||
|
*/
|
||||||
|
public class Q123 {
|
||||||
|
|
||||||
|
public static int solution(int[] prices) {
|
||||||
|
int res = 0;
|
||||||
|
int start = -1;
|
||||||
|
for (int i = 0; i < prices.length - 1; i++) {
|
||||||
|
if (prices[i + 1] > prices[i] && start == -1) {
|
||||||
|
//说明是首次上坡
|
||||||
|
start = prices[i];
|
||||||
|
} else if (prices[i + 1] < prices[i] && start != -1) {
|
||||||
|
res += prices[i] - start;
|
||||||
|
start = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//判断最后一个能不能卖
|
||||||
|
if (start != -1 && prices[prices.length - 1] > start) {
|
||||||
|
res += prices[prices.length - 1] - start;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] prices = {7, 1, 5, 3, 6, 4};
|
||||||
|
System.out.println(solution(prices));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user