forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ebfb0d
commit f1b0aa0
Showing
5 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0322_new) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main3.cpp) | ||
add_executable(cpp_0322_new ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/// Source : https://leetcode.com/problems/coin-change/solution/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-03-08 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
/// Memory Search | ||
/// | ||
/// Time Complexity: O(coins_size * amount) | ||
/// Space Complexity: O(amount) | ||
class Solution { | ||
|
||
private: | ||
vector<int> dp; | ||
int max_amount; | ||
|
||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
max_amount = amount + 1; | ||
dp = vector<int>(amount+1, -1); | ||
int res = search(coins, amount); | ||
return res == max_amount ? -1 : res; | ||
} | ||
|
||
private: | ||
int search(const vector<int>& coins, int amount){ | ||
|
||
if(amount == 0) | ||
return 0; | ||
|
||
if(dp[amount] != -1) | ||
return dp[amount]; | ||
|
||
int res = max_amount; | ||
for(int coin: coins) | ||
if(amount - coin >= 0) | ||
res = min(res, 1 + search(coins, amount -coin)); | ||
return dp[amount] = res; | ||
} | ||
}; | ||
|
||
int main() { | ||
|
||
vector<int> coins1 = {1, 2, 5}; | ||
int amount1 = 11; | ||
cout<< Solution().coinChange(coins1, amount1) << endl; | ||
// 3 | ||
|
||
// --- | ||
|
||
vector<int> coins2 = {2}; | ||
int amount2 = 1; | ||
cout << Solution().coinChange(coins2, amount2) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins3 = {2}; | ||
int amount3 = 3; | ||
cout << Solution().coinChange(coins3, amount3) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins4 = {2, 5, 10, 1}; | ||
int amount4 = 27; | ||
cout << Solution().coinChange(coins4, amount4) << endl; | ||
// 4 | ||
|
||
// --- | ||
|
||
vector<int> coins5 = {186, 419, 83, 408}; | ||
int amount5 = 6249; | ||
cout << Solution().coinChange(coins5, amount5) << endl; | ||
// 20 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// Source : https://leetcode.com/problems/coin-change/solution/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-03-08 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
/// Dynamic Problem | ||
/// 0-1 backpack problem | ||
/// | ||
/// Time Complexity: O(coins_size * amount) | ||
/// Space Complexity: O(amount) | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
|
||
vector<int> dp(amount+1, amount + 1); | ||
dp[0] = 0; | ||
|
||
for(int coin: coins) | ||
for( int j = coin ; j <= amount ; j ++ ) | ||
if( dp[j - coin] != -1 ) | ||
dp[j] = min( dp[j], dp[j-coin] + 1); | ||
|
||
return dp[amount] == amount + 1 ? -1 : dp[amount]; | ||
} | ||
}; | ||
|
||
int main() { | ||
|
||
vector<int> coins1 = {1, 2, 5}; | ||
int amount1 = 11; | ||
cout<< Solution().coinChange(coins1, amount1) << endl; | ||
// 3 | ||
|
||
// --- | ||
|
||
vector<int> coins2 = {2}; | ||
int amount2 = 1; | ||
cout << Solution().coinChange(coins2, amount2) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins3 = {2}; | ||
int amount3 = 3; | ||
cout << Solution().coinChange(coins3, amount3) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins4 = {2, 5, 10, 1}; | ||
int amount4 = 27; | ||
cout << Solution().coinChange(coins4, amount4) << endl; | ||
// 4 | ||
|
||
// --- | ||
|
||
vector<int> coins5 = {186, 419, 83, 408}; | ||
int amount5 = 6249; | ||
cout << Solution().coinChange(coins5, amount5) << endl; | ||
// 20 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// Source : https://leetcode.com/problems/coin-change/solution/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-03-08 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
/// Dynamic Problem | ||
/// 0-1 backpack problem | ||
/// | ||
/// Time Complexity: O(coins_size * amount) | ||
/// Space Complexity: O(amount) | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
|
||
vector<int> dp(amount + 1, amount + 1); | ||
dp[0] = 0; | ||
|
||
for(int i = 1 ; i <= amount ; i ++) | ||
for(int coin: coins) | ||
if(i - coin >= 0) | ||
dp[i] = min(dp[i], dp[i-coin] + 1); | ||
|
||
return dp[amount] == amount + 1 ? -1 : dp[amount]; | ||
} | ||
}; | ||
|
||
int main() { | ||
|
||
vector<int> coins1 = {1, 2, 5}; | ||
int amount1 = 11; | ||
cout<< Solution().coinChange(coins1, amount1) << endl; | ||
// 3 | ||
|
||
// --- | ||
|
||
vector<int> coins2 = {2}; | ||
int amount2 = 1; | ||
cout << Solution().coinChange(coins2, amount2) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins3 = {2}; | ||
int amount3 = 3; | ||
cout << Solution().coinChange(coins3, amount3) << endl; | ||
// -1 | ||
|
||
// --- | ||
|
||
vector<int> coins4 = {2, 5, 10, 1}; | ||
int amount4 = 27; | ||
cout << Solution().coinChange(coins4, amount4) << endl; | ||
// 4 | ||
|
||
// --- | ||
|
||
vector<int> coins5 = {186, 419, 83, 408}; | ||
int amount5 = 6249; | ||
cout << Solution().coinChange(coins5, amount5) << endl; | ||
// 20 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,8 @@ email: [[email protected]](mailto:[email protected]) | |
| | | | | | | | ||
| 319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/description/) | [无] | [C++](0319-Bulb-Switcher/cpp-0319/) | | | | ||
| | | | | | | | ||
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | [solution](https://leetcode.com/problems/coin-change/solution/) | [C++](0322-Coin-Change/cpp-0322/) | | | | ||
| | | | | | | | ||
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/description/) | [无] | [C++](0337-House-Robber-III/cpp-0337/) | | | | ||
| | | | | | | | ||
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/description/) | [无] | [C++](0343-Integer-Break/cpp-0343/) | [Java](0343-Integer-Break/java-0343/src/) | | | ||
|