Skip to content

Commit 0d05e3e

Browse files
solves calculate money in leetcode bank
1 parent d12afc9 commit 0d05e3e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@
421421
| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike) | [![Java](assets/java.png)](src/DetermineIfStringHalvesAreAlike.java) | |
422422
| 1708 | 🔒 [Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/) | | |
423423
| 1710 | [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | [![Java](assets/java.png)](src/MaximumUnitsOnATruck.java) | |
424-
| 1716 | [Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank) | | |
424+
| 1716 | [Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank) | [![Java](assets/java.png)](src/CalculateMoneyInLeetCodeBank.java) | |
425425
| 1720 | [Decode XORed Array](https://leetcode.com/problems/decode-xored-array) | | |
426426
| 1725 | [Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square) | | |
427427
| 1732 | [Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude) | | |

src/CalculateMoneyInLeetCodeBank.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class CalculateMoneyInLeetCodeBank {
2+
private static final int SUM_7 = 28;
3+
4+
public int totalMoney (int n) {
5+
final int r = ceil((double) n / 7);
6+
return r * SUM_7
7+
+ 7 * summation(r - 1)
8+
- (summation(6 + r) - summation(n % 7 == 0 ? 6 + r : r - 1 + n % 7));
9+
}
10+
11+
private int ceil(double d) {
12+
return (int) Math.ceil(d);
13+
}
14+
15+
private int summation(int x) {
16+
if (x <= 0) return 0;
17+
return (x * (x + 1)) / 2;
18+
}
19+
}

0 commit comments

Comments
 (0)