Skip to content

Commit dd50eaf

Browse files
committed
feat: solve problem
1 parent 803cabd commit dd50eaf

18 files changed

+533
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
//Runtime: 4 ms, faster than 89.79% of C++ online submissions for Find Minimum in Rotated Sorted Array.
3+
//Memory Usage: 8.8 MB, less than 29.27% of C++ online submissions for Find Minimum in Rotated Sorted Array.
4+
public:
5+
int findMin(vector<int>& nums) {
6+
int minVal=0x7fffffff;
7+
function<void(int,int)> helper=[&](int low,int high){
8+
if(low>high)
9+
return;
10+
if(nums[low]<=nums[high])
11+
minVal=min(minVal,nums[low]);
12+
else{
13+
int mid=(high+low)/2;
14+
helper(low,mid-1);
15+
helper(mid+1,high);
16+
minVal=min(minVal,nums[mid]);
17+
}
18+
};
19+
helper(0,nums.size()-1);
20+
return minVal;
21+
}
22+
23+
};

cpp/220.ContainsDuplicateIii.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
//Runtime: 20 ms, faster than 71.64% of C++ online submissions for Contains Duplicate III.
3+
//Memory Usage: 10.9 MB, less than 32.96% of C++ online submissions for Contains Duplicate III.
4+
public:
5+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
6+
if (k < 1 or t < 0) return false;
7+
set<int>s;
8+
for(int i=0;i<nums.size();++i){
9+
if(i>k)
10+
s.erase(nums[i-k-1]);
11+
auto [it,success]=s.insert(nums[i]);
12+
if(not success)
13+
return true;
14+
if(it!=begin(s) and abs((long long)*prev(it)-(long long)*it)<=t)
15+
return true;
16+
if(next(it)!=end(s) and abs((long long)*next(it)-(long long)*it)<=t)
17+
return true;
18+
}
19+
return false;
20+
}
21+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
//Runtime: 0 ms Your runtime beats 100.00 % of cpp submissions
3+
//Memory Usage: 9.1 MB Your memory usage beats 95.63 % of cpp submissions.
4+
public:
5+
vector<int> diffWaysToCompute(string input) {
6+
unordered_map<string_view, vector<int>> dpMap;
7+
function<void(string_view)>recursion=[&](string_view input) {
8+
vector<int> result;
9+
for (int i = 0; i < input.size(); i++) {
10+
char cur = input[i];
11+
if (cur == '+' || cur == '-' || cur == '*') {
12+
auto substr = input.substr(0, i);
13+
if (dpMap.find(substr) == dpMap.end())
14+
recursion(substr);
15+
auto &result1 = dpMap[substr];
16+
substr = input.substr(i + 1);
17+
if (dpMap.find(substr) == dpMap.end())
18+
recursion(substr);
19+
auto &result2 = dpMap[substr];
20+
for (auto n1 : result1)
21+
for (auto n2 : result2)
22+
switch(cur){
23+
case '+':result.push_back(n1 + n2);break;
24+
case '-':result.push_back(n1 - n2);break;
25+
case '*':result.push_back(n1 * n2);break;
26+
}
27+
}
28+
}
29+
if (result.empty()){
30+
int res;
31+
from_chars(input.data(), input.data() + input.size(), res);
32+
result.push_back(res);
33+
}
34+
dpMap[input]=move(result);
35+
};
36+
recursion(input);
37+
return dpMap[input];
38+
}
39+
};

cpp/29.DivideTwoIntegers.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Divide Two Integers.
3+
//Memory Usage: 8.2 MB, less than 47.58% of C++ online submissions for Divide Two Integers.
4+
public:
5+
int divide(int dividend, int divisor) {
6+
if(divisor==0)
7+
return 0x7fffffff;
8+
if(dividend==0)
9+
return 0;
10+
auto res = [](long long dividend,long long divisor)->long long{
11+
auto res=0ll;
12+
while(dividend>=divisor){
13+
auto level=1ll;
14+
for(;(divisor<<level)<dividend;++level);
15+
res+=1ll<<(level-1ll);
16+
dividend-=divisor<<(level-1ll);
17+
}
18+
return res;
19+
}(abs((long long)dividend),abs((long long)divisor))*((dividend>0 and divisor>0)or(dividend<0 and divisor<0)?1:-1);
20+
if(res>2147483647ll)
21+
return 0x7fffffff;
22+
if(res<-2147483648ll)
23+
return -2146483648;
24+
return res;
25+
}
26+
};

cpp/365.WaterAndJugProblem.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
//Runtime: 984 ms, faster than 5.08% of C++ online submissions for Water and Jug Problem.
3+
//Memory Usage: 152.6 MB, less than 5.31% of C++ online submissions for Water and Jug Problem.
4+
public:
5+
bool canMeasureWater(int x, int y, int z) {
6+
array<function<void(int&,int&)>,6>ops={
7+
[&](int &a,int &){
8+
a=x;
9+
},
10+
[&](int &,int &b){
11+
b=y;
12+
},
13+
[&](int &a,int &){
14+
a=0;
15+
},
16+
[&](int &,int &b){
17+
b=0;
18+
},
19+
[&](int &a,int &b){
20+
auto diff=min(y-b,a);
21+
a-=diff;
22+
b+=diff;
23+
},
24+
[&](int &a,int &b){
25+
auto diff=min(x-a,b);
26+
a+=diff;
27+
b-=diff;
28+
}
29+
};
30+
unordered_set<long long>seen{0};
31+
deque<pair<int,int>> dq{{0,0}};
32+
while(dq.size()){
33+
for(auto &op:ops){
34+
auto [a,b]=dq.front();
35+
op(a,b);
36+
if(a==z or b==z or a+b==z)
37+
return true;
38+
auto [it,ok]=seen.insert(((long long)a<<32)+b);
39+
if(ok)
40+
dq.push_back({a,b});
41+
}
42+
dq.pop_front();
43+
}
44+
return false;
45+
}
46+
};

cpp/376.WiggleSubsequence.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
//Runtime: 4 ms, faster than 86.10% of C++ online submissions for Wiggle Subsequence.
3+
//Memory Usage: 8.8 MB, less than 7.24% of C++ online submissions for Wiggle Subsequence.
4+
public:
5+
int wiggleMaxLength(vector<int>& nums) {
6+
if (nums.size() == 0) return 0;
7+
int up = 1, down = 1;
8+
for (int i = 1; i < nums.size(); ++i)
9+
if (nums[i] > nums[i-1])
10+
up = down + 1;
11+
else if (nums[i] < nums[i-1])
12+
down = up + 1;
13+
return max(up, down);
14+
}
15+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class RandomizedSet {
2+
vector<int> nums;
3+
unordered_map<int, int> m;
4+
public:
5+
/** Initialize your data structure here. */
6+
RandomizedSet() {
7+
8+
}
9+
10+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
11+
bool insert(int val) {
12+
if (m.find(val) != m.end()) return false;
13+
nums.emplace_back(val);
14+
m[val] = nums.size() - 1;
15+
return true;
16+
}
17+
18+
/** Removes a value from the set. Returns true if the set contained the specified element. */
19+
bool remove(int val) {
20+
if (auto it=m.find(val);it == m.end())
21+
return false;
22+
else{
23+
m[nums.back()] = it->second;
24+
nums[it->second] = nums.back();
25+
nums.pop_back();
26+
m.erase(it);
27+
return true;
28+
}
29+
}
30+
31+
/** Get a random element from the set. */
32+
int getRandom() {
33+
return nums[rand() % nums.size()];
34+
}
35+
};
36+
37+
/**
38+
* Your RandomizedSet object will be instantiated and called as such:
39+
* RandomizedSet* obj = new RandomizedSet();
40+
* bool param_1 = obj->insert(val);
41+
* bool param_2 = obj->remove(val);
42+
* int param_3 = obj->getRandom();
43+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Longest Substring with At Least K Repeating Characters.
3+
//Memory Usage: 8.8 MB, less than 74.27% of C++ online submissions for Longest Substring with At Least K Repeating Characters.
4+
public:
5+
int longestSubstring(string s, int k) {
6+
int ret=0;
7+
function<void(string_view)> dfs=[&](string_view ss){
8+
if(not ss.size()) return;
9+
unordered_map<char,int>counter;
10+
for(auto ch:ss)
11+
++counter[ch];
12+
for(auto it=counter.begin();it!=counter.end();)
13+
if(it->second>=k)
14+
counter.erase(it++);
15+
else
16+
++it;
17+
if(counter.size()){
18+
int start=0;
19+
for(int i=0;i<ss.size();++i)
20+
if(counter.count(ss[i])){
21+
if(i-start>ret)
22+
dfs(ss.substr(start,i-start));
23+
start=i+1;
24+
}
25+
dfs(ss.substr(start));
26+
}
27+
else
28+
ret=max(ret,(int)ss.size());
29+
};
30+
dfs(s);
31+
return ret;
32+
}
33+
};

cpp/402.RemoveKDigits.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
//Runtime: 44 ms, faster than 12.45% of C++ online submissions for Remove K Digits.
3+
//Memory Usage: 180 MB, less than 8.63% of C++ online submissions for Remove K Digits.
4+
public:
5+
string removeKdigits(string num, int k) {
6+
if(k>=num.size())
7+
return "0";
8+
if(k==0){
9+
int i=0;
10+
while(i<num.size() and num[i]=='0')++i;
11+
return i==num.size()?"0":num.substr(i);
12+
}
13+
for(int i=0;i+1<num.size();++i)
14+
if(num[i]>num[i+1])
15+
return removeKdigits(num.substr(0,i)+num.substr(i+1),k-1);
16+
return removeKdigits(num.substr(0,num.size()-k),0);
17+
}
18+
};

cpp/410.SplitArrayLargestSum.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
//Runtime: 4 ms
3+
//Memory Usage: 8.6 MB
4+
public:
5+
int splitArray(vector<int>& nums, int m) {
6+
long long left = 0, right = 0;
7+
for (auto num : nums) {
8+
left = max(left, (long long)num);
9+
right += num;
10+
}
11+
while (left < right) {
12+
auto mid = left + (right - left) / 2;
13+
if ([](const vector<int>& nums, int cuts, long long max) {
14+
long long acc = 0;
15+
for (auto num : nums) {
16+
if (acc + num <= max)
17+
acc += num;
18+
else {
19+
--cuts;
20+
acc = num;
21+
if (cuts < 0) return false;
22+
}
23+
}
24+
return true;
25+
}(nums, m - 1, mid))
26+
right = mid;
27+
else
28+
left = mid + 1;
29+
}
30+
return left;
31+
}
32+
};

0 commit comments

Comments
 (0)