Skip to content

Commit 4a6a7fd

Browse files
2 parents 8056406 + 37cc35e commit 4a6a7fd

File tree

9 files changed

+383
-1
lines changed

9 files changed

+383
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution1 {
2+
public:
3+
bool isSubsequence(string s, string t) {
4+
int i=0, j=0;
5+
while(i<s.size() && j<t.size()){
6+
if(t[j] == s[i]) ++j, ++i;
7+
else ++j;
8+
}
9+
if(i == s.size()) return true;
10+
else return false;
11+
}
12+
};
13+
14+
class Solution2 {
15+
public:
16+
bool isSubsequence(string s, string t) {
17+
int n = s.length();
18+
int m = t.length();
19+
vector<vector<bool>> dp(n+1, vector<bool>(m+1, false));
20+
for (int i = 0; i <= m; i++) dp[0][i] = true;
21+
for (int i = 1; i <= n; i++)
22+
for (int j = 1; j <= m; j++){
23+
if (s[i-1] == t[j-1]) dp[i][j] = dp[i][j] or dp[i-1][j-1];
24+
dp[i][j] = dp[i][j] or dp[i][j-1];
25+
}
26+
return dp[n][m];
27+
}
28+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Solution {
2+
3+
// A faster method
4+
public bool IsSubsequence(string s, string t) {
5+
if (s == "")
6+
return true;
7+
8+
if (s.Length > t.Length)
9+
return false;
10+
11+
int inds = 0;
12+
foreach (char c in t) {
13+
if (s[inds] == c) {
14+
inds++;
15+
if (inds == s.Length)
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
21+
22+
// A more general method
23+
public bool IsSubsequenceMine(string s, string t) {
24+
25+
if (s.Length > t.Length)
26+
return false;
27+
28+
// the following comparison makes the method faster
29+
if (s.Length == t.Length)
30+
return s == t;
31+
32+
int inds = 0, indt = 0;
33+
while (inds < s.Length && indt < t.Length) {
34+
if (s[inds] == t[indt]) {
35+
inds++;
36+
}
37+
indt++;
38+
}
39+
return inds > s.Length-1;
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int searchInsert(vector<int>& nums, int target) {
4+
return distance(nums.begin(), lower_bound(nums.begin(), nums.end(), target));
5+
}
6+
};
7+
8+
9+
class Solution2 {
10+
public:
11+
int searchInsert(vector<int>& nums, int target) {
12+
return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
13+
}
14+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
void swap(int& a, int& b) {
4+
int tmp = a;
5+
a = b;
6+
b = tmp;
7+
}
8+
9+
void sortColors(vector<int>& nums) {
10+
int a = 0, b = nums.size()-1;
11+
int i = 0;
12+
while (i <= b){
13+
if (nums[i] == 2){
14+
swap(nums[b], nums[i]);
15+
b--;
16+
continue;
17+
}
18+
19+
if (nums[i] == 0) {
20+
swap(nums[a], nums[i]);
21+
a++;
22+
}
23+
i++;
24+
}
25+
}
26+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class RandomizedSet {
2+
public:
3+
unordered_map<int,int> m;
4+
/** Initialize your data structure here. */
5+
RandomizedSet() {
6+
7+
}
8+
9+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
10+
bool insert(int val) {
11+
return (++m[val])==1;
12+
}
13+
14+
/** Removes a value from the set. Returns true if the set contained the specified element. */
15+
bool remove(int val) {
16+
if(m.find(val)==m.end()) return false;
17+
m.erase(val);
18+
return true;
19+
}
20+
21+
/** Get a random element from the set. */
22+
int getRandom() {
23+
return next(begin(m), rand()%m.size())->first;
24+
}
25+
};
26+
27+
class RandomizedSet {
28+
unordered_map<int, int> hm;
29+
vector<int> datas;
30+
public:
31+
/** Initialize your data structure here. */
32+
RandomizedSet() { }
33+
34+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
35+
bool insert(int val) {
36+
if (hm.find(val) != hm.end())
37+
return false;
38+
hm[val] = datas.size();
39+
datas.push_back(val);
40+
return true;
41+
}
42+
43+
/** Removes a value from the set. Returns true if the set contained the specified element. */
44+
bool remove(int val) {
45+
if (hm.find(val) == hm.end())
46+
return false;
47+
hm[datas.back()] = hm[val];
48+
datas[hm[val]] = datas.back();
49+
hm.erase(val);
50+
datas.pop_back();
51+
return true;
52+
}
53+
54+
/** Get a random element from the set. */
55+
int getRandom() {
56+
return datas[random()%datas.size()];
57+
}
58+
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
class Solution {
2+
public:
3+
vector<int> largestDivisibleSubset(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int n = nums.size();
6+
if (n == 0) return vector<int>();
7+
vector<int> dp(n+1, 0);
8+
vector<int> prev(n+1, 0);
9+
10+
int maxv = 0, p = 0;
11+
12+
for (int i = 1; i <= n; i++){
13+
dp[i] = 1;
14+
prev[i] = i;
15+
for (int j = 1; j < i; j++){
16+
if (nums[i-1] % nums[j-1] == 0 and dp[j] + 1 > dp[i]){
17+
prev[i] = j;
18+
dp[i] = dp[j] + 1;
19+
}
20+
}
21+
if (dp[i] > maxv){
22+
maxv = dp[i];
23+
p = i;
24+
}
25+
}
26+
27+
vector<int> ans;
28+
while (p != prev[p]) {
29+
ans.push_back(nums[p-1]);
30+
p = prev[p];
31+
}
32+
ans.push_back(nums[p-1]);
33+
34+
sort(ans.begin(), ans.end());
35+
return ans;
36+
}
37+
};
38+
39+
class Solution {/* time: O(n*n), space: O(n) */
40+
public:
41+
/*
42+
* the idea is for each num in nums,
43+
* find all multiple of num.
44+
* each num have a count of previous multiple
45+
*/
46+
vector<int> largestDivisibleSubset(vector<int>& nums) {
47+
int i;
48+
if (!nums.size())
49+
return {};
50+
51+
map<int, int> m;
52+
map<int, int>::iterator it;
53+
vector<pair<int, int>> dp(nums.size());
54+
unsigned int acc, num;
55+
int largest_last_index = 0;
56+
int largest_subcount = 0;
57+
58+
sort(nums.begin(), nums.end());
59+
for (i = 0; i < nums.size(); ++i)
60+
m[nums[i]] = i;
61+
62+
if (nums[0] == 1) {
63+
fill(dp.begin()+1, dp.end(), make_pair(0,1));
64+
dp[0].first = -1;
65+
dp[0].second = 0;
66+
largest_last_index = nums.size()-1;
67+
i = 1;
68+
} else {
69+
i = 0;
70+
fill(dp.begin(), dp.end(), make_pair(-1,0));
71+
}
72+
73+
for (; i < nums.size(); ++i) {
74+
num = nums[i];
75+
acc = num << 1;
76+
if ((int)num > (int)acc) // solve overflow problem
77+
continue;
78+
/* find all multiple of num */
79+
while ((it = m.upper_bound(acc-1)) != m.end()) {
80+
int mod = it->first % num;
81+
auto &ref = dp[i];
82+
auto &tgt = dp[it->second];
83+
/* current list of multiple is larger than the old one */
84+
if (mod == 0 && ref.second >= tgt.second) {
85+
/* move target into biger list */
86+
tgt.first = i ;
87+
tgt.second = ref.second +1;
88+
if (tgt.second > largest_subcount) {
89+
largest_subcount = tgt.second;
90+
largest_last_index = it->second;
91+
}
92+
}
93+
acc = it->first - mod + num;
94+
if ((int)num > (int)acc) // solve overflow problem
95+
break;
96+
}
97+
}
98+
99+
vector<int> ret;
100+
while (largest_last_index >= 0) {
101+
ret.push_back(nums[largest_last_index]);
102+
largest_last_index = dp[largest_last_index].first;
103+
}
104+
return ret;
105+
}
106+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Solution {
2+
public:
3+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
4+
vector<vector<int>> a(n, vector<int>(n, 0));
5+
for (int i = 0; i < flights.size(); i++)
6+
a[flights[i][0]][flights[i][1]] = flights[i][2];
7+
8+
9+
vector<vector<int>> dp(K+2, vector<int>(n, 0));
10+
11+
for (int i = 0; i < n; i++) dp[0][i] = 1000000000;
12+
dp[0][src] = 0;
13+
14+
int ans = min(dp[0][dst], 1000000000);
15+
for (int i = 1; i <= K+1; i++){
16+
for (int j = 0; j < n; j++){
17+
dp[i][j] = 1000000000;
18+
for (int k = 0; k < n; k++)
19+
if (a[k][j])
20+
dp[i][j] = min(dp[i][j], dp[i-1][k] + a[k][j]);
21+
}
22+
ans = min(ans, dp[i][dst]);
23+
}
24+
return (ans == 1000000000)?-1:ans;
25+
}
26+
};
27+
28+
class Solution2 {
29+
// algo dijkstra
30+
public:
31+
typedef tuple<int, int, int> typeTuple; // (AccumulatedCost, cityId, stopUsed)
32+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
33+
34+
priority_queue<typeTuple, vector<typeTuple>, greater<typeTuple>> neighs;
35+
36+
unordered_map<int, vector<pair<int, int>>> g;
37+
for (auto& v : flights)
38+
g[v[0]].emplace_back(v[1], v[2]);
39+
40+
neighs.emplace(0, src, 0);
41+
42+
while (!neighs.empty()) {
43+
auto t = neighs.top();
44+
int cost = get<0>(t); // accumulated cost
45+
int u = get<1>(t); // city u -> city v
46+
int stopused = get<2>(t);
47+
neighs.pop();
48+
49+
if (u == dst)
50+
return cost;
51+
52+
if (stopused > K) continue;
53+
54+
for (auto p : g[u])
55+
neighs.emplace(cost + p.second, p.first, stopused + 1); // increment stopused
56+
57+
}
58+
return -1;
59+
60+
}
61+
};

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ Solutions in various programming languages are provided. Enjoy it.
1616
6. [Queue Reconstruction by Height](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/06-Queue-Reconstruction-By-Height): Greedy
1717
7. [Coin Change 2](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/07-Coin-Change-2): DP
1818
8. [Power of Two](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/08-Power-Of-Two): Bitwise manipulation
19-
19+
9. [Is Subsequence](https://github.com/AlgoStudyGroup/Leetcode/blob/master/June-LeetCoding-Challenge/09-Is-Subsequence): Two pointers / DP
20+
10. [Search Insert Position](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/10-Search-Insert-Position): Binary Search
21+
11. [Sort Colors](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/11-Sort-Colors): Two Pointers
22+
12. [Insert Delete GetRandom](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/12-Insert-Delete-GetRandom): Hash Table
23+
13. [Largest Divisible Subset](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/13-Largest-Divisible-Subset): DP
24+
14. [Cheapest Flights Within K Stops](https://github.com/AlgoStudyGroup/Leetcode/tree/master/June-LeetCoding-Challenge/14-Cheapest-Flights-Within-K-Stops): DP
2025

2126

2227
## May LeetCoding Challenge

0 commit comments

Comments
 (0)