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
f1b0aa0
commit 123b84f
Showing
5 changed files
with
148 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_0518) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main3.cpp) | ||
add_executable(cpp_0518 ${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,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; | ||
} |
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,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; | ||
} |
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,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; | ||
} |
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 |
---|---|---|
|
@@ -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/) | | | | ||
|