1. 题目
读题
考查点
2. 解法
思路
代码逻辑
具体实现
public class Solutions {
public static void main(String[] args) {
System.out.println(solution(10, new int[]{2, 3, 4, 8}, new int[]{2, 5}));
System.out.println(solution(10, new int[]{2, 3, 4, 8}, new int[]{3, 4}));
}
public static int solution(int L, int[] stones, int[] distances) {
Arrays.sort(stones);
int minStep = distances[0];
int maxStep = distances[1];
int i = 0;
int ans = 0;
while (i < L) {
i = jump(i + minStep, i + maxStep, stones);
if (contain(stones, i)) {
ans++;
}
}
return ans;
}
public static int jump(int min, int max, int[] stones) {
for (int i = max; i >= min; i--) {
if (!contain(stones, i)) {
return i;
}
}
return max;
}
public static boolean contain(int[] stones, int x) {
for (int i = 0; i < stones.length; i++) {
if (stones[i] == x) {
return true;
}
}
return false;
}
}