Skip to content

Commit

Permalink
0518 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Mar 9, 2018
1 parent f1b0aa0 commit 123b84f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0518-Coin-Change-2/cpp-0518/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0518)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main3.cpp)
add_executable(cpp_0518 ${SOURCE_FILES})
56 changes: 56 additions & 0 deletions 0518-Coin-Change-2/cpp-0518/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// Source : https://leetcode.com/problems/domino-and-tromino-tiling/description/
/// Author : liuyubobobo
/// Time : 2018-03-08

#include <iostream>
#include <vector>

using namespace std;


/// Memory Search
///
/// Time Complexity: O(len(coins)*amount)
/// Space Complexity: O(len(coins)*amount)
class Solution {

private:
vector<vector<int>> dp;

public:
int change(int amount, vector<int>& coins) {

dp = vector<vector<int>>(coins.size(), vector<int>(amount + 1, -1));
int res = search(coins, 0, amount);
return res == -1 ? 0 : res;
}

private:
int search(const vector<int>& coins, int index, int amount){

if(index == coins.size())
return amount == 0 ? 1 : 0;

if(dp[index][amount] != -1)
return dp[index][amount];

int res = 0;
for(int i = 0 ; i * coins[index] <= amount ; i ++)
res += search(coins, index + 1, amount - i * coins[index]);
return dp[index][amount] = res;
}
};

int main() {

vector<int> coins1 = {1, 2, 5};
cout << Solution().change(5, coins1) << endl;

vector<int> coins2 = {2};
cout << Solution().change(3, coins2) << endl;

vector<int> coins3 = {10};
cout << Solution().change(10, coins3) << endl;

return 0;
}
42 changes: 42 additions & 0 deletions 0518-Coin-Change-2/cpp-0518/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Source : https://leetcode.com/problems/domino-and-tromino-tiling/description/
/// Author : liuyubobobo
/// Time : 2018-03-08

#include <iostream>
#include <vector>

using namespace std;


/// Dynamic Programming
///
/// Time Complexity: O(len(coins)*amount*log(amount))
/// Space Complexity: O(len(coins)*amount)
class Solution {

public:
int change(int amount, vector<int>& coins) {

vector<vector<int>> dp(coins.size() + 1, vector<int>(amount + 1, 0));
dp[coins.size()][0] = 1;
for(int i = coins.size() - 1; i >= 0 ; i --)
for(int j = 0; j <= amount; j ++)
for(int k = 0 ; k * coins[i] <= j ; k ++)
dp[i][j] += dp[i + 1][j - k * coins[i]];
return dp[0][amount];
}
};

int main() {

vector<int> coins1 = {1, 2, 5};
cout << Solution().change(5, coins1) << endl;

vector<int> coins2 = {2};
cout << Solution().change(3, coins2) << endl;

vector<int> coins3 = {10};
cout << Solution().change(10, coins3) << endl;

return 0;
}
41 changes: 41 additions & 0 deletions 0518-Coin-Change-2/cpp-0518/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Source : https://leetcode.com/problems/domino-and-tromino-tiling/description/
/// Author : liuyubobobo
/// Time : 2018-03-08

#include <iostream>
#include <vector>

using namespace std;


/// Dynamic Programming
///
/// Time Complexity: O(len(coins)*amount)
/// Space Complexity: O(amount)
class Solution {

public:
int change(int amount, vector<int>& coins) {

vector<int> dp(amount + 1, 0);
dp[0] = 1;
for(int i = coins.size() - 1; i >= 0 ; i --)
for(int j = coins[i]; j <= amount; j ++)
dp[j] += dp[j - coins[i]];
return dp[amount];
}
};

int main() {

vector<int> coins1 = {1, 2, 5};
cout << Solution().change(5, coins1) << endl;

vector<int> coins2 = {2};
cout << Solution().change(3, coins2) << endl;

vector<int> coins3 = {10};
cout << Solution().change(10, coins3) << endl;

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ email: [[email protected]](mailto:[email protected])
| 454 | [4Sum II](https://leetcode.com/problems/4sum-ii/description/) | [] | [C++](0454-4Sum-II/cpp-0454/) | [Java](0454-4Sum-II/java-0454/src/) | |
| 455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies/description/) | [] | [C++](0455-Assign-Cookies/cpp-0455/) | [Java](0455-Assign-Cookies/java-0455/src/) | |
| | | | | | |
| 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/description/) | [] | [C++](0518-Coin-Change-2/cpp-0518/) | | |
| | | | | | |
| 583 | [Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/description/) | [solution](https://leetcode.com/problems/delete-operation-for-two-strings/solution/) | [C++](0583-Delete-Operation-for-Two-Strings/cpp-0583/) | | |
| | | | | | |
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii/description/) | | [C++](0598-Range-Addition-II/cpp-0598/) | | |
Expand Down

0 comments on commit 123b84f

Please sign in to comment.