Skip to content

Commit 067c0b9

Browse files
committed
482/559
1 parent 4fddd0f commit 067c0b9

34 files changed

+706
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 454/495 problems.
5+
Solved 482/559 problems.
66

77
## Database
88

@@ -12,10 +12,39 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|635|[Design Log Storage System](https://leetcode.com/problems/design-log-storage-system)|[design-log-storage-system.cc](design-log-storage-system.cc)|
16+
|634|[Find the Derangement of An Array](https://leetcode.com/problems/find-the-derangement-of-an-array)|[find-the-derangement-of-an-array.cc](find-the-derangement-of-an-array.cc)|
17+
|633|[Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers)|[sum-of-square-numbers.cc](sum-of-square-numbers.cc)|
18+
|631|[Design Excel Sum Formula](https://leetcode.com/problems/design-excel-sum-formula)|[design-excel-sum-formula.cc](design-excel-sum-formula.cc)|
19+
|630|[Course Schedule III](https://leetcode.com/problems/course-schedule-iii)|[course-schedule-iii.cc](course-schedule-iii.cc)|
20+
|629|[K Inverse Pairs Array](https://leetcode.com/problems/k-inverse-pairs-array)|[k-inverse-pairs-array.cc](k-inverse-pairs-array.cc)|
21+
|628|[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers)|[maximum-product-of-three-numbers.cc](maximum-product-of-three-numbers.cc)|
22+
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees)|[merge-two-binary-trees.cc](merge-two-binary-trees.cc)|
23+
|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string)|[add-bold-tag-in-string.cc](add-bold-tag-in-string.cc)|
24+
|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number)|[valid-triangle-number.cc](valid-triangle-number.cc)|
25+
|604|[Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator)|[design-compressed-string-iterator.cc](design-compressed-string-iterator.cc)|
26+
|600|[Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones)|[non-negative-integers-without-consecutive-ones.cc](non-negative-integers-without-consecutive-ones.cc)|
27+
|599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists)|[minimum-index-sum-of-two-lists.cc](minimum-index-sum-of-two-lists.cc)|
28+
|598|[Range Addition II](https://leetcode.com/problems/range-addition-ii)|[range-addition-ii.cc](range-addition-ii.cc)|
29+
|594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence)|[longest-harmonious-subsequence.cc](longest-harmonious-subsequence.cc)|
30+
|593|[Valid Square](https://leetcode.com/problems/valid-square)|[valid-square.cc](valid-square.cc)|
31+
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction)|[fraction-addition-and-subtraction.cc](fraction-addition-and-subtraction.cc)|
32+
|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system)|[design-in-memory-file-system.cc](design-in-memory-file-system.cc)|
33+
|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence)|[erect-the-fence.cc](erect-the-fence.cc)|
34+
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings)|[delete-operation-for-two-strings.cc](delete-operation-for-two-strings.cc)|
35+
|582|[Kill Process](https://leetcode.com/problems/kill-process)|[kill-process.cc](kill-process.cc)|
36+
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray)|[shortest-unsorted-continuous-subarray.cc](shortest-unsorted-continuous-subarray.cc)|
37+
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies)|[distribute-candies.cc](distribute-candies.cc)|
38+
|565|[Array Nesting](https://leetcode.com/problems/array-nesting)|[array-nesting.cc](array-nesting.cc)|
39+
|556|[Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii)|[next-greater-element-iii.cc](next-greater-element-iii.cc)|
40+
|553|[Optimal Division](https://leetcode.com/problems/optimal-division)|[optimal-division.cc](optimal-division.cc)|
41+
|552|[Student Attendance Record II](https://leetcode.com/problems/student-attendance-record-ii)|[student-attendance-record-ii.cc](student-attendance-record-ii.cc)|
1542
|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii)|[lonely-pixel-ii.cc](lonely-pixel-ii.cc)|
1643
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i)|[lonely-pixel-i.cc](lonely-pixel-i.cc)|
1744
|514|[Freedom Trail](https://leetcode.com/problems/freedom-trail)|[freedom-trail.cc](freedom-trail.cc)|
1845
|508|[Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum)|[most-frequent-subtree-sum.cc](most-frequent-subtree-sum.cc)|
46+
|507|[Perfect Number](https://leetcode.com/problems/perfect-number)|[perfect-number.cc](perfect-number.cc)|
47+
|504|[Base 7](https://leetcode.com/problems/base-7)|[base-7.cc](base-7.cc)|
1948
|502|[IPO](https://leetcode.com/problems/ipo)|[ipo.cc](ipo.cc)|
2049
|501|[Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree)|[find-mode-in-binary-search-tree.cc](find-mode-in-binary-search-tree.cc)|
2150
|500|[Keyboard Row](https://leetcode.com/problems/keyboard-row)|[keyboard-row.cc](keyboard-row.cc)|
@@ -417,7 +446,6 @@ See [database.md](database.md)
417446
|52|[N-Queens II](https://leetcode.com/problems/n-queens-ii)|[n-queens-ii.cc](n-queens-ii.cc)|
418447
|51|[N-Queens](https://leetcode.com/problems/n-queens)|[n-queens.cc](n-queens.cc)|
419448
|50|[Pow(x, n)](https://leetcode.com/problems/powx-n)|[powx-n.cc](powx-n.cc)|
420-
|49|[Group Anagrams](https://leetcode.com/problems/anagrams)|[anagrams.cc](anagrams.cc)|
421449
|48|[Rotate Image](https://leetcode.com/problems/rotate-image)|[rotate-image.cc](rotate-image.cc)|
422450
|47|[Permutations II](https://leetcode.com/problems/permutations-ii)|[permutations-ii.cc](permutations-ii.cc)|
423451
|46|[Permutations](https://leetcode.com/problems/permutations)|[permutations.cc](permutations.cc)|

add-bold-tag-in-string.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Add Bold Tag in String
2+
class Solution {
3+
public:
4+
string addBoldTag(string s, vector<string>& dict) {
5+
vector<int> a(s.size()+1);
6+
for (auto t: dict) {
7+
vector<int> pi(t.size());
8+
for (int j = 0, i = 1; i < t.size(); i++) {
9+
while (j && t[j] != t[i]) j = pi[j-1];
10+
if (t[j] == t[i]) j++;
11+
pi[i] = j;
12+
}
13+
for (int j = 0, i = 0; i < s.size(); i++) {
14+
while (j && t[j] != s[i]) j = pi[j-1];
15+
if (t[j] == s[i] && ++j == t.size()) {
16+
a[i-t.size()+1]++;
17+
a[i+1]--;
18+
j = pi[j-1];
19+
}
20+
}
21+
}
22+
string r;
23+
int c = 0, cc;
24+
for (int i = 0; i <= s.size(); i++) {
25+
cc = c+a[i];
26+
if (! c && cc) r += "<b>";
27+
else if (c && ! cc) r += "</b>";
28+
if (i < n) r += s[i];
29+
c = cc;
30+
}
31+
return r;
32+
}
33+
};

array-nesting.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Array Nesting
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int arrayNesting(vector<int>& nums) {
8+
int n = nums.size(), r = 0;
9+
vector<int> v(n, 0);
10+
REP(i, nums.size())
11+
if (! v[i]) {
12+
int c = 0;
13+
for (int j = i; ; ) {
14+
c++;
15+
v[j] = 1;
16+
j = nums[j];
17+
if (j == i) break;
18+
}
19+
r = max(r, c);
20+
}
21+
return r;
22+
}
23+
};

base-7.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Base 7
2+
class Solution {
3+
public:
4+
string convertToBase7(int num) {
5+
if (num < 0) return '-'+convertToBase7(-num);
6+
string r;
7+
do r.push_back(num%7+'0');
8+
while (num /= 7);
9+
reverse(r.begin(), r.end());
10+
return r;
11+
}
12+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct LogSystem{
2+
map<int,string>a;
3+
void put(int i,string t){a[i]=t;}
4+
vector<int>retrieve(string s,string e,string g){
5+
int i=string("YMDHMS").find(g[0]);g[1]=='i'?i=4:0;
6+
for(s=s.substr(0,4+3*i),e=e.substr(0,4+3*i);i++<5;s+=":00",e+=":99");
7+
vector<int>r;
8+
for(auto&x:a)
9+
if(s<=(g=x.second)&&g<=e)
10+
r.push_back(x.first);
11+
return r;
12+
}
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Solution {
2+
int findDerangement(int n) {
3+
long s=0,i=2;
4+
for(;i<=n;i++)
5+
s=(i*s+1-i%2*2)%1000000007;
6+
return s;
7+
}
8+
};

course-schedule-iii.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Course Schedule III My SubmissionsBack To Contest
2+
class Solution {
3+
public:
4+
int scheduleCourse(vector<vector<int>>& as) {
5+
sort(as.begin(), as.end(), [](const vector<int>& x, const vector<int>& y) {
6+
return x[1] < y[1];
7+
});
8+
priority_queue<int> q;
9+
int s = 0;
10+
for (auto &a: as) {
11+
q.push(a[0]);
12+
if ((s += a[0]) > a[1])
13+
s -= q.top(), q.pop();
14+
}
15+
return q.size();
16+
}
17+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Delete Operation for Two Strings
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
int minDistance(string a, string b) {
8+
int m = a.size(), n = b.size();
9+
vector<vector<int>> s(2, vector<int>(n+1));
10+
iota(s[0].begin(), s[0].end(), 0);
11+
REP(i, m) {
12+
s[i+1&1][0] = i+1;
13+
REP(j, n)
14+
s[i+1&1][j+1] = a[i] == b[j] ? s[i&1][j] : min(s[i+1&1][j], s[i&1][j+1])+1;
15+
}
16+
return s[m&1][n];
17+
}
18+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Design Compressed String Iterator
2+
class StringIterator {
3+
string a;
4+
size_t i = 0, c = 0;
5+
char ch;
6+
public:
7+
StringIterator(string a) {
8+
this->a = a;
9+
}
10+
11+
char next() {
12+
if (c)
13+
return c--, ch;
14+
if (i >= a.size())
15+
return ' ';
16+
ch = a[i++];
17+
while (i < a.size() && isdigit(a[i]))
18+
c = c*10+a[i++]-'0';
19+
c--;
20+
return ch;
21+
}
22+
23+
bool hasNext() {
24+
return c || i < a.size();
25+
}
26+
};

design-excel-sum-formula.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Design Excel Sum Formula
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Excel {
6+
vector<vector<long>> a, c, d;
7+
vector<vector<vector<tuple<int,int,int,int>>>> b;
8+
long tick = 0;
9+
10+
pair<int, int> f(string x) {
11+
return {atoi(x.c_str()+1)-1, x[0]-'A'};
12+
}
13+
long get_sum(const vector<tuple<int,int,int,int>>& xs, bool t = true) {
14+
long sum = 0;
15+
t && tick++;
16+
for (auto& r: xs)
17+
FOR(i, std::get<0>(r), std::get<2>(r))
18+
FOR(j, std::get<1>(r), std::get<3>(r))
19+
sum += get_cell(i, j);
20+
return sum;
21+
}
22+
long get_cell(int x, int y) {
23+
if (a[x][y] != LONG_MAX) return a[x][y];
24+
if (d[x][y] == tick) return c[x][y];
25+
d[x][y] = tick;
26+
return c[x][y] = get_sum(b[x][y], false);
27+
}
28+
public:
29+
Excel(int H, char W) {
30+
a = vector<vector<long>>(H, vector<long>(W-'A'+1));
31+
b.resize(H);
32+
REP(i, H) b[i].resize(W);
33+
c = vector<vector<long>>(H, vector<long>(W-'A'+1));
34+
d = vector<vector<long>>(H, vector<long>(W-'A'+1));
35+
}
36+
37+
void set(int r, char c, int v) {
38+
a[r-1][c-'A'] = v;
39+
}
40+
41+
int get(int r, char c) {
42+
tick++;
43+
return get_cell(r-1, c-'A');
44+
}
45+
46+
int sum(int r, char c, vector<string> strs) {
47+
r--;
48+
c -= 'A';
49+
a[r][c] = LONG_MAX;
50+
b[r][c].clear();
51+
for (auto& s: strs) {
52+
string::size_type g;
53+
int x0, y0, x1, y1;
54+
if ((g = s.find(':')) != string::npos) {
55+
tie(x0, y0) = f(s.substr(0, g));
56+
tie(x1, y1) = f(s.substr(g+1));
57+
} else
58+
tie(x0, y0) = tie(x1, y1) = f(s);
59+
b[r][c].emplace_back(x0, y0, x1+1, y1+1);
60+
}
61+
return get_sum(b[r][c]);
62+
}
63+
};

0 commit comments

Comments
 (0)