From 92e87cbf0355dd92425cbcc062dfe362f2078fff Mon Sep 17 00:00:00 2001 From: fanxb Date: Wed, 23 Feb 2022 23:29:58 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q121.java | 24 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q122.java | 28 +++++++------------ 5.leetcode/src/com/fanxb/common/Q123.java | 34 +++++++++++++++++++++++ 3 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q121.java create mode 100644 5.leetcode/src/com/fanxb/common/Q123.java diff --git a/5.leetcode/src/com/fanxb/common/Q121.java b/5.leetcode/src/com/fanxb/common/Q121.java new file mode 100644 index 0000000..e76603a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q121.java @@ -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; + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q122.java b/5.leetcode/src/com/fanxb/common/Q122.java index ecd4890..da1c8b2 100644 --- a/5.leetcode/src/com/fanxb/common/Q122.java +++ b/5.leetcode/src/com/fanxb/common/Q122.java @@ -6,9 +6,7 @@ import java.util.List; import java.util.Map; /** - * 题目地址: https://leetcode-cn.com/problems/partition-labels/ - * 解题思路:首先遍历一次字符串,记录每个字母最后出现的位置 - * 然后再遍历一遍,记录当前字符串的开始位置start,结束位置end. 当第i个字母的结束位置》end时end=第i个字母的结束位置,知道i=end说明当前位置为字符串的结束位置 + * 找到每次从低点到高点的变化即可 * * @author fanxb * Date: 2020/6/9 15:10 @@ -17,25 +15,19 @@ public class Q122 { public static int solution(int[] prices) { int res = 0; - int start = -1, end = -1; + int start = -1; for (int i = 0; i < prices.length - 1; i++) { - if (prices[i + 1] > prices[i]) { - //说明是上坡 - if (start == -1) { - start = prices[i]; - } - end = prices[i + 1]; - } else if (prices[i + 1] < prices[i]) { - //说明开始下坡了 - if (end > start) { - res += end - start; - } + 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; - end = -1; } } - if (end > start) { - res += end - start; + //判断最后一个能不能卖 + if (start != -1 && prices[prices.length - 1] > start) { + res += prices[prices.length - 1] - start; } return res; } diff --git a/5.leetcode/src/com/fanxb/common/Q123.java b/5.leetcode/src/com/fanxb/common/Q123.java new file mode 100644 index 0000000..e431e91 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q123.java @@ -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)); + } +}