显示原始代码
import java.util.HashMap;
public class Traveler {
public static boolean canReachLastRoom(
int N, int timeLimit, int[] travelTimes, HashMap<Integer, Integer> rewardRooms) {
int currentTime = timeLimit;
for (int i = 0; i < N - 1; ++i) {
currentTime -= travelTimes[i];
if (rewardRooms.containsKey(i + 1)) {
currentTime += rewardRooms.get(i + 1);
}
if (currentTime <= 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int N = 5;
int timeLimit = 10;
int[] travelTimes = { 2, 3, 1, 2 };
HashMap<Integer, Integer> rewardRooms = new HashMap<>();
rewardRooms.put(2, 5);
rewardRooms.put(4, 3);
boolean result = canReachLastRoom(N, timeLimit, travelTimes, rewardRooms);
System.out.println(result);
}
}