Skip to content

Commit 8437bda

Browse files
authored
Improved task 1780.
1 parent 626a13e commit 8437bda

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
22832283
| 1784 |[Check if Binary String Has at Most One Segment of Ones](src.save/main/java/g1701_1800/s1784_check_if_binary_string_has_at_most_one_segment_of_ones/Solution.java)| Easy | String | 1 | 65.60
22842284
| 1782 |[Count Pairs Of Nodes](src.save/main/java/g1701_1800/s1782_count_pairs_of_nodes/Solution.java)| Hard | Binary_Search, Two_Pointers, Graph | 128 | 86.96
22852285
| 1781 |[Sum of Beauty of All Substrings](src.save/main/java/g1701_1800/s1781_sum_of_beauty_of_all_substrings/Solution.java)| Medium | String, Hash_Table, Counting | 38 | 100.00
2286-
| 1780 |[Check if Number is a Sum of Powers of Three](src.save/main/java/g1701_1800/s1780_check_if_number_is_a_sum_of_powers_of_three/Solution.java)| Medium | Math | 2 | 19.71
2286+
| 1780 |[Check if Number is a Sum of Powers of Three](src.save/main/java/g1701_1800/s1780_check_if_number_is_a_sum_of_powers_of_three/Solution.java)| Medium | Math | 0 | 100.00
22872287
| 1779 |[Find Nearest Point That Has the Same X or Y Coordinate](src.save/main/java/g1701_1800/s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate/Solution.java)| Easy | Array, Programming_Skills_I_Day_3_Conditional_Statements | 1 | 100.00
22882288
| 1776 |[Car Fleet II](src.save/main/java/g1701_1800/s1776_car_fleet_ii/Solution.java)| Hard | Array, Math, Stack, Heap_Priority_Queue, Monotonic_Stack | 19 | 93.81
22892289
| 1775 |[Equal Sum Arrays With Minimum Number of Operations](src.save/main/java/g1701_1800/s1775_equal_sum_arrays_with_minimum_number_of_operations/Solution.java)| Medium | Array, Hash_Table, Greedy, Counting | 16 | 70.88
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
package g1701_1800.s1780_check_if_number_is_a_sum_of_powers_of_three;
22

3-
// #Medium #Math #2022_04_30_Time_2_ms_(19.71%)_Space_41.1_MB_(41.61%)
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
3+
// #Medium #Math #2022_07_14_Time_0_ms_(100.00%)_Space_39_MB_(98.65%)
74

85
public class Solution {
96
public boolean checkPowersOfThree(int n) {
10-
List<Integer> powers = new ArrayList<>();
11-
int power = 1;
12-
for (int i = 1; power <= n; i++) {
13-
powers.add(power);
14-
power = (int) Math.pow(3, i);
15-
}
16-
int i = powers.size() - 1;
17-
while (n > 0 && i >= 0) {
18-
if (n - powers.get(i) > 0) {
19-
n -= powers.get(i--);
20-
} else if (n - powers.get(i) == 0) {
21-
return true;
22-
} else {
23-
i--;
7+
while (n != 0) {
8+
int rem = n % 3;
9+
n /= 3;
10+
if (rem == 2 || n == 2) {
11+
return false;
2412
}
2513
}
26-
return n == 0;
14+
return true;
2715
}
2816
}

0 commit comments

Comments
 (0)