Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int marcheNocturneLastGen(int n, int[] listePrix, int budget) {
- HashMap<Integer, Integer> storingHashMap = new HashMap<>();
- int currentSum = 0;
- int currentMinLenght = Integer.MAX_VALUE;
- storingHashMap.put(0, -1);
- for (int i = 0; i < n; i++) {
- currentSum += listePrix[i];
- if (!storingHashMap.containsKey(currentSum) || listePrix[i] == 0) {
- storingHashMap.put(currentSum, i);
- }
- if (storingHashMap.containsKey(currentSum - budget)) {
- currentMinLenght = Math.min(currentMinLenght, storingHashMap.get(currentSum) - storingHashMap.get(currentSum - budget));
- }
- }
- if (currentMinLenght == Integer.MAX_VALUE)
- currentMinLenght = -1;
- return currentMinLenght;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement