Skip to content
This repository was archived by the owner on May 16, 2021. It is now read-only.

Commit 8e423e1

Browse files
committed
add 3 problems
1 parent 275ba44 commit 8e423e1

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

exclusive-time-of-functions_1_AC.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <cstdio>
2+
#include <stack>
3+
#include <string>
4+
#include <vector>
5+
using std::sscanf;
6+
using std::stack;
7+
using std::string;
8+
using std::vector;
9+
10+
class Solution {
11+
public:
12+
vector<int> exclusiveTime(int n, vector<string>& logs) {
13+
vector<int> res(n, 0);
14+
stack<int> si, st;
15+
int i, t;
16+
17+
vector<string> vs;
18+
int len;
19+
for (string &log: logs) {
20+
split(log, vs, ':');
21+
sscanf(vs[0].data(), "%d", &i);
22+
sscanf(vs[2].data(), "%d", &t);
23+
if (vs[1] == "start") {
24+
si.push(i);
25+
st.push(t);
26+
} else if (vs[1] == "end") {
27+
t += 1;
28+
if (si.top() != i) {
29+
// not supposed to happen
30+
return vector<int>();
31+
}
32+
len = t - st.top();
33+
res[i] += len;
34+
si.pop();
35+
st.pop();
36+
if (!si.empty()) {
37+
res[si.top()] -= len;
38+
}
39+
}
40+
}
41+
42+
return res;
43+
}
44+
private:
45+
void split(const string &s, vector<string> &vs, char del) {
46+
int ls = s.size();
47+
int i;
48+
string ss;
49+
50+
vs.clear();
51+
i = 0;
52+
while (true) {
53+
if (i < ls && s[i] != del) {
54+
ss.push_back(s[i]);
55+
} else if (ss.size() > 0) {
56+
vs.push_back(ss);
57+
ss.clear();
58+
}
59+
if (i < ls) {
60+
++i;
61+
} else {
62+
break;
63+
}
64+
}
65+
}
66+
};

maximum-average-subarray-i_1_AC.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <algorithm>
2+
#include <vector>
3+
using std::max;
4+
using std::vector;
5+
6+
class Solution {
7+
public:
8+
double findMaxAverage(vector<int>& nums, int k) {
9+
double sum;
10+
int n = nums.size();
11+
int i;
12+
13+
sum = 0;
14+
i = 0;
15+
while (i < k) {
16+
sum += nums[i];
17+
++i;
18+
}
19+
20+
double res = sum;
21+
while (i < n) {
22+
sum += (nums[i] - nums[i - k]);
23+
res = max(res, sum);
24+
++i;
25+
}
26+
27+
return res / k;
28+
}
29+
};

maximum-average-subarray-ii_1_AC.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// If the answer is not among variables, try to find them among values.
2+
#include <algorithm>
3+
#include <vector>
4+
using std::max;
5+
using std::min;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
double findMaxAverage(vector<int>& nums, int k) {
11+
auto &a = nums;
12+
int n = a.size();
13+
double ll, mm, rr;
14+
static const double EPS = 1e-5;
15+
16+
ll = mm = a[0];
17+
for (auto &x: a) {
18+
ll = min<double>(ll, x);
19+
rr = max<double>(rr, x);
20+
}
21+
while (rr - ll > EPS) {
22+
mm = ll + (rr - ll) / 2;
23+
if (hasLargerAverage(a, k, mm)) {
24+
ll = mm;
25+
} else {
26+
rr = mm;
27+
}
28+
}
29+
return mm;
30+
}
31+
private:
32+
bool hasLargerAverage(vector<int> &a, int k, double ave) {
33+
int n = a.size();
34+
double sum, pref_sum, min_sum;
35+
int i;
36+
37+
sum = pref_sum = min_sum = 0;
38+
i = 0;
39+
while (i < k) {
40+
sum += a[i] - ave;
41+
++i;
42+
}
43+
if (sum > 0) {
44+
return true;
45+
}
46+
while (i < n) {
47+
sum += a[i] - ave;
48+
pref_sum += a[i - k] - ave;
49+
min_sum = min(min_sum, pref_sum);
50+
if (sum - min_sum > 0) {
51+
return true;
52+
}
53+
++i;
54+
}
55+
return false;
56+
}
57+
};

0 commit comments

Comments
 (0)