Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public static int marcheNocturneLastGen(int n, int[] listePrix, int budget) {
  2.  
  3. HashMap<Integer, Integer> storingHashMap = new HashMap<>();
  4. int currentSum = 0;
  5. int currentMinLenght = Integer.MAX_VALUE;
  6. storingHashMap.put(0, -1);
  7.  
  8.  
  9. for (int i = 0; i < n; i++) {
  10.  
  11. currentSum += listePrix[i];
  12.  
  13. if (!storingHashMap.containsKey(currentSum) || listePrix[i] == 0) {
  14. storingHashMap.put(currentSum, i);
  15. }
  16.  
  17. if (storingHashMap.containsKey(currentSum - budget)) {
  18. currentMinLenght = Math.min(currentMinLenght, storingHashMap.get(currentSum) - storingHashMap.get(currentSum - budget));
  19. }
  20.  
  21. }
  22. if (currentMinLenght == Integer.MAX_VALUE)
  23. currentMinLenght = -1;
  24.  
  25.  
  26. return currentMinLenght;
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement