Skip to content

Commit ed7179f

Browse files
committed
add daily exercise
1 parent 6622bc8 commit ed7179f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:math';
2+
3+
/// https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/description
4+
/// Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
5+
/// An integer y is a power of three if there exists an integer x such that y == 3x.
6+
///
7+
/// Example 1:
8+
///
9+
/// Input: n = 12
10+
/// Output: true
11+
/// Explanation: 12 = 31 + 32
12+
/// Example 2:
13+
///
14+
/// Input: n = 91
15+
/// Output: true
16+
/// Explanation: 91 = 30 + 32 + 34
17+
/// Example 3:
18+
///
19+
/// Input: n = 21
20+
/// Output: false
21+
class Daily20250304 {
22+
bool checkPowersOfThree(int n) {
23+
while (n > 1){
24+
if (n % 3 == 2){
25+
return false;
26+
}
27+
n = (n/3).round();
28+
}
29+
return true;
30+
}
31+
}

test/leetcode_daily_test.dart

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:leetcode/exercises/add_two_numbers.dart';
22
import 'package:leetcode/exercises/daily/2025/03/daily20250301.dart';
33
import 'package:leetcode/exercises/daily/2025/03/daily20250302.dart';
44
import 'package:leetcode/exercises/daily/2025/03/daily20250303.dart';
5+
import 'package:leetcode/exercises/daily/2025/03/daily20250304.dart';
56
import 'package:leetcode/models/list_node.dart';
67
import 'package:test/test.dart';
78

@@ -27,4 +28,11 @@ void dailyExercises() {
2728
expect(daily.pivotArray([9,12,5,10,14,3,10], 10), [9,5,3,10,10,12,14]);
2829
expect(daily.pivotArray([-3,4,3,2], 2), [-3,2,4,3]);
2930
});
31+
test('Daily 2025-03-04', (){
32+
final daily = Daily20250304();
33+
expect(daily.checkPowersOfThree(12) , true);
34+
expect(daily.checkPowersOfThree(91), true);
35+
expect(daily.checkPowersOfThree(21), false);
36+
});
37+
3038
}

0 commit comments

Comments
 (0)