File tree 3 files changed +122
-0
lines changed
3 files changed +122
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < deque>
2
+ #include < iostream>
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ int maximumRobots (vector<int >& chargeTimes, vector<int >& runningCosts,
8
+ long long budget) {
9
+ int ans = 0 , le = 0 ;
10
+ long long sum = 0 ;
11
+ deque<int > dq;
12
+ for (int ri = 0 ; ri < chargeTimes.size (); ri++) {
13
+ // enque
14
+ while (!dq.empty () && chargeTimes[ri] >= chargeTimes[dq.back ()]) {
15
+ dq.pop_back ();
16
+ }
17
+ dq.push_back (ri);
18
+ sum += runningCosts[ri];
19
+
20
+ // deque
21
+ while (!dq.empty () &&
22
+ chargeTimes[dq.front ()] + (ri - le + 1 ) * sum > budget) {
23
+ if (dq.front () == le) {
24
+ dq.pop_front ();
25
+ }
26
+ sum -= runningCosts[le++];
27
+ }
28
+
29
+ ans = max (ans, ri - le + 1 );
30
+ }
31
+ return ans;
32
+ }
33
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ string removeStars1 (string s) {
7
+ string ans;
8
+ stack<char > stk;
9
+ for (char c : s) {
10
+ if (c == ' *' ) {
11
+ stk.pop ();
12
+ } else {
13
+ stk.push (c);
14
+ }
15
+ }
16
+ while (!stk.empty ()) {
17
+ char c = stk.top ();
18
+ ans.push_back (c);
19
+ stk.pop ();
20
+ }
21
+ std::reverse (ans.begin (), ans.end ());
22
+ return ans;
23
+ }
24
+
25
+ string removeStars2 (string s) {
26
+ string ans;
27
+ deque<char > dq;
28
+ for (char c : s) {
29
+ if (c == ' *' ) {
30
+ dq.pop_back ();
31
+ } else {
32
+ dq.push_back (c);
33
+ }
34
+ }
35
+
36
+ while (!dq.empty ()) {
37
+ ans.push_back (dq.front ());
38
+ dq.pop_front ();
39
+ }
40
+ return ans;
41
+ }
42
+
43
+ /* *
44
+ * @brief remove stars in a string
45
+ * Use the stack to solve this problem, however, use stack or deque
46
+ * is a little bit complex
47
+ * Remember that `string` also has the `pop_back()` api, just use string.
48
+ *
49
+ * @param s
50
+ * @return string
51
+ */
52
+ string removeStars3 (string s) {
53
+ string ans;
54
+ for (char c : s) {
55
+ if (c == ' *' ) {
56
+ ans.pop_back ();
57
+ } else {
58
+ ans += c;
59
+ }
60
+ }
61
+ return ans;
62
+ }
63
+ };
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+
3
+ class Solution {
4
+ public:
5
+ vector<int > sortedSquares (vector<int > nums) {
6
+ int len = nums.size ();
7
+ for (int i = 0 ; i < len ; i++) {
8
+ nums[i] = nums[i] * nums[i];
9
+ }
10
+
11
+ vector<int > ans (len, 0 );
12
+ int le = 0 , ri = len - 1 ;
13
+ int pivot = len - 1 ;
14
+ while (le <= ri) {
15
+ if (nums[le] > nums[ri]) {
16
+ ans[pivot] = nums[le]; // get larger
17
+ le++;
18
+ } else {
19
+ ans[pivot] = nums[ri];
20
+ ri--;
21
+ }
22
+ pivot--;
23
+ }
24
+ return ans;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments