diff --git a/docs/notes/Leetcode 题解 - 贪心思想.md b/docs/notes/Leetcode 题解 - 贪心思想.md index de195026..243aaebe 100644 --- a/docs/notes/Leetcode 题解 - 贪心思想.md +++ b/docs/notes/Leetcode 题解 - 贪心思想.md @@ -2,14 +2,14 @@ * [1. 分配饼干](#1-分配饼干) * [2. 不重叠的区间个数](#2-不重叠的区间个数) * [3. 投飞镖刺破气球](#3-投飞镖刺破气球) -* [3. 根据身高和序号重组队列](#3-根据身高和序号重组队列) -* [4. 买卖股票最大的收益](#4-买卖股票最大的收益) -* [5. 买卖股票的最大收益 II](#5-买卖股票的最大收益-ii) -* [6. 种植花朵](#6-种植花朵) -* [7. 判断是否为子序列](#7-判断是否为子序列) -* [8. 修改一个数成为非递减数组](#8-修改一个数成为非递减数组) -* [9. 子数组最大的和](#9-子数组最大的和) -* [10. 分隔字符串使同种字符出现在一起](#10-分隔字符串使同种字符出现在一起) +* [4. 根据身高和序号重组队列](#4-根据身高和序号重组队列) +* [5. 买卖股票最大的收益](#5-买卖股票最大的收益) +* [6. 买卖股票的最大收益 II](#6-买卖股票的最大收益-ii) +* [7. 种植花朵](#7-种植花朵) +* [8. 判断是否为子序列](#8-判断是否为子序列) +* [9. 修改一个数成为非递减数组](#9-修改一个数成为非递减数组) +* [10. 子数组最大的和](#10-子数组最大的和) +* [11. 分隔字符串使同种字符出现在一起](#11-分隔字符串使同种字符出现在一起) @@ -78,18 +78,18 @@ Explanation: You don't need to remove any of the intervals since they're already 按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。 ```java -public int eraseOverlapIntervals(Interval[] intervals) { +public int eraseOverlapIntervals(int[][] intervals) { if (intervals.length == 0) { return 0; } - Arrays.sort(intervals, Comparator.comparingInt(o -> o.end)); + Arrays.sort(intervals, Comparator.comparingInt(o -> o[1])); int cnt = 1; - int end = intervals[0].end; + int end = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { - if (intervals[i].start < end) { + if (intervals[i][0] < end) { continue; } - end = intervals[i].end; + end = intervals[i][1]; cnt++; } return intervals.length - cnt; @@ -99,10 +99,10 @@ public int eraseOverlapIntervals(Interval[] intervals) { 使用 lambda 表示式创建 Comparator 会导致算法运行时间过长,如果注重运行时间,可以修改为普通创建 Comparator 语句: ```java -Arrays.sort(intervals, new Comparator() { +Arrays.sort(intervals, new Comparator() { @Override - public int compare(Interval o1, Interval o2) { - return o1.end - o2.end; + public int compare(int[] o1, int[] o2) { + return o1[1] - o2[1]; } }); ``` @@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) { } ``` -# 3. 根据身高和序号重组队列 +# 4. 根据身高和序号重组队列 [406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/) @@ -173,7 +173,7 @@ public int[][] reconstructQueue(int[][] people) { } ``` -# 4. 买卖股票最大的收益 +# 5. 买卖股票最大的收益 [121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) @@ -196,7 +196,7 @@ public int maxProfit(int[] prices) { ``` -# 5. 买卖股票的最大收益 II +# 6. 买卖股票的最大收益 II [122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) @@ -217,7 +217,7 @@ public int maxProfit(int[] prices) { ``` -# 6. 种植花朵 +# 7. 种植花朵 [605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/) @@ -247,7 +247,7 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) { } ``` -# 7. 判断是否为子序列 +# 8. 判断是否为子序列 [392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/) @@ -269,7 +269,7 @@ public boolean isSubsequence(String s, String t) { } ``` -# 8. 修改一个数成为非递减数组 +# 9. 修改一个数成为非递减数组 [665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/) @@ -303,7 +303,7 @@ public boolean checkPossibility(int[] nums) { -# 9. 子数组最大的和 +# 10. 子数组最大的和 [53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) @@ -327,7 +327,7 @@ public int maxSubArray(int[] nums) { } ``` -# 10. 分隔字符串使同种字符出现在一起 +# 11. 分隔字符串使同种字符出现在一起 [763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/) diff --git a/notes/Leetcode 题解 - 贪心思想.md b/notes/Leetcode 题解 - 贪心思想.md index de195026..243aaebe 100644 --- a/notes/Leetcode 题解 - 贪心思想.md +++ b/notes/Leetcode 题解 - 贪心思想.md @@ -2,14 +2,14 @@ * [1. 分配饼干](#1-分配饼干) * [2. 不重叠的区间个数](#2-不重叠的区间个数) * [3. 投飞镖刺破气球](#3-投飞镖刺破气球) -* [3. 根据身高和序号重组队列](#3-根据身高和序号重组队列) -* [4. 买卖股票最大的收益](#4-买卖股票最大的收益) -* [5. 买卖股票的最大收益 II](#5-买卖股票的最大收益-ii) -* [6. 种植花朵](#6-种植花朵) -* [7. 判断是否为子序列](#7-判断是否为子序列) -* [8. 修改一个数成为非递减数组](#8-修改一个数成为非递减数组) -* [9. 子数组最大的和](#9-子数组最大的和) -* [10. 分隔字符串使同种字符出现在一起](#10-分隔字符串使同种字符出现在一起) +* [4. 根据身高和序号重组队列](#4-根据身高和序号重组队列) +* [5. 买卖股票最大的收益](#5-买卖股票最大的收益) +* [6. 买卖股票的最大收益 II](#6-买卖股票的最大收益-ii) +* [7. 种植花朵](#7-种植花朵) +* [8. 判断是否为子序列](#8-判断是否为子序列) +* [9. 修改一个数成为非递减数组](#9-修改一个数成为非递减数组) +* [10. 子数组最大的和](#10-子数组最大的和) +* [11. 分隔字符串使同种字符出现在一起](#11-分隔字符串使同种字符出现在一起) @@ -78,18 +78,18 @@ Explanation: You don't need to remove any of the intervals since they're already 按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。 ```java -public int eraseOverlapIntervals(Interval[] intervals) { +public int eraseOverlapIntervals(int[][] intervals) { if (intervals.length == 0) { return 0; } - Arrays.sort(intervals, Comparator.comparingInt(o -> o.end)); + Arrays.sort(intervals, Comparator.comparingInt(o -> o[1])); int cnt = 1; - int end = intervals[0].end; + int end = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { - if (intervals[i].start < end) { + if (intervals[i][0] < end) { continue; } - end = intervals[i].end; + end = intervals[i][1]; cnt++; } return intervals.length - cnt; @@ -99,10 +99,10 @@ public int eraseOverlapIntervals(Interval[] intervals) { 使用 lambda 表示式创建 Comparator 会导致算法运行时间过长,如果注重运行时间,可以修改为普通创建 Comparator 语句: ```java -Arrays.sort(intervals, new Comparator() { +Arrays.sort(intervals, new Comparator() { @Override - public int compare(Interval o1, Interval o2) { - return o1.end - o2.end; + public int compare(int[] o1, int[] o2) { + return o1[1] - o2[1]; } }); ``` @@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) { } ``` -# 3. 根据身高和序号重组队列 +# 4. 根据身高和序号重组队列 [406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/) @@ -173,7 +173,7 @@ public int[][] reconstructQueue(int[][] people) { } ``` -# 4. 买卖股票最大的收益 +# 5. 买卖股票最大的收益 [121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) @@ -196,7 +196,7 @@ public int maxProfit(int[] prices) { ``` -# 5. 买卖股票的最大收益 II +# 6. 买卖股票的最大收益 II [122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) @@ -217,7 +217,7 @@ public int maxProfit(int[] prices) { ``` -# 6. 种植花朵 +# 7. 种植花朵 [605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/) @@ -247,7 +247,7 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) { } ``` -# 7. 判断是否为子序列 +# 8. 判断是否为子序列 [392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/) @@ -269,7 +269,7 @@ public boolean isSubsequence(String s, String t) { } ``` -# 8. 修改一个数成为非递减数组 +# 9. 修改一个数成为非递减数组 [665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/) @@ -303,7 +303,7 @@ public boolean checkPossibility(int[] nums) { -# 9. 子数组最大的和 +# 10. 子数组最大的和 [53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/) @@ -327,7 +327,7 @@ public int maxSubArray(int[] nums) { } ``` -# 10. 分隔字符串使同种字符出现在一起 +# 11. 分隔字符串使同种字符出现在一起 [763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)