Skip to content

Commit d93d92c

Browse files
refactor 465
1 parent e31b377 commit d93d92c

File tree

1 file changed

+51
-48
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+51
-48
lines changed
Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.ArrayList;
54
import java.util.Collections;
65
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.
5453
Therefore, person #1 only need to give person #0 $4, and all debt is settled.
5554
*/
5655
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;
8963
}
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);
9271
}
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) {
9977
continue;
10078
}
101-
if (n > p) {
102-
stNeg.push(n - p);
79+
if (m < 0) {
80+
negs.add(-m);
10381
} 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+
}
105108
}
109+
ans = Math.min(ans, cur);
110+
Collections.shuffle(negs);
111+
Collections.shuffle(poss);
106112
}
107-
ans = Math.min(ans, cur);
108-
Collections.shuffle(negs);
109-
Collections.shuffle(poss);
113+
return ans;
110114
}
111-
return ans;
112115
}
113116
}

0 commit comments

Comments
 (0)