I like to read this solution as a small machine: keep the useful information, throw away the noise. For 869. Reordered Power of 2, the solution in this repository is mainly a backtracking solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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: backtracking.
The notes already sitting in the source point us in the right direction:
- Reference to 47. Permutations II.cpp
- https://github.com/keineahnung2345/leetcode-cpp-practices/blob/master/47.%20Permutations%20II.cpp
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isPowerOf2, backtrack, reorderedPowerOf2, used, swap.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Reference to 47. Permutations II.cpp
02//https://github.com/keineahnung2345/leetcode-cpp-practices/blob/master/47.%20Permutations%20II.cpp
03//Runtime: 288 ms, faster than 8.14% of C++ online submissions for Reordered Power of 2.
04//Memory Usage: 7.8 MB, less than 100.00% of C++ online submissions for Reordered Power of 2.
05class Solution {
06public:
07 bool isPowerOf2(vector<int>& perm){
08 if(perm[0] == 0) return false;
09
10 int N = 0;
11 for(int i = 0; i < perm.size(); i++){
12 N = N * 10 + perm[i];
13 }
14
15 // if((int)log2(N) == log2(N)){
16 // for(int i = 0; i < perm.size(); i++){
17 // cout << perm[i];
18 // }
19 // cout << endl;
20 // cout << N << endl;
21 // }
22
23 return (int)log2(N) == log2(N);
24 };
25
26 bool backtrack(vector<int>& perm, vector<int>& nums, vector<bool>& used){
27 if(perm.size() == nums.size()){
28 // for(int i = 0; i < perm.size(); i++){
29 // cout << perm[i];
30 // }
31 // cout << endl;
32 return isPowerOf2(perm);
33 }else{
34 for(int i = 0; i < nums.size(); i++){
35 if(used[i]) continue;
36 if(i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue;
37 perm.push_back(nums[i]);
38 used[i] = true;
39 if(backtrack(perm, nums, used)) return true;
40 perm.pop_back();
41 used[i] = false;
42 }
43 }
44
45 return false;
46 };
47
48 bool reorderedPowerOf2(int N) {
49 vector<int> nums;
50 while(N){
51 nums.insert(nums.begin(), N%10);
52 N /= 10;
53 }
54
55 sort(nums.begin(), nums.end());
56
57 vector<bool> used(nums.size(), false);
58 vector<int> perm;
59
60 return backtrack(perm, nums, used);
61 }
62};
63
64//Approach 1: Permutations
65//Runtime: 356 ms, faster than 6.98% of C++ online submissions for Reordered Power of 2.
66//Memory Usage: 7.3 MB, less than 100.00% of C++ online submissions for Reordered Power of 2.
67//time: O(log(N)! * log(N)), space: O(log(N))
68class Solution {
69public:
70 void swap(vector<int>& nums, int i, int j){
71 int t = nums[i];
72 nums[i] = nums[j];
73 nums[j] = t;
74 };
75
76 bool isPowerOf2(vector<int>& nums){
77 if(nums[0] == 0) return false;
78
79 int N = 0;
80 for(int e : nums){
81 N = N * 10 + e;
82 }
83
84 //the parenthesis around "N & 1" is required here!!
85 while(N > 0 && ((N & 1) == 0)){
86 N >>= 1;
87 }
88
89 return N == 1;
90 }
91
92 bool permutations(vector<int>& nums, int start){
93 if(start == nums.size()){
94 return isPowerOf2(nums);
95 }
96
97 for(int i = start; i < nums.size(); i++){
98 swap(nums, start, i);
99
100 if(permutations(nums, start+1))
101 return true;
102
103 swap(nums, start, i);
104 }
105
106 return false;
107 };
108
109 bool reorderedPowerOf2(int N) {
110 vector<int> nums;
111 while(N){
112 nums.push_back(N%10);
113 N /= 10;
114 }
115
116 return permutations(nums, 0);
117 }
118};
119
120//Approach 2: Counting
121//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reordered Power of 2.
122//Memory Usage: 7.8 MB, less than 100.00% of C++ online submissions for Reordered Power of 2.
123//time: O((logN)^2), space: O(logN)
124class Solution {
125public:
126 vector<int> count(int N){
127 vector<int> ans(10);
128 while(N){
129 ans[N%10]++;
130 N /= 10;
131 }
132 return ans;
133 };
134
135 bool reorderedPowerOf2(int N) {
136 vector<int> arr = count(N);
137 //pow(2,30) is 1073741824
138 //and N's upper bound is pow(10,9)
139 for(int i = 0; i < 31; i++){
140 if(arr == count(1 << i)){
141 return true;
142 }
143 }
144 return false;
145 }
146};
Cost