Skip to content

Commit ec5a38e

Browse files
add 1780
1 parent 65f3624 commit ec5a38e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1780.java) ||Medium|Math, Backtracking, Recursion|
1112
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1779.java) ||Easy|Array|
1213
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java) ||Medium|Greedy|
1314
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1773.java) ||Easy|Array, String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1780 {
7+
public static class Solution1 {
8+
public boolean checkPowersOfThree(int n) {
9+
List<Integer> powers = new ArrayList<>();
10+
int power = 1;
11+
for (int i = 1; power <= n; i++) {
12+
powers.add(power);
13+
power = (int) Math.pow(3, i);
14+
}
15+
int i = powers.size() - 1;
16+
while (n > 0 && i >= 0) {
17+
if (n - powers.get(i) > 0) {
18+
n -= powers.get(i--);
19+
} else if (n - powers.get(i) == 0) {
20+
return true;
21+
} else {
22+
i--;
23+
}
24+
}
25+
return n == 0;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)