The trick here is to name the state correctly, then let the implementation follow. For 326. Power of Three, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isPowerOfThree.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(log3(n)), space: O(log3(n))
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 28 ms, faster than 17.10% of C++ online submissions for Power of Three.
02//Memory Usage: 8.2 MB, less than 76.19% of C++ online submissions for Power of Three.
03class Solution {
04public:
05 bool isPowerOfThree(int n) {
06 if(n == 1) return true;
07 if(n == 0) return false;
08 if(n % 3 != 0) return false;
09 return isPowerOfThree(n/3);
10 }
11};
12
13//log
14//Runtime: 16 ms, faster than 69.14% of C++ online submissions for Power of Three.
15//Memory Usage: 8.6 MB, less than 9.52% of C++ online submissions for Power of Three.
16class Solution {
17public:
18 bool isPowerOfThree(int n) {
19 if(n == 0) return false;
20 // cout << (log(n) / log(3)) << ", " << (log(n) / log(3)) - ceil((log(n) / log(3))) << endl;
21 //ceil: round up
22 //<1e-14: double precision
23 return abs((log(n) / log(3)) - ceil((log(n) / log(3)))) < 1e-14;
24 }
25};
26
27//Approach 1: Loop Iteration
28//time: O(log3(n)), space: O(1)
29//Runtime: 28 ms, faster than 17.10% of C++ online submissions for Power of Three.
30//Memory Usage: 7.2 MB, less than 100.00% of C++ online submissions for Power of Three.
31class Solution {
32public:
33 bool isPowerOfThree(int n) {
34 if(n < 1) return false;
35
36 while(n % 3 == 0){
37 n /= 3;
38 }
39
40 return n == 1;
41 }
42};
43
44//Approach 2: Base Conversion & regex
45//time: O(log3(n)), space: O(log3(n))
46//Runtime: 188 ms, faster than 5.75% of C++ online submissions for Power of Three.
47//Memory Usage: 85.7 MB, less than 9.52% of C++ online submissions for Power of Three.
48class Solution {
49public:
50 bool isPowerOfThree(int n) {
51 // char buffer[32];
52 // itoa(n, buffer, 3);
53 // string str = string(buffer);
54
55 string str = "";
56 while(n){
57 // str = to_string(n%3) + str;
58 str.insert(0, to_string(n%3));
59 n /= 3;
60 }
61
62 regex pattern("^10*$");
63
64 return regex_match(str, pattern);
65 }
66};
67
68//Approach 3: Mathematics
69//time: depends on log's implementation, space: O(1)
70//Runtime: 16 ms, faster than 69.14% of C++ online submissions for Power of Three.
71//Memory Usage: 7.6 MB, less than 100.00% of C++ online submissions for Power of Three.
72class Solution {
73public:
74 bool isPowerOfThree(int n) {
75 // cout << n << ", " << round(log(n)/log(3)) << ", " << abs(round(log(n)/log(3)) - log(n)/log(3)) << ", " << std::numeric_limits<double>::epsilon() << endl;
76 return abs(round(log(n)/log(3)) - log(n)/log(3)) < std::numeric_limits<double>::epsilon()*20;
77 }
78};
79
80//Approach 4: Integer Limitations
81//time: O(1), space: O(1)
82//Runtime: 12 ms, faster than 86.75% of C++ online submissions for Power of Three.
83//Memory Usage: 7.1 MB, less than 100.00% of C++ online submissions for Power of Three.
84class Solution {
85public:
86 bool isPowerOfThree(int n) {
87 int max_power_of_3 = pow(3, int(log(INT_MAX)/log(3)));
88 //if n can divide max_power_of_3, it's power of 3
89 return n > 0 && max_power_of_3 % n == 0;
90 }
91};
Cost