Skip to content

Commit deca28f

Browse files
committed
0996 added.
1 parent 78ce43c commit deca28f

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(D main3.cpp)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/// Source : https://leetcode.com/problems/number-of-squareful-arrays/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cmath>
8+
#include <unordered_set>
9+
10+
using namespace std;
11+
12+
13+
/// Backtrack
14+
/// Using string hash to record the same value
15+
/// Time Complexity: O(n^n)
16+
/// Space Complexity: O(n)
17+
class Solution {
18+
19+
private:
20+
int n;
21+
22+
public:
23+
int numSquarefulPerms(vector<int>& A) {
24+
25+
n = A.size();
26+
vector<vector<int>> ok(n);
27+
for(int i = 0; i < n; i ++)
28+
for(int j = 0; j < n; j ++)
29+
if(j != i && perfectSquare(A[i] + A[j]))
30+
ok[i].push_back(j);
31+
32+
int res = 0;
33+
unordered_set<string> hashset;
34+
for(int i = 0; i < n; i ++){
35+
vector<bool> visited(n, false);
36+
visited[i] = true;
37+
38+
vector<int> seqindex = {i};
39+
40+
string hash = to_string(A[i]);
41+
hashset.insert(hash);
42+
43+
res += dfs(A, ok, 1, seqindex, hash, visited, hashset);
44+
}
45+
return res;
46+
}
47+
48+
private:
49+
int dfs(const vector<int>& A, const vector<vector<int>>& ok,
50+
int index, vector<int>& seqindex, const string& hash, vector<bool>& visited,
51+
unordered_set<string>& hashset){
52+
53+
if(index == n)
54+
return 1;
55+
56+
int res = 0;
57+
for(int next: ok[seqindex[index - 1]])
58+
if(!visited[next]){
59+
60+
string newhash = hash + "#" + to_string(A[next]);
61+
if(!hashset.count(newhash)){
62+
hashset.insert(newhash);
63+
64+
seqindex.push_back(next);
65+
visited[next] = true;
66+
res += dfs(A, ok, index + 1, seqindex, newhash, visited, hashset);
67+
visited[next] = false;
68+
seqindex.pop_back();
69+
}
70+
}
71+
return res;
72+
}
73+
74+
bool perfectSquare(int x){
75+
int t = sqrt(x);
76+
return t * t == x;
77+
}
78+
};
79+
80+
81+
int main() {
82+
83+
vector<int> A1 = {1, 17, 8};
84+
cout << Solution().numSquarefulPerms(A1) << endl;
85+
// 2
86+
87+
vector<int> A2 = {2, 2, 2};
88+
cout << Solution().numSquarefulPerms(A2) << endl;
89+
// 1
90+
91+
vector<int> A3(12, 0);
92+
cout << Solution().numSquarefulPerms(A3) << endl;
93+
// 1
94+
95+
return 0;
96+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/// Source : https://leetcode.com/problems/number-of-squareful-arrays/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-19
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cmath>
8+
#include <unordered_map>
9+
#include <unordered_set>
10+
11+
using namespace std;
12+
13+
14+
/// Backtrack
15+
/// Using HashMap to record the same value
16+
/// Time Complexity: O(n^n)
17+
/// Space Complexity: O(n)
18+
class Solution {
19+
20+
private:
21+
int n;
22+
23+
public:
24+
int numSquarefulPerms(vector<int>& A) {
25+
26+
n = A.size();
27+
unordered_map<int, int> freq;
28+
unordered_set<int> elements;
29+
for(int e: A){
30+
freq[e] ++;
31+
elements.insert(e);
32+
}
33+
34+
unordered_map<int, unordered_set<int>> g;
35+
for(int i = 0; i < n; i ++)
36+
for(int j = i + 1; j < n; j ++)
37+
if(perfectSquare(A[i] + A[j])){
38+
g[A[i]].insert(A[j]);
39+
g[A[j]].insert(A[i]);
40+
}
41+
42+
int res = 0;
43+
for(int e: elements){
44+
freq[e] --;
45+
res += dfs(g, e, 1, freq);
46+
freq[e] ++;
47+
}
48+
return res;
49+
}
50+
51+
private:
52+
int dfs(unordered_map<int, unordered_set<int>>& g,
53+
int e, int index, unordered_map<int, int>& freq){
54+
55+
if(index == n)
56+
return 1;
57+
58+
int res = 0;
59+
for(int next: g[e])
60+
if(freq[next]){
61+
freq[next] --;
62+
res += dfs(g, next, index + 1, freq);
63+
freq[next] ++;
64+
}
65+
return res;
66+
}
67+
68+
bool perfectSquare(int x){
69+
int t = sqrt(x);
70+
return t * t == x;
71+
}
72+
};
73+
74+
75+
int main() {
76+
77+
vector<int> A1 = {1, 17, 8};
78+
cout << Solution().numSquarefulPerms(A1) << endl;
79+
// 2
80+
81+
vector<int> A2 = {2, 2, 2};
82+
cout << Solution().numSquarefulPerms(A2) << endl;
83+
// 1
84+
85+
vector<int> A3(12, 0);
86+
cout << Solution().numSquarefulPerms(A3) << endl;
87+
// 1
88+
89+
return 0;
90+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/// Source : https://leetcode.com/problems/number-of-squareful-arrays/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-19
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cmath>
8+
#include <unordered_map>
9+
10+
using namespace std;
11+
12+
13+
/// Memory Search
14+
/// Divide corresponding factorial number to avoid repeating counting :-)
15+
/// Time Complexity: O(n*2^n)
16+
/// Space Complexity: O(n*2^n)
17+
class Solution {
18+
19+
private:
20+
int n;
21+
22+
public:
23+
int numSquarefulPerms(vector<int>& A) {
24+
25+
n = A.size();
26+
27+
vector<vector<int>> g(n);
28+
for(int i = 0; i < n; i ++)
29+
for(int j = i + 1; j < n; j ++)
30+
if(perfectSquare(A[i] + A[j])){
31+
g[i].push_back(j);
32+
g[j].push_back(i);
33+
}
34+
35+
int res = 0;
36+
vector<vector<int>> dp(n, vector<int>(1 << n, -1));
37+
for(int i = 0; i < n; i ++)
38+
res += dfs(g, i, 1 << i, dp);
39+
40+
unordered_map<int, int> freq;
41+
for(int e: A) freq[e] ++;
42+
for(const pair<int, int>& p: freq) res /= fac(p.second);
43+
return res;
44+
}
45+
46+
private:
47+
int dfs(const vector<vector<int>>& g,
48+
int index, int visited, vector<vector<int>>& dp){
49+
50+
if(visited == (1 << n) - 1)
51+
return 1;
52+
53+
if(dp[index][visited] != -1) return dp[index][visited];
54+
55+
int res = 0;
56+
for(int next: g[index])
57+
if(!(visited & (1 << next))){
58+
visited += (1 << next);
59+
res += dfs(g, next, visited, dp);
60+
visited -= (1 << next);
61+
}
62+
return dp[index][visited] = res;
63+
}
64+
65+
int fac(int x){
66+
if(x == 0 || x == 1) return 1;
67+
return x * fac(x - 1);
68+
}
69+
70+
bool perfectSquare(int x){
71+
int t = sqrt(x);
72+
return t * t == x;
73+
}
74+
};
75+
76+
77+
int main() {
78+
79+
vector<int> A1 = {1, 17, 8};
80+
cout << Solution().numSquarefulPerms(A1) << endl;
81+
// 2
82+
83+
vector<int> A2 = {2, 2, 2};
84+
cout << Solution().numSquarefulPerms(A2) << endl;
85+
// 1
86+
87+
vector<int> A3(12, 0);
88+
cout << Solution().numSquarefulPerms(A3) << endl;
89+
// 1
90+
91+
return 0;
92+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
660660
| 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [solution](https://leetcode.com/problems/cousins-in-binary-tree/solution/) | [C++](0993-Cousins-in-Binary-Tree/cpp-0993/) | | |
661661
| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [solution](https://leetcode.com/problems/rotting-oranges/solution/) | [C++](0994-Rotting-Oranges/cpp-0994/) | | |
662662
| 995 | [Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [solution](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/) | [C++](0995-Minimum-Number-of-K-Consecutive-Bit-Flips/cpp-0995/) | | |
663+
| 996 | [Number of Squareful Arrays](https://leetcode.com/problems/number-of-squareful-arrays/) | [solution](https://leetcode.com/problems/number-of-squareful-arrays/solution/) | [C++](0996-Number-of-Squareful-Arrays/cpp-0996/) | | |
663664
| | | | | | |

0 commit comments

Comments
 (0)