Skip to content

Commit

Permalink
0398-Random-Pick-Index/
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 8, 2018
1 parent 22f6c36 commit d13172e
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0375-Guess-Number-Higher-or-Lower-II/cpp-0375/CMakeLists.txt
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})
25 changes: 25 additions & 0 deletions 0375-Guess-Number-Higher-or-Lower-II/cpp-0375/main.cpp
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;
}
7 changes: 7 additions & 0 deletions 0398-Random-Pick-Index/cpp-0398/CMakeLists.txt
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})
53 changes: 53 additions & 0 deletions 0398-Random-Pick-Index/cpp-0398/main.cpp
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;
}
7 changes: 7 additions & 0 deletions 0767-Reorganize-String/cpp-0767/CMakeLists.txt
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})
65 changes: 65 additions & 0 deletions 0767-Reorganize-String/cpp-0767/main.cpp
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;
}
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})
9 changes: 9 additions & 0 deletions 0798-Smallest-Rotation-with-Highest-Score/cpp-0798/main.cpp
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;
}
7 changes: 7 additions & 0 deletions 0808-Soup-Servings/cpp-0808/CMakeLists.txt
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})
111 changes: 111 additions & 0 deletions 0808-Soup-Servings/cpp-0808/main.cpp
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;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | |
| | | | | | |
Expand Down

0 comments on commit d13172e

Please sign in to comment.