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
22f6c36
commit d13172e
Showing
11 changed files
with
300 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_0375) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0375 ${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,25 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
|
||
public: | ||
int getMoneyAmount(int n) { | ||
|
||
int res = 0; | ||
for(int pick = 1 ; pick <= n ; pick ++) | ||
res = max(res, solve(n, pick)); | ||
return res; | ||
} | ||
|
||
private: | ||
int solve(int n, int pick){ | ||
|
||
} | ||
}; | ||
|
||
int main() { | ||
|
||
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,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0398) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0398 ${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,53 @@ | ||
/// Source : https://leetcode.com/problems/random-pick-index/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-07-08 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Reservor Sampling | ||
/// The detail of Resevoir Sampling can be seen here: | ||
/// https://www.geeksforgeeks.org/reservoir-sampling/ | ||
/// | ||
/// Attention: Don't use srand in the random problem in Leetcode | ||
/// Since it may lead to WA | ||
/// I think the test code use its own random seed to verify your submitted code | ||
/// | ||
/// Time Complexity: init: O(n) | ||
/// pick: O(n) | ||
/// Space Complexity: O(1) | ||
class Solution { | ||
|
||
private: | ||
vector<int> nums; | ||
|
||
public: | ||
Solution(vector<int> nums) { | ||
|
||
for(int num: nums) | ||
this->nums.push_back(num); | ||
} | ||
|
||
int pick(int target) { | ||
|
||
int res = -1; | ||
int cnt = 0; | ||
for(int i = 0 ; i < nums.size() ; i ++) | ||
if(nums[i] == target){ | ||
cnt ++; | ||
int rnd = rand() % cnt; | ||
if(rnd == 0) | ||
res = i; | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
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,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(B) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(B ${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,65 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
string reorganizeString(string S) { | ||
|
||
vector<int> freq(26, 0); | ||
for(char c: S) | ||
freq[c-'a'] ++; | ||
|
||
vector<pair<int, char>> vec; | ||
for(int i = 0 ; i < 26 ; i ++) | ||
if(freq[i]) | ||
vec.push_back(make_pair(freq[i], 'a' + i)); | ||
|
||
sort(vec.begin(), vec.end()); | ||
//printPairVec(vec); | ||
|
||
vector<string> parts(vec.back().first, ""); | ||
for(int i = 0 ; i < parts.size() ; i ++) | ||
parts[i] += vec.back().second; | ||
//printStringVec(parts); | ||
|
||
int index = 0; | ||
for(int i = vec.size() - 2 ; i >= 0 ; i --) | ||
for(int j = 0 ; j < vec[i].first; j ++){ | ||
parts[index] += vec[i].second; | ||
index = (index + 1) % parts.size(); | ||
} | ||
|
||
string res = ""; | ||
for(int i = 0 ; i < parts.size(); i ++) | ||
if(res == "" || res[res.size()-1] != parts[i][0]) | ||
res += parts[i]; | ||
else | ||
return ""; | ||
|
||
return res; | ||
} | ||
|
||
private: | ||
void printPairVec(const vector<pair<int, char>>& vec){ | ||
for(pair<int, char> e: vec) | ||
cout << e.first << " " << e.second << endl; | ||
} | ||
|
||
void printStringVec(const vector<string>& vec){ | ||
for(string e: vec) | ||
cout << e << endl; | ||
} | ||
}; | ||
|
||
int main() { | ||
|
||
cout << Solution().reorganizeString("aab") << endl; | ||
cout << Solution().reorganizeString("aaab") << endl; | ||
cout << Solution().reorganizeString("vlvov") << endl; | ||
|
||
return 0; | ||
} |
7 changes: 7 additions & 0 deletions
7
0798-Smallest-Rotation-with-Highest-Score/cpp-0798/CMakeLists.txt
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_0798) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0798 ${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,9 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
|
||
int main() { | ||
cout << "Hello, World!" << 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,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0808) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0808 ${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,111 @@ | ||
/// Source : https://leetcode.com/problems/soup-servings/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-03-31 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
using namespace std; | ||
|
||
/// Dynamic Programming | ||
/// | ||
/// 1. N should be round up based on 25, suppose that's n; | ||
/// 2. We can consifer the 4 types of serving as (4, 0), (3, 1), (2, 2), (1, 3) | ||
/// Then we remove 1 from ever pair, and become (3, 0), (2, 1), (1, 2), (0, 3) | ||
/// We can see that after n/2 serving, | ||
/// it's only (1/2)^(n/2) probability that B is empty | ||
/// because every time, 1 unit of soup will be served from A | ||
/// 3. We can see that for n is large, there's really low probability that B will be empty, | ||
/// which means there's a low probability that A and B are both empty | ||
/// 4. For how large n, we can safely say the result is 1? When (1/2)^n < 10^(-6) | ||
/// 5. For the smaller n, we use dp to solve the problem. | ||
/// | ||
/// Using Memory Search | ||
/// | ||
/// Time Complexity: O(1) (Compare to N) | ||
/// Space Complexity: O(1) (Compare to N) | ||
class Solution { | ||
|
||
private: | ||
vector<vector<double>> memo1, memo2; | ||
public: | ||
double soupServings(int N) { | ||
|
||
int n_limit = 50; | ||
|
||
int n = N / 25 + (N % 25 == 0 ? 0 : 1); | ||
if(n >= n_limit) | ||
return 1.0; | ||
|
||
return solve(n); | ||
} | ||
|
||
private: | ||
double solve(int n){ | ||
|
||
memo1 = vector<vector<double>>(n + 1, vector<double>(n + 1, -1.0)); | ||
double res1 = solve1(n, n); | ||
// cout << "res1 = " << res1 << endl; | ||
|
||
memo2 = vector<vector<double>>(n + 1, vector<double>(n + 1, -1.0)); | ||
double res2 = solve2(n, n); | ||
// cout << "res2 = " << res2 << endl; | ||
|
||
return res1 + 0.5 * res2; | ||
} | ||
|
||
double solve1(int x, int y){ | ||
|
||
if(x <= 0 && y > 0) | ||
return 1.0; | ||
|
||
if(y <= 0) | ||
return 0.0; | ||
|
||
if(memo1[x][y] >= 0) | ||
return memo1[x][y]; | ||
|
||
double res = 0.0; | ||
res += 0.25 * solve1(x - 4, y); | ||
res += 0.25 * solve1(x - 3, y - 1); | ||
res += 0.25 * solve1(x - 2, y - 2); | ||
res += 0.25 * solve1(x - 1, y - 3); | ||
return memo1[x][y] = res; | ||
} | ||
|
||
double solve2(int x, int y){ | ||
|
||
if(x <= 0 && y <= 0) | ||
return 1.0; | ||
|
||
if(x <= 0 || y <= 0) | ||
return 0.0; | ||
|
||
if(memo2[x][y] >= 0) | ||
return memo2[x][y]; | ||
|
||
double res = 0.0; | ||
res += 0.25 * solve2(x - 4, y); | ||
res += 0.25 * solve2(x - 3, y - 1); | ||
res += 0.25 * solve2(x - 2, y - 2); | ||
res += 0.25 * solve2(x - 1, y - 3); | ||
return memo2[x][y] = res; | ||
} | ||
|
||
int get_n_limit(){ | ||
double prob = 1.0; | ||
for(int n = 1; ;n ++){ | ||
prob *= 0.5; | ||
if(prob < 0.000001) | ||
return n; | ||
} | ||
assert(false); | ||
return -1; | ||
} | ||
}; | ||
|
||
int main() { | ||
cout << Solution().soupServings(50) << 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 |
---|---|---|
|
@@ -231,6 +231,8 @@ email: [[email protected]](mailto:[email protected]) | |
| 390 | [Elimination Game](https://leetcode.com/problems/elimination-game/description/) | | [C++](0390-Elimination-Game/cpp-0390/) | | | | ||
| 391 | [Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/description/) | [缺:矩形求交] | [C++](0391-Perfect-Rectangle/cpp-0391/) | | | | ||
| | | | | | | | ||
| 398 | [Random Pick Index](https://leetcode.com/problems/random-pick-index/description/) | [无] | [C++](0398-Random-Pick-Index/cpp-0398/) | | | | ||
| | | | | | | | ||
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | [无] | [C++](0416-Partition-Equal-Subset-Sum/cpp-0416/) | [Java](0416-Partition-Equal-Subset-Sum/java-0416/src/) | | | ||
| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [无] | | [Java](0417-Pacific-Atlantic-Water-Flow/java-0417/src/) | | | ||
| | | | | | | | ||
|