Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9070e7

Browse files
committed
301/301
1 parent 78613d8 commit c9070e7

4 files changed

+89
-1
lines changed

README.md

+4-1
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 298/298 problems.
5+
Solved 301/301 problems.
66

77
## Database
88

@@ -12,6 +12,9 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|318|[Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/)|[maximum-product-of-word-lengths.cc](maximum-product-of-word-lengths.cc)|
16+
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[shortest-distance-from-all-buildings.cc](shortest-distance-from-all-buildings.cc)|
17+
|316|[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)|[remove-duplicate-letters.cc](remove-duplicate-letters.cc)|
1518
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)|[count-of-smaller-numbers-after-self.cc](count-of-smaller-numbers-after-self.cc)|
1619
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[binary-tree-vertical-order-traversal.cc](binary-tree-vertical-order-traversal.cc)|
1720
|313|[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/)|[super-ugly-number.cc](super-ugly-number.cc)|

maximum-product-of-word-lengths.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Maximum Product of Word Lengths
2+
class Solution {
3+
public:
4+
int maxProduct(vector<string>& words) {
5+
vector<int> a;
6+
int r = 0;
7+
for (auto &w: words) {
8+
int m = 0;
9+
for (auto c: w)
10+
m |= 1 << c-'a';
11+
int i = 0;
12+
for (auto mm: a) {
13+
if (! (m & mm))
14+
r = max(r, int(w.size() * words[i].size()));
15+
i++;
16+
}
17+
a.push_back(m);
18+
}
19+
return r;
20+
}
21+
};

remove-duplicate-letters.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Remove Duplicate Letters
2+
#define REP(i, n) for (int i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
string removeDuplicateLetters(string s) {
7+
int n = s.size(), last[26] = {};
8+
bool in[26] = {};
9+
string r;
10+
REP(i, n)
11+
last[s[i]-'a'] = i;
12+
REP(i, n)
13+
if (! in[s[i]-'a']) {
14+
while (r.size() && s[i] < r.back() && i < last[r.back()-'a']) {
15+
in[r.back()-'a'] = false;
16+
r.pop_back();
17+
}
18+
in[s[i]-'a'] = true;
19+
r.push_back(s[i]);
20+
}
21+
return r;
22+
}
23+
};
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Shortest Distance from All Buildings
2+
typedef pair<int, int> pii;
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
int shortestDistance(vector<vector<int>>& grid) {
8+
int m = grid.size(), n = grid[0].size(), ans = INT_MAX;
9+
vector<vector<int>> ds(m, vector<int>(n, 0)), d(m, vector<int>(n));
10+
REP(i, m)
11+
REP(j, n)
12+
if (grid[i][j] == 1) {
13+
queue<pii> q;
14+
q.emplace(i, j);
15+
REP(ii, m)
16+
fill_n(d[ii].begin(), n, -1);
17+
d[i][j] = 0;
18+
while (! q.empty()) {
19+
int x, y;
20+
tie(x, y) = q.front();
21+
q.pop();
22+
if (! grid[x][y] || x == i && y == j)
23+
REP(k, 4) {
24+
int xx = x+((int[]){-1,0,1,0})[k], yy = y+((int[]){0,1,0,-1})[k];
25+
if (unsigned(xx) < m && unsigned(yy) < n && d[xx][yy] < 0) {
26+
d[xx][yy] = d[x][y]+1;
27+
q.emplace(xx, yy);
28+
}
29+
}
30+
}
31+
REP(ii, m)
32+
REP(jj, n)
33+
ds[ii][jj] = ds[ii][jj] < 0 || d[ii][jj] < 0 ? -1 : ds[ii][jj]+d[ii][jj];
34+
}
35+
REP(i, m)
36+
REP(j, n)
37+
if (! grid[i][j] && ds[i][j] >= 0)
38+
ans = min(ans, ds[i][j]);
39+
return ans == INT_MAX ? -1 : ans;
40+
}
41+
};

0 commit comments

Comments
 (0)