Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class Calculator {
  5. public static Map<Integer, Integer> buyPass(int[] schedule) {
  6. Map<Integer, Integer> result = new HashMap<>();
  7. dfs(result, schedule, 0, 0);
  8. return result;
  9. }
  10.  
  11. private static int dfs(Map<Integer, Integer> map, int[] plan, int start, int money) {
  12. if (map.containsKey(start)) {
  13. return map.get(start);
  14. }
  15. while (start < plan.length && plan[start] == 0) {
  16. start++;
  17. }
  18. if (start >= plan.length) {
  19. return money;
  20. }
  21.  
  22. Map<Integer, Integer> map1 = new HashMap<>(map);
  23. Map<Integer, Integer> map2 = new HashMap<>(map);
  24. int m1 = dfs(map1, plan, start + 1, money + 2);
  25. int m2 = dfs(map2, plan, start + 2, money + 3);
  26.  
  27. map.clear();
  28. Map<Integer, Integer> tmp = m1 > m2 ? map2 : map1;
  29. for (int key : tmp.keySet()) {
  30. map.put(key, tmp.get(key));
  31. }
  32. map.put(start, m1 > m2 ? 2 : 1);
  33. return m1 > m2 ? m2 : m1;
  34. }
  35.  
  36. public static void main(String[] args) {
  37. int[] schedule = {1, 0, 1, 1, 0, 0, 1};
  38. Map<Integer, Integer> map = Calculator.buyPass(schedule);
  39. System.out.println(map);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement