- 转圈循环最好用while
435. Non-overlapping Intervals
Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
有两种解决方案 分别按照末尾排和按照起始位置排
按照末尾排,直接碰到overlap就排除
按照起始排,就需要判断一下,如果遍历当前项的interval的end小于记录的end就替换end
for(int i = 1; i < intervals.length; i++) {
if (intervals[i][0] >= end) {
end = intervals[i][1];
} else if (intervals[i][1] < end) {
end = intervals[i][1];
count++;
} else {
count++;
}
}
738. Monotone Increasing Digits
An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
Given an integer n, return the largest number that is less than or equal to n with monotone increasing digits.
Example 1:
Input: n = 10
Output: 9
Example 2:
Input: n = 1234
Output: 1234
Example 3:
Input: n = 332
Output: 299
这道题的贪心思维就是只要是前一个数字小于后一个数字,那么前一个数字减1,后面的数字变成9。没做出来的一个原因是没理解题意