|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.ArrayList;
|
5 | 4 | import java.util.Collections;
|
6 | 5 | import java.util.HashMap;
|
@@ -54,60 +53,64 @@ A transaction will be given as a tuple (x, y, z). Note that x ? y and z > 0.
|
54 | 53 | Therefore, person #1 only need to give person #0 $4, and all debt is settled.
|
55 | 54 | */
|
56 | 55 | public class _465 {
|
57 |
| - /**Reference: https://discuss.leetcode.com/topic/68948/easy-java-solution-with-explanation*/ |
58 |
| - public int minTransfers(int[][] transactions) { |
59 |
| - if (transactions == null || transactions.length == 0) { |
60 |
| - return 0; |
61 |
| - } |
62 |
| - Map<Integer, Integer> acc = new HashMap<>(); |
63 |
| - for (int i = 0; i < transactions.length; i++) { |
64 |
| - int id1 = transactions[i][0]; |
65 |
| - int id2 = transactions[i][1]; |
66 |
| - int m = transactions[i][2]; |
67 |
| - acc.put(id1, acc.getOrDefault(id1, 0) - m); |
68 |
| - acc.put(id2, acc.getOrDefault(id2, 0) + m); |
69 |
| - } |
70 |
| - List<Integer> negs = new ArrayList<>(); |
71 |
| - List<Integer> poss = new ArrayList<>(); |
72 |
| - for (Integer key : acc.keySet()) { |
73 |
| - int m = acc.get(key); |
74 |
| - if (m == 0) { |
75 |
| - continue; |
76 |
| - } |
77 |
| - if (m < 0) { |
78 |
| - negs.add(-m); |
79 |
| - } else { |
80 |
| - poss.add(m); |
81 |
| - } |
82 |
| - } |
83 |
| - int ans = Integer.MAX_VALUE; |
84 |
| - Stack<Integer> stNeg = new Stack<>(); |
85 |
| - Stack<Integer> stPos = new Stack<>(); |
86 |
| - for (int i = 0; i < 1000; i++) { |
87 |
| - for (Integer num : negs) { |
88 |
| - stNeg.push(num); |
| 56 | + public static class Solution1 { |
| 57 | + /** |
| 58 | + * Credit: https://discuss.leetcode.com/topic/68948/easy-java-solution-with-explanation |
| 59 | + */ |
| 60 | + public int minTransfers(int[][] transactions) { |
| 61 | + if (transactions == null || transactions.length == 0) { |
| 62 | + return 0; |
89 | 63 | }
|
90 |
| - for (Integer num : poss) { |
91 |
| - stPos.push(num); |
| 64 | + Map<Integer, Integer> acc = new HashMap<>(); |
| 65 | + for (int i = 0; i < transactions.length; i++) { |
| 66 | + int id1 = transactions[i][0]; |
| 67 | + int id2 = transactions[i][1]; |
| 68 | + int m = transactions[i][2]; |
| 69 | + acc.put(id1, acc.getOrDefault(id1, 0) - m); |
| 70 | + acc.put(id2, acc.getOrDefault(id2, 0) + m); |
92 | 71 | }
|
93 |
| - int cur = 0; |
94 |
| - while (!stNeg.isEmpty()) { |
95 |
| - int n = stNeg.pop(); |
96 |
| - int p = stPos.pop(); |
97 |
| - cur++; |
98 |
| - if (n == p) { |
| 72 | + List<Integer> negs = new ArrayList<>(); |
| 73 | + List<Integer> poss = new ArrayList<>(); |
| 74 | + for (Integer key : acc.keySet()) { |
| 75 | + int m = acc.get(key); |
| 76 | + if (m == 0) { |
99 | 77 | continue;
|
100 | 78 | }
|
101 |
| - if (n > p) { |
102 |
| - stNeg.push(n - p); |
| 79 | + if (m < 0) { |
| 80 | + negs.add(-m); |
103 | 81 | } else {
|
104 |
| - stPos.push(p - n); |
| 82 | + poss.add(m); |
| 83 | + } |
| 84 | + } |
| 85 | + int ans = Integer.MAX_VALUE; |
| 86 | + Stack<Integer> stNeg = new Stack<>(); |
| 87 | + Stack<Integer> stPos = new Stack<>(); |
| 88 | + for (int i = 0; i < 1000; i++) { |
| 89 | + for (Integer num : negs) { |
| 90 | + stNeg.push(num); |
| 91 | + } |
| 92 | + for (Integer num : poss) { |
| 93 | + stPos.push(num); |
| 94 | + } |
| 95 | + int cur = 0; |
| 96 | + while (!stNeg.isEmpty()) { |
| 97 | + int n = stNeg.pop(); |
| 98 | + int p = stPos.pop(); |
| 99 | + cur++; |
| 100 | + if (n == p) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + if (n > p) { |
| 104 | + stNeg.push(n - p); |
| 105 | + } else { |
| 106 | + stPos.push(p - n); |
| 107 | + } |
105 | 108 | }
|
| 109 | + ans = Math.min(ans, cur); |
| 110 | + Collections.shuffle(negs); |
| 111 | + Collections.shuffle(poss); |
106 | 112 | }
|
107 |
| - ans = Math.min(ans, cur); |
108 |
| - Collections.shuffle(negs); |
109 |
| - Collections.shuffle(poss); |
| 113 | + return ans; |
110 | 114 | }
|
111 |
| - return ans; |
112 | 115 | }
|
113 | 116 | }
|
0 commit comments