Skip to content

Commit 566ff5e

Browse files
committed
🚀 02-Oct-2020
1 parent 55a050c commit 566ff5e

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
You have a RecentCounter class which counts the number of recent requests within a certain time frame.
2+
3+
Implement the RecentCounter class:
4+
5+
RecentCounter() Initializes the counter with zero recent requests.
6+
int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
7+
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.
8+
9+
10+
11+
Example 1:
12+
13+
Input
14+
["RecentCounter", "ping", "ping", "ping", "ping"]
15+
[[], [1], [100], [3001], [3002]]
16+
Output
17+
[null, 1, 2, 3, 3]
18+
19+
Explanation
20+
RecentCounter recentCounter = new RecentCounter();
21+
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
22+
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
23+
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
24+
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
25+
26+
27+
Constraints:
28+
29+
1 <= t <= 109
30+
Each test case will call ping with strictly increasing values of t.
31+
At most 104 calls will be made to ping.
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
class RecentCounter {
45+
public:
46+
47+
queue<int> q;
48+
int capacity=3000;
49+
50+
RecentCounter() {
51+
}
52+
53+
int ping(int t) {
54+
q.push(t);
55+
int start=t-capacity;
56+
while(!q.empty() && q.front()<start){
57+
q.pop();
58+
}
59+
return q.size();
60+
61+
}
62+
};
63+
64+
/**
65+
* Your RecentCounter object will be instantiated and called as such:
66+
* RecentCounter* obj = new RecentCounter();
67+
* int param_1 = obj->ping(t);
68+
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
2+
3+
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
4+
5+
6+
7+
Example 1:
8+
9+
Input: candidates = [2,3,6,7], target = 7
10+
Output: [[2,2,3],[7]]
11+
Explanation:
12+
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
13+
7 is a candidate, and 7 = 7.
14+
These are the only two combinations.
15+
Example 2:
16+
17+
Input: candidates = [2,3,5], target = 8
18+
Output: [[2,2,2,2],[2,3,3],[3,5]]
19+
Example 3:
20+
21+
Input: candidates = [2], target = 1
22+
Output: []
23+
Example 4:
24+
25+
Input: candidates = [1], target = 1
26+
Output: [[1]]
27+
Example 5:
28+
29+
Input: candidates = [1], target = 2
30+
Output: [[1,1]]
31+
32+
33+
Constraints:
34+
35+
1 <= candidates.length <= 30
36+
1 <= candidates[i] <= 200
37+
All elements of candidates are distinct.
38+
1 <= target <= 500
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
class Solution {
49+
public:
50+
51+
void solve(vector<int> candidates, int target, int start, vector<int> &csum, vector<vector<int> > &res){
52+
if(target==0){
53+
res.push_back(csum);
54+
return;
55+
}
56+
if(target<0) return;
57+
58+
for(int i=start;i<candidates.size();i++){
59+
csum.push_back(candidates[i]);
60+
solve(candidates, target-candidates[i], i, csum, res);
61+
csum.erase(csum.end()-1);
62+
}
63+
}
64+
65+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
66+
vector<vector<int> > res;
67+
vector<int> csum;
68+
69+
solve(candidates, target, 0, csum, res);
70+
71+
return res;
72+
}
73+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
You have a RecentCounter class which counts the number of recent requests within a certain time frame.
2+
3+
Implement the RecentCounter class:
4+
5+
RecentCounter() Initializes the counter with zero recent requests.
6+
int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
7+
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.
8+
9+
10+
11+
Example 1:
12+
13+
Input
14+
["RecentCounter", "ping", "ping", "ping", "ping"]
15+
[[], [1], [100], [3001], [3002]]
16+
Output
17+
[null, 1, 2, 3, 3]
18+
19+
Explanation
20+
RecentCounter recentCounter = new RecentCounter();
21+
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
22+
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
23+
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
24+
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
25+
26+
27+
Constraints:
28+
29+
1 <= t <= 109
30+
Each test case will call ping with strictly increasing values of t.
31+
At most 104 calls will be made to ping.
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
class RecentCounter {
45+
public:
46+
47+
queue<int> q;
48+
int capacity=3000;
49+
50+
RecentCounter() {
51+
}
52+
53+
int ping(int t) {
54+
q.push(t);
55+
int start=t-capacity;
56+
while(!q.empty() && q.front()<start){
57+
q.pop();
58+
}
59+
return q.size();
60+
61+
}
62+
};
63+
64+
/**
65+
* Your RecentCounter object will be instantiated and called as such:
66+
* RecentCounter* obj = new RecentCounter();
67+
* int param_1 = obj->ping(t);
68+
*/

0 commit comments

Comments
 (0)