Skip to content

Commit e6726c8

Browse files
committed
Add 279 code
1 parent 09a97c1 commit e6726c8

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0279)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main3.cpp)
7+
add_executable(cpp_0279 ${SOURCE_FILES})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <queue>
8+
#include <stdexcept>
9+
10+
using namespace std;
11+
12+
/// BFS
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
int numSquares(int n) {
18+
19+
if(n == 0)
20+
return 0;
21+
22+
queue<pair<int, int>> q;
23+
q.push(make_pair(n, 0));
24+
25+
vector<bool> visited(n + 1, false);
26+
visited[n] = true;
27+
28+
while(!q.empty()){
29+
int num = q.front().first;
30+
int step = q.front().second;
31+
q.pop();
32+
33+
for(int i = 1; num - i * i >= 0; i ++){
34+
int a = num - i * i;
35+
if(!visited[a]){
36+
if(a == 0) return step + 1;
37+
q.push(make_pair(a, step + 1));
38+
visited[a] = true;
39+
}
40+
}
41+
}
42+
43+
throw invalid_argument("No Solution.");
44+
}
45+
};
46+
47+
int main() {
48+
49+
cout << Solution().numSquares(12) << endl;
50+
cout << Solution().numSquares(13) << endl;
51+
52+
return 0;
53+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
/// Memory Search
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(n)
13+
class Solution {
14+
15+
public:
16+
int numSquares(int n) {
17+
18+
vector<int> mem(n + 1, -1);
19+
20+
return numSquares(n, mem);
21+
}
22+
23+
private:
24+
int numSquares(int n, vector<int>& mem){
25+
26+
if(n == 0)
27+
return 0;
28+
29+
if(mem[n] != -1)
30+
return mem[n];
31+
32+
int res = INT_MAX;
33+
for(int i = 1; n - i * i >= 0; i ++ )
34+
res = min(res, 1 + numSquares(n - i * i, mem));
35+
return mem[n] = res;
36+
}
37+
};
38+
39+
int main() {
40+
41+
cout << Solution().numSquares(12) << endl;
42+
cout << Solution().numSquares(13) << endl;
43+
44+
return 0;
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
/// Dynamic Programming
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(n)
13+
class Solution {
14+
15+
public:
16+
int numSquares(int n) {
17+
18+
vector<int> mem(n + 1, INT_MAX);
19+
mem[0] = 0;
20+
for(int i = 1; i <= n ; i ++)
21+
for(int j = 1 ; i - j * j >= 0 ; j ++)
22+
mem[i] = min(mem[i], 1 + mem[i - j * j]);
23+
24+
return mem[n];
25+
}
26+
};
27+
28+
int main() {
29+
30+
cout << Solution().numSquares(12) << endl;
31+
cout << Solution().numSquares(13) << endl;
32+
33+
return 0;
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
import java.util.LinkedList;
6+
import javafx.util.Pair;
7+
8+
/// BFS
9+
/// Time Complexity: O(n)
10+
/// Space Complexity: O(n)
11+
public class Solution1 {
12+
13+
public int numSquares(int n) {
14+
15+
if(n == 0)
16+
return 0;
17+
18+
LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>();
19+
queue.addLast(new Pair<Integer, Integer>(n, 0));
20+
21+
boolean[] visited = new boolean[n+1];
22+
visited[n] = true;
23+
24+
while(!queue.isEmpty()){
25+
Pair<Integer, Integer> front = queue.removeFirst();
26+
int num = front.getKey();
27+
int step = front.getValue();
28+
29+
if(num == 0)
30+
return step;
31+
32+
for(int i = 1 ; num - i*i >= 0 ; i ++){
33+
int a = num - i*i;
34+
if(!visited[a]){
35+
if(a == 0) return step + 1;
36+
queue.addLast(new Pair(num - i * i, step + 1));
37+
visited[num - i * i] = true;
38+
}
39+
}
40+
}
41+
42+
throw new IllegalStateException("No Solution.");
43+
}
44+
45+
public static void main(String[] args) {
46+
47+
System.out.println((new Solution1()).numSquares(12));
48+
System.out.println((new Solution1()).numSquares(13));
49+
}
50+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
import java.util.Arrays;
6+
7+
/// Memory Search
8+
/// Time Complexity: O(n)
9+
/// Space Complexity: O(n)
10+
public class Solution2 {
11+
12+
public int numSquares(int n) {
13+
14+
int[] mem = new int[n+1];
15+
Arrays.fill(mem, -1);
16+
17+
return numSquares(n, mem);
18+
}
19+
20+
private int numSquares(int n, int[] mem){
21+
22+
if(n == 0)
23+
return 0;
24+
25+
if(mem[n] != -1)
26+
return mem[n];
27+
28+
int res = Integer.MAX_VALUE;
29+
for(int i = 1; n - i * i >= 0; i ++ )
30+
res = Math.min(res, 1 + numSquares(n - i * i, mem));
31+
return mem[n] = res;
32+
}
33+
34+
public static void main(String[] args) {
35+
36+
System.out.println((new Solution2()).numSquares(12));
37+
System.out.println((new Solution2()).numSquares(13));
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// Source : https://leetcode.com/problems/perfect-squares/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-11-17
4+
5+
import java.util.Arrays;
6+
7+
/// Dynamic Programming
8+
/// Time Complexity: O(n)
9+
/// Space Complexity: O(n)
10+
public class Solution3 {
11+
12+
public int numSquares(int n) {
13+
14+
int[] mem = new int[n+1];
15+
Arrays.fill(mem, Integer.MAX_VALUE);
16+
mem[0] = 0;
17+
for(int i = 1; i <= n ; i ++)
18+
for(int j = 1 ; i - j * j >= 0 ; j ++)
19+
mem[i] = Math.min(mem[i], 1 + mem[i - j * j]);
20+
21+
return mem[n];
22+
}
23+
24+
public static void main(String[] args) {
25+
26+
System.out.println((new Solution3()).numSquares(12));
27+
System.out.println((new Solution3()).numSquares(13));
28+
}
29+
}

0 commit comments

Comments
 (0)