一、俄罗斯套娃信封Leecode354,参考宫水三叶答案
package zsh;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class MaxEnvelopes {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] s = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
int num = s.length/2; //信封个数
int[][] arr = new int[num][2];
for(int i = 0; i < s.length/2; i++){
arr[i][0] = Integer.parseInt(s[i*2]);
arr[i][1] = Integer.parseInt(s[i*2+1]);
}
int[] count = new int[num];
// 因为我们在找第 i 件物品的前一件物品时,会对前面的 i - 1 件物品都遍历一遍,因此第二维(高度)排序与否都不影响
// 先按底边排序
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
int max = 1; //最少是本身1个
for(int i = 0; i < num; i++){
count[i] = 1; //最小值为1
for(int j = i - 1; j >= 0; j--){
if(arr[j][0] < arr[i][0] && arr[j][1] < arr[i][1]){
count[i] = Math.max(count[i], count[j]+1);
}
}
max = Math.max(max, count[i]);
}
System.out.println(max);
}
}
改编:垒积木,当一个积木长宽不比另一个积木大,则这个积木能够垒在另一个积木上面,问最多垒多少层,积木可旋转
不同:(1)积木可旋转,我们可以先将较大的一边保存在a[i][0],较小的边保存到a[i][1];
(2)不用绝对大于,不小于即可,这里我们在首先对数组排序就要多加一步,先比长边,长边相等比短边,这样在下面排序 就会为 [5,4][6,3][6,4][6,6][7,6], 而原来的排序规则会 [5,4][6,3][6,6][6,4][7,6]这样现象出现,在按照上述从前向后动态规划时就会漏掉(如[6,6]装不到[6,4],程序只装前面的),上面题不会出现是因为比的绝对大,不考虑相同边情况,而绝对大一定会按顺序来。
输入 [[5,4],[6,3],[6,7],[6,6],[4,6]]
输出 4
package zsh;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class MaxSquare {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] s = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
int num = s.length/2; //信封个数
int[][] arr = new int[num][2];
for(int i = 0; i < s.length/2; i++){
arr[i][0] = Math.max(Integer.parseInt(s[i*2]), Integer.parseInt(s[i*2+1]));
arr[i][1] = Math.min(Integer.parseInt(s[i*2]), Integer.parseInt(s[i*2+1]));
// arr[i][0] = Integer.parseInt(s[i*2]);
// arr[i][1] = Integer.parseInt(s[i*2+1]);
}
int[] count = new int[num];
// 因为我们在找第 i 件物品的前一件物品时,会对前面的 i - 1 件物品都遍历一遍,因此第二维(高度)排序与否都不影响
// 先按底边排序
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] != o2[0]){
return o1[0] - o2[0];
}
return o1[1] - o2[1];
}
});
int max = 1; //最少是本身1个
for(int i = 0; i < num; i++){
count[i] = 1; //最小值为1
for(int j = i - 1; j >= 0; j--){
if(arr[j][0] <= arr[i][0] && arr[j][1] <= arr[i][1]){
count[i] = Math.max(count[i], count[j]+1);
}
}
max = Math.max(max, count[i]);
}
System.out.println(max);
}
}
// [[5,4],[6,3],[6,7],[6,6],[4,6]]
二、接雨水leecode42
参考解法三
package zsh;
import java.util.Scanner;
public class RainLeecode42 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] strArr = scanner.nextLine().trim().replace("[", "").replace("]", "").split(",");
int num = strArr.length;
int[] height = new int[num];
for(int i = 0; i < num; i++){
height[i] = Integer.parseInt(strArr[i]);
}
int[] leftMax = new int[num];
int[] rightMax = new int[num];
for(int i = 1; i < num - 1; i++){ //第i列左边最高
leftMax[i] = Math.max(leftMax[i-1], height[i-1]);
}
for(int i = num - 2; i > 0; i--){ //第i列右边最高
rightMax[i] = Math.max(rightMax[i+1], height[i+1]);
}
int sum = 0;
for(int i = 1; i < num - 1; i++){
int min = Math.min(leftMax[i], rightMax[i]); //木桶效应,找出两边最高中较小值
if(height[i] < min){ //i列高度小于两边较小值,和较小值相差部分即可存水
sum += min - height[i];
}
}
System.out.println(sum);
}
}
改编:坑处存货(时间复杂度O(M*N)太大,不行)
package zsh;
import java.util.Arrays;
import java.util.Scanner;
public class LadderCargo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cargoLength = Integer.parseInt(scanner.nextLine().trim());
int num = Integer.parseInt(scanner.nextLine().trim());
int[] height = Arrays.stream(scanner.nextLine().trim().split(",")).mapToInt(Integer::valueOf).toArray();
int minHeight = Arrays.stream(height).min().getAsInt();
int sum = 0;
if(minHeight < 0) {
int[] count = new int[Math.abs(minHeight)];
for (int i = 0; i < num; i++) {
if (height[i] >= 0) {
Arrays.fill(count, 0); //大于等于地平面累计中断清零
continue;
}
for(int j = 0; j < Math.abs(height[i]); j++){
count[j]++; //每一层+1
if(count[j] == cargoLength){ //达到货物长度清零
sum++; //已放货物数量
count[j] = 0;
}
}
}
}
System.out.println(sum);
}
}
// 0,-1,-2,0
//3 8 0,-1,-2,2,1,1,1,2
// 2
// 11
// 0,-1,-2,-3,-3,-2,0,1,2,-3,-3
// 输出 8
三、送外卖,大厦共L层(0<L<=10^5,处于第N层,每分钟可步行向上到N+1层,或向下到N-1层,或者坐电梯到2*N层,计算到达M层的最短时间。
输入 N M
5 17
输出 4
解释 5-10-9-18-17
package zsh;
import java.util.Arrays;
import java.util.Scanner;
public class ClimbStairs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int base = scanner.nextInt();
int aim = scanner.nextInt();
int res = 0;
int[] time = new int[aim+1]; //爬到每一层需要的时间
if(aim <= base){ //目标楼层低于当前楼层,步行,电梯只上不下
System.out.println(base - aim);
}
else{
for (int i = 1; i <= base; i++){
time[i] = base - i;
}
for (int i = base+1; i < aim+1; i++){
int walk = time[i-1]+1; //走路
int elevator = 0; //电梯
if(i%2 == 0){
elevator = time[i/2]+1;
}
else{
elevator = time[(i+1)/2]+2; //多上一层再下一层
}
time[i] = Math.min(walk, elevator);
}
}
System.out.println(time[aim]);
}
}