1564. 把箱子放进仓库里 I
题目如下:

先排序,再用指针来获取最大的数字。
class Solution {
public int maxBoxesInWarehouse(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
int[] max = new int[nums2.length];
max[max.length - 1] = nums2[0];
for (int i = 1; i < nums2.length; i++) {
nums2[i] = Math.min(nums2[i], nums2[i - 1]);
max[max.length - 1 - i] = nums2[i];
}
int m1 = 0;
int m2 = 0;
int res = 0;
while (m1 < nums1.length && m2 < max.length) {
if (max[m2] >= nums1[m1]) {
res++;
m1++;
m2++;
} else {
m2++;
}
}
return res;
}
}
网友评论