Skip to content

Commit

Permalink
0322 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Mar 8, 2018
1 parent 3ebfb0d commit f1b0aa0
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0322-Coin-Change/cpp-0322/CMakeLists.txt
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})
82 changes: 82 additions & 0 deletions 0322-Coin-Change/cpp-0322/main.cpp
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;
}
67 changes: 67 additions & 0 deletions 0322-Coin-Change/cpp-0322/main2.cpp
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;
}
67 changes: 67 additions & 0 deletions 0322-Coin-Change/cpp-0322/main3.cpp
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;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | |
Expand Down

0 comments on commit f1b0aa0

Please sign in to comment.