写一个递归函数,计算出从某一个位置开始走的最大可能步数。1A,哈哈。就是时间复杂度、空间复杂度好都有点高。
运行时间:308ms超过2.53% 用Java提交的代码
占用内存:11080KB超过38.23%用Java提交的代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] walls = new int[n];
for(int i = 0; i < n; i++) {
walls[i] = in.nextInt();
}
int max = jump(0, walls, 1);
for(int i = 1; i < n; i++) {
int t = jump(i, walls, 1);
if (max < t) {
max = t;
}
}
System.out.println(max);
}
public static int jump(int pos, int[] walls, int steps) {
int h = walls[pos];
int maxSteps = steps;
for(int i = pos + 1; i < walls.length; i++) {
if (h < walls[i]) {
int t = jump(i, walls, steps + 1);
if (maxSteps < t) {
maxSteps = t;
}
}
}
return maxSteps;
}
}

京公网安备 11010502036488号