Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite297dd0

Browse files
add 465
1 parent64e2758 commite297dd0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Your ideas/fixes/algorithms are more than welcome!
153153
|468|[Validate IP Address](https://leetcode.com/problems/validate-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_468.java) | O(n) |O(1) | Medium | String
154154
|467|[Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_467.java) | O(n) |O(1) | Medium| DP
155155
|466|[Count The Repetitions](https://leetcode.com/problems/count-the-repetitions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_466.java)| O(max(m,n))|O(1) | Hard| DP
156+
|465|[Optimal Account Balancing](https://leetcode.com/problems/optimal-account-balancing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_465.java)| | | Hard| DP
156157
|464|[Can I Win](https://leetcode.com/problems/can-i-win/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_464.java)| O(2^n)|O(n) | Medium| DP
157158
|463|[Island Perimeter](https://leetcode.com/problems/island-perimeter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_463.java)| O(m*n)|O(1)| Easy|
158159
|462|[Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_462.java)| O(nlogn)|O(1)| Medium|
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.*;
4+
5+
/**
6+
* 465. Optimal Account Balancing
7+
*
8+
* A group of friends went on holiday and sometimes lent each other money.
9+
* For example, Alice paid for Bill's lunch for $10.
10+
* Then later Chris gave Alice $5 for a taxi ride.
11+
* We can model each transaction as a tuple (x, y, z) which means person x gave person y $z.
12+
* Assuming Alice, Bill, and Chris are person 0, 1, and 2 respectively (0, 1, 2 are the person's ID),
13+
* the transactions can be represented as [[0, 1, 10], [2, 0, 5]].
14+
15+
Given a list of transactions between a group of people, return the minimum number of transactions required to settle the debt.
16+
17+
Note:
18+
19+
A transaction will be given as a tuple (x, y, z). Note that x ? y and z > 0.
20+
Person's IDs may not be linear, e.g. we could have the persons 0, 1, 2 or we could also have the persons 0, 2, 6.
21+
Example 1:
22+
23+
Input:
24+
[[0,1,10], [2,0,5]]
25+
26+
Output:
27+
2
28+
29+
Explanation:
30+
Person #0 gave person #1 $10.
31+
Person #2 gave person #0 $5.
32+
33+
Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each.
34+
Example 2:
35+
36+
Input:
37+
[[0,1,10], [1,0,1], [1,2,5], [2,0,5]]
38+
39+
Output:
40+
1
41+
42+
Explanation:
43+
Person #0 gave person #1 $10.
44+
Person #1 gave person #0 $1.
45+
Person #1 gave person #2 $5.
46+
Person #2 gave person #0 $5.
47+
48+
Therefore, person #1 only need to give person #0 $4, and all debt is settled.
49+
*/
50+
publicclass_465 {
51+
/**Reference: https://discuss.leetcode.com/topic/68948/easy-java-solution-with-explanation*/
52+
publicintminTransfers(int[][]transactions) {
53+
if (transactions ==null ||transactions.length ==0)return0;
54+
Map<Integer,Integer>acc =newHashMap<>();
55+
for (inti =0;i <transactions.length;i++) {
56+
intid1 =transactions[i][0];
57+
intid2 =transactions[i][1];
58+
intm =transactions[i][2];
59+
acc.put(id1,acc.getOrDefault(id1,0) -m);
60+
acc.put(id2,acc.getOrDefault(id2,0) +m);
61+
}
62+
List<Integer>negs =newArrayList<>();
63+
List<Integer>poss =newArrayList<>();
64+
for (Integerkey :acc.keySet()) {
65+
intm =acc.get(key);
66+
if (m ==0)continue;
67+
if (m <0)negs.add(-m);
68+
elseposs.add(m);
69+
}
70+
intans =Integer.MAX_VALUE;
71+
Stack<Integer>stNeg =newStack<>(),stPos =newStack<>();
72+
for (inti =0;i <1000;i++) {
73+
for (Integernum :negs)stNeg.push(num);
74+
for (Integernum :poss)stPos.push(num);
75+
intcur =0;
76+
while (!stNeg.isEmpty()) {
77+
intn =stNeg.pop();
78+
intp =stPos.pop();
79+
cur++;
80+
if (n ==p)continue;
81+
if (n >p) {
82+
stNeg.push(n -p);
83+
}else {
84+
stPos.push(p -n);
85+
}
86+
}
87+
ans =Math.min(ans,cur);
88+
Collections.shuffle(negs);
89+
Collections.shuffle(poss);
90+
}
91+
returnans;
92+
}
93+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp