分析
代码
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int child = 0;
int cookies = 0;
while (child<g.length && cookies<s.length) {
if (g[child] <= s[cookies]) {
child++;
}
cookies++;
}
return child;
}
分析
代码
public int candy(int[] ratings) {
int[] array = new int[ratings.length];
Arrays.fill(array, 1);
for (int i = 1; i < ratings.length; i++) {
// 右边大于左边,右边=左边+1
if (ratings[i] > ratings[i-1]) {
array[i] = array[i-1] + 1;
}
}
int res = array[ratings.length-1];
for (int i = ratings.length-2; i >= 0; i--) {
// 左边大于右边,如果左边<=右边,则左边=右边+1,不然左边不变
if (ratings[i] > ratings[i+1]) {
array[i] = array[i] <= array[i+1] ? array[i+1] + 1 : array[i];
}
res += array[i];
}
return res;
}
分析
代码
public static int eraseOverlapIntervals(int[][] intervals) {
if (intervals.length <= 1)
return 0;
// 先排序
Arrays.sort(intervals, new Comparator<>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1];
}
});
for (int[] interval : intervals) {
System.out.println(interval[0] + "-" + interval[1]);
}
int res = 0;
int[] prev = intervals[0];
// 如果当前区间和prev区间相交,那么则删除。
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < prev[1]) {
res++;
} else {
prev = intervals[i];
}
}
return res;
}
注意
分析
先判断头尾是否满足能种上的条件,然后依次逐个判断能否种上
代码
public static boolean canPlaceFlowers(int[] flowerbed, int n) {
if(n==0) {
return true;
}
int len = flowerbed.length;
if (len == 1) {
return flowerbed[0] == 0 && n == 1;
}
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
flowerbed[0] = 1;
n--;
}
if (flowerbed[len-1] == 0 && flowerbed[len-2] == 0) {
flowerbed[len-1] = 1;
n--;
}
for (int i = 1; i < len - 1; i++) {
if (flowerbed[i-1] == 0 && flowerbed[i+1] == 0 && flowerbed[i] == 0) {
flowerbed[i] = 1;
n--;
}
}
return n <= 0;
}
注意:
分析
代码
public static int findMinArrowShots(int[][] points) {
if (points.length <= 1) {
return points.length;
}
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] > o2[0])
return 1;
else if (o1[0] < o2[0])
return -1;
else {
if (o1[1] > o2[1])
return 1;
else if (o1[1] < o2[1])
return -1;
else
return 0;
}
}
});
int[] temp = points[0];
int res = 1;
for (int i = 1; i < points.length; i++) {
// 相交的情况下
if (temp[1] >= points[i][0]) {
temp[0] = Math.max(temp[0], points[i][0]);
temp[1] = Math.min(temp[1], points[i][1]);
} else {
temp = points[i];
res++;
}
}
return res;
}
注意
-2^31 <= xstart < xend <= 2^31 - 1
o1[0]-o2[0]
分析
代码
public static int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1] != 0 ? o1[1] - o2[1] : o1[0] - o2[0];
}
});
int maxHeight = 0;
for (int[] person : people) {
maxHeight = Math.max(person[0], maxHeight);
}
List<int[]> queue = new LinkedList<>();
for (int i = 0; i < people.length; i++) {
if (people[i][1] == 0 || people[i][0] == maxHeight) {
queue.add(people[i]);
continue;
}
int heigher = 0;
int index = 0;
// 找到高于当前元素的前K个
while (heigher < people[i][1]) {
int[] ints = queue.get(index);
index++;
if (ints[0] >= people[i][0]) {
heigher++;
}
}
// 找到第K+1个,找不到就加在
while (index < queue.size()) {
int[] p = queue.get(index);
if (p[0] < people[i][0]) {
index++;
} else {
break;
}
}
queue.add(index, people[i]);
}
return queue.toArray(people);
}
问题:耗时太长了,不够简练,时间复杂度为O(n^3)
优化改进(来自LeetCode官方题解)
让我们从最简单的情况下思考,当队列中所有人的 (h,k)
都是相同的高度 h
,只有 k
不同时,解决方案很简单:每个人在队列的索引 index = k
。
即使不是所有人都是同一高度,这个策略也是可行的。因为个子矮的人相对于个子高的人是 “看不见” 的,所以可以先安排个子高的人。
总结
简直太强了好吗
新代码
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
}
});
List<int[]> res = new LinkedList<>();
for (int[] person : people) {
res.add(person[1], person);
}
return res.toArray(people);
}
分析
nums[i-1] > nums[i]
),拉高nums[i]
或拉低 nums[i-1]
nums[i]>=nums[i-2]
nums[i]>=nums[i-2]
这种情况时,既可以采取拉高也可以从采取拉低,但是采取拉低能避免 nums[i]<nums[i+1]<nums[i-1]
拉高无法修复的问题nums[i]<nums[i-2]
代码
public boolean checkPossibility(int[] nums) {
int times = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i-1] > nums[i]) {
times++;
if (i == 1 || nums[i-2] <= nums[i]) {
nums[i-1] = nums[i];
} else {
nums[i] = nums[i-1];
}
}
}
return times <= 1;
}
原文:https://www.cnblogs.com/primabrucexu/p/13937478.html