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

Commit 5652f77

Browse files
committed
add 6 problems
1 parent 2df38b3 commit 5652f77

6 files changed

+354
-0
lines changed

course-schedule-iii_1_AC.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <algorithm>
2+
#include <queue>
3+
#include <vector>
4+
using std::priority_queue;
5+
using std::sort;
6+
using std::vector;
7+
8+
bool comparator(const vector<int> &v1, const vector<int> &v2)
9+
{
10+
return v1[1] < v2[1];
11+
}
12+
13+
class Solution {
14+
public:
15+
int scheduleCourse(vector<vector<int>>& courses) {
16+
auto &vc = courses;
17+
sort(vc.begin(), vc.end(), comparator);
18+
19+
priority_queue<int> pq;
20+
int now = 0;
21+
int tp;
22+
for (auto &c: vc) {
23+
if (now + c[0] <= c[1]) {
24+
now += c[0];
25+
pq.push(c[0]);
26+
continue;
27+
}
28+
29+
tp = pq.top();
30+
if (c[0] >= tp) {
31+
continue;
32+
}
33+
34+
now += c[0] - tp;
35+
pq.pop();
36+
pq.push(c[0]);
37+
}
38+
39+
int res = pq.size();
40+
return res;
41+
}
42+
};

design-log-storage-system_1_AC.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// I think the problem is poorly specified.
2+
// If it's supposed to be an OO or system design problem, much more have to be said.
3+
#include <cstdint>
4+
#include <cstdio>
5+
#include <map>
6+
#include <string>
7+
#include <vector>
8+
using std::map;
9+
using std::sscanf;
10+
using std::string;
11+
using std::vector;
12+
13+
class LogSystem {
14+
public:
15+
LogSystem() {
16+
string legend = "Year:Month:Day:Hour:Minute:Second";
17+
vl = split(legend, ':');
18+
19+
int n = vl.size();
20+
int i;
21+
for (i = 0; i < n; ++i) {
22+
gi[vl[i]] = i;
23+
}
24+
25+
int64_t conv[] = {366, 31, 24, 60, 60, 1};
26+
n = sizeof(conv) / sizeof(int64_t);
27+
for (i = 0; i < n; ++i) {
28+
vc.push_back(conv[i]);
29+
}
30+
31+
n = vc.size();
32+
vc_pro.resize(n);
33+
vc_pro[n - 1] = vc[n - 1];
34+
for (i = n - 2; i >= 0; --i) {
35+
vc_pro[i] = vc_pro[i + 1] * vc[i];
36+
}
37+
}
38+
39+
void put(int id, string timestamp) {
40+
int64_t ts = getTimestamp(timestamp);
41+
logs[ts] = id;
42+
}
43+
44+
vector<int> retrieve(string s, string e, string gra) {
45+
int gra_idx = gi[gra];
46+
int64_t g = vc_pro[gra_idx];
47+
int64_t ts = getTimestamp(s) / g * g;
48+
int64_t te = (getTimestamp(e) / g + 1) * g;
49+
50+
map<int64_t, int>::iterator it1, it2;
51+
it1 = logs.lower_bound(ts);
52+
it2 = logs.lower_bound(te);
53+
vector<int> res;
54+
for (auto it = it1; it != it2; ++it) {
55+
res.push_back(it->second);
56+
}
57+
58+
return res;
59+
}
60+
private:
61+
vector<string> vl;
62+
vector<int64_t> vc;
63+
vector<int64_t> vc_pro;
64+
unordered_map<string, int> gi;
65+
map<int64_t, int> logs;
66+
67+
vector<string> split(const string &s, char del) {
68+
vector<string> vs;
69+
string ss;
70+
int ls = s.size();
71+
int i;
72+
for (i = 0; i < ls; ++i) {
73+
if (s[i] != del) {
74+
ss.push_back(s[i]);
75+
} else {
76+
if (ss.size() > 0) {
77+
vs.push_back(ss);
78+
ss.clear();
79+
}
80+
}
81+
}
82+
if (ss.size() > 0) {
83+
vs.push_back(ss);
84+
ss.clear();
85+
}
86+
87+
return vs;
88+
}
89+
90+
int64_t getTimestamp(const string &s) {
91+
vector<string> vs = split(s, ':');
92+
int64_t res = 0;
93+
int n = vs.size();
94+
int i;
95+
int64_t val;
96+
for (i = 0; i < n; ++i) {
97+
sscanf(vs[i].data(), "%lld", &val);
98+
res += val * vc_pro[i];
99+
}
100+
101+
return res;
102+
}
103+
};
104+
105+
/**
106+
* Your LogSystem object will be instantiated and called as such:
107+
* LogSystem obj = new LogSystem();
108+
* obj.put(id,timestamp);
109+
* vector<int> param_2 = obj.retrieve(s,e,gra);
110+
*/

k-inverse-pairs-array_1_AC.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Actually k can be as large as O(n ^ 2).
2+
// Do some reduction before coding.
3+
// Try not to worry about boundary, it simplifies the problem.
4+
#include <algorithm>
5+
#include <vector>
6+
using std::max;
7+
using std::min;
8+
using std::vector;
9+
10+
class Solution {
11+
public:
12+
int kInversePairs(int n, int iv) {
13+
if (iv == 0) {
14+
return 1;
15+
}
16+
17+
const int MOD = 1000000007;
18+
vector<vector<int>> dp(2, vector<int>(iv + 1));
19+
int m, m0;
20+
int i, j;
21+
int f, nf;
22+
23+
f = 1;
24+
nf = !f;
25+
dp[nf][0] = 1;
26+
m0 = 0;
27+
for (i = 1; i < n; ++i) {
28+
m = min(i * (i + 1) / 2, iv);
29+
for (j = 0; j <= m; ++j) {
30+
dp[f][j] = 0;
31+
}
32+
33+
dp[f][0] = 1;
34+
for (j = 1; j <= m; ++j) {
35+
dp[f][j] = dp[f][j - 1];
36+
if (j <= m0) {
37+
dp[f][j] = (dp[f][j] + dp[nf][j]) % MOD;
38+
}
39+
if (j - (i + 1) >= 0 && j - (i + 1) <= m0) {
40+
dp[f][j] = (dp[f][j] + MOD - dp[nf][j - (i + 1)]) % MOD;
41+
}
42+
}
43+
m0 = m;
44+
45+
f = !f;
46+
nf = !f;
47+
}
48+
49+
int res = dp[nf][iv];
50+
dp.clear();
51+
52+
return res;
53+
}
54+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <algorithm>
2+
#include <climits>
3+
#include <vector>
4+
using std::max;
5+
using std::min;
6+
using std::vector;
7+
8+
class Solution {
9+
public:
10+
int maximumProduct(vector<int>& nums) {
11+
auto &a = nums;
12+
int n = a.size();
13+
14+
// v1 for max, v2 for min
15+
vector<vector<int>> v1(2, vector<int>(n)), v2(2, vector<int>(n));
16+
const int k = 3;
17+
18+
int f, nf;
19+
int i;
20+
f = 1;
21+
nf = !f;
22+
v1[nf][0] = v2[nf][0] = a[0];
23+
for (i = 1; i < n; ++i) {
24+
v1[nf][i] = max(v1[nf][i - 1], a[i]);
25+
v2[nf][i] = min(v2[nf][i - 1], a[i]);
26+
}
27+
28+
int p1, p2;
29+
int j;
30+
for (i = 1; i < k; ++i) {
31+
v1[f][i - 1] = INT_MIN;
32+
v2[f][i - 1] = INT_MAX;
33+
for (j = i; j < n; ++j) {
34+
p1 = v1[nf][j - 1] * a[j];
35+
p2 = v2[nf][j - 1] * a[j];
36+
v1[f][j] = max(v1[f][j - 1], max(p1, p2));
37+
v2[f][j] = min(v2[f][j - 1], min(p1, p2));
38+
}
39+
f = !f;
40+
nf = !f;
41+
}
42+
43+
int res = v1[nf][n - 1];
44+
v1.clear();
45+
v2.clear();
46+
47+
return res;
48+
}
49+
};

smallest-range_1_AC.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <algorithm>
2+
#include <utility>
3+
#include <vector>
4+
using std::make_pair;
5+
using std::pair;
6+
using std::sort;
7+
using std::vector;
8+
9+
typedef pair<int, int> PII;
10+
bool comparator(const PII &p1, const PII &p2)
11+
{
12+
if (p1.first != p2.first) {
13+
return p1.first < p2.first;
14+
}
15+
if (p1.second != p2.second) {
16+
return p1.second < p2.second;
17+
}
18+
return false;
19+
}
20+
21+
class Solution {
22+
public:
23+
vector<int> smallestRange(vector<vector<int>>& nums) {
24+
vector<pair<int, int>> v;
25+
int nl = nums.size();
26+
int i;
27+
int len;
28+
for (i = 0; i < nl; ++i) {
29+
for (auto &x: nums[i]) {
30+
v.push_back(make_pair(x, i));
31+
}
32+
}
33+
sort(v.begin(), v.end(), comparator);
34+
35+
vector<int> c(nl, 0);
36+
int cc = 0;
37+
int mi, mj;
38+
int j;
39+
int r, min_r;
40+
int idx;
41+
42+
len = v.size();
43+
mi = 0;
44+
mj = len - 1;
45+
min_r = v[mj].first - v[mi].first;
46+
i = 0;
47+
j = 0;
48+
do {
49+
while (j < len && cc < nl) {
50+
// push j forward until every list is covered
51+
idx = v[j].second;
52+
if (c[idx] == 0) {
53+
++cc;
54+
}
55+
++c[idx];
56+
++j;
57+
}
58+
while (cc >= nl) {
59+
// push i forward until not every list is covered
60+
idx = v[i].second;
61+
--c[idx];
62+
++i;
63+
if (c[idx] == 0) {
64+
--cc;
65+
}
66+
}
67+
r = v[j - 1].first - v[i - 1].first;
68+
if (r < min_r) {
69+
mi = i - 1;
70+
mj = j - 1;
71+
min_r = r;
72+
}
73+
} while (j < len);
74+
75+
vector<int> res(2);
76+
res[0] = v[mi].first;
77+
res[1] = v[mj].first;
78+
v.clear();
79+
c.clear();
80+
81+
return res;
82+
}
83+
};

sum-of-square-numbers_1_AC.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <cmath>
2+
using std::sqrt;
3+
4+
class Solution {
5+
public:
6+
bool judgeSquareSum(int c) {
7+
int i, j;
8+
for (i = 0; 2LL * i * i <= c; ++i) {
9+
j = (int)sqrt(c - i * i);
10+
if (j * j == c - i * i) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
};

0 commit comments

Comments
 (0)