Add 279 code

This commit is contained in:
MisterBigbooo 2019-01-20 22:01:46 +08:00
parent 09a97c1fad
commit e6726c8479
7 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0279)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main3.cpp)
add_executable(cpp_0279 ${SOURCE_FILES})

View File

@ -0,0 +1,53 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <queue>
#include <stdexcept>
using namespace std;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
if(n == 0)
return 0;
queue<pair<int, int>> q;
q.push(make_pair(n, 0));
vector<bool> visited(n + 1, false);
visited[n] = true;
while(!q.empty()){
int num = q.front().first;
int step = q.front().second;
q.pop();
for(int i = 1; num - i * i >= 0; i ++){
int a = num - i * i;
if(!visited[a]){
if(a == 0) return step + 1;
q.push(make_pair(a, step + 1));
visited[a] = true;
}
}
}
throw invalid_argument("No Solution.");
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}

View File

@ -0,0 +1,45 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
vector<int> mem(n + 1, -1);
return numSquares(n, mem);
}
private:
int numSquares(int n, vector<int>& mem){
if(n == 0)
return 0;
if(mem[n] != -1)
return mem[n];
int res = INT_MAX;
for(int i = 1; n - i * i >= 0; i ++ )
res = min(res, 1 + numSquares(n - i * i, mem));
return mem[n] = res;
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}

View File

@ -0,0 +1,34 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
vector<int> mem(n + 1, INT_MAX);
mem[0] = 0;
for(int i = 1; i <= n ; i ++)
for(int j = 1 ; i - j * j >= 0 ; j ++)
mem[i] = min(mem[i], 1 + mem[i - j * j]);
return mem[n];
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}

View File

@ -0,0 +1,50 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.LinkedList;
import javafx.util.Pair;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution1 {
public int numSquares(int n) {
if(n == 0)
return 0;
LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>();
queue.addLast(new Pair<Integer, Integer>(n, 0));
boolean[] visited = new boolean[n+1];
visited[n] = true;
while(!queue.isEmpty()){
Pair<Integer, Integer> front = queue.removeFirst();
int num = front.getKey();
int step = front.getValue();
if(num == 0)
return step;
for(int i = 1 ; num - i*i >= 0 ; i ++){
int a = num - i*i;
if(!visited[a]){
if(a == 0) return step + 1;
queue.addLast(new Pair(num - i * i, step + 1));
visited[num - i * i] = true;
}
}
}
throw new IllegalStateException("No Solution.");
}
public static void main(String[] args) {
System.out.println((new Solution1()).numSquares(12));
System.out.println((new Solution1()).numSquares(13));
}
}

View File

@ -0,0 +1,39 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.Arrays;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution2 {
public int numSquares(int n) {
int[] mem = new int[n+1];
Arrays.fill(mem, -1);
return numSquares(n, mem);
}
private int numSquares(int n, int[] mem){
if(n == 0)
return 0;
if(mem[n] != -1)
return mem[n];
int res = Integer.MAX_VALUE;
for(int i = 1; n - i * i >= 0; i ++ )
res = Math.min(res, 1 + numSquares(n - i * i, mem));
return mem[n] = res;
}
public static void main(String[] args) {
System.out.println((new Solution2()).numSquares(12));
System.out.println((new Solution2()).numSquares(13));
}
}

View File

@ -0,0 +1,29 @@
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.Arrays;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution3 {
public int numSquares(int n) {
int[] mem = new int[n+1];
Arrays.fill(mem, Integer.MAX_VALUE);
mem[0] = 0;
for(int i = 1; i <= n ; i ++)
for(int j = 1 ; i - j * j >= 0 ; j ++)
mem[i] = Math.min(mem[i], 1 + mem[i - j * j]);
return mem[n];
}
public static void main(String[] args) {
System.out.println((new Solution3()).numSquares(12));
System.out.println((new Solution3()).numSquares(13));
}
}