Skip to content

Commit 4387490

Browse files
committed
Contains Duplicate III & Maximal Square
1 parent 0c6137a commit 4387490

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
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 194/203 problems.
5+
Solved 196/205 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|221|[Maximal Square](https://leetcode.com/problems/maximal-square/)|[maximal-square.cc](maximal-square.cc)|
16+
|220|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)|[contains-duplicate-iii.cc](contains-duplicate-iii.cc)|
1517
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[contains-duplicate-ii.cc](contains-duplicate-ii.cc)|
1618
|218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)|[the-skyline-problem.cc](the-skyline-problem.cc)|
1719
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)|[contains-duplicate.cc](contains-duplicate.cc)|

contains-duplicate-iii.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Contains Duplicate III
2+
class Solution {
3+
public:
4+
bool containsNearbyAlmostDuplicate(vector<int> &a, int k, int t) {
5+
typedef long long ll;
6+
if (k < 1 || t < 0) return false;
7+
unordered_map<ll, ll> b;
8+
for (vector<int>::size_type i = 0; i < a.size(); i++) {
9+
ll x = a[i], j = (x-INT_MIN)/(t+1LL);
10+
if (b.count(j) || b.count(j-1) && x-b[j-1] <= t || b.count(j+1) && b[j+1]-x <= t)
11+
return true;
12+
if (i >= k)
13+
b.erase((ll(a[i-k])-INT_MIN)/(t+1LL));
14+
b[j] = x;
15+
}
16+
return false;
17+
}
18+
};

maximal-rectangle.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ class Solution {
1010
int maximalRectangle(vector<vector<char> > &a) {
1111
if (a.empty()) return 0;
1212
int m = a.size(), n = a[0].size(), ans = 0;
13-
vector<int> h(n), l(n), r(n, n-1);
13+
vector<int> h(n, 0), l(n, 0), r(n, n-1);
1414
REP(i, m) {
15-
int ll = -1;
16-
REP(j, n) {
17-
h[j] = a[i][j] == '1' ? h[j]+1 : 0;
18-
if (a[i][j] == '0') ll = j;
19-
l[j] = h[j] ? max(h[j] == 1 ? 0 : l[j], ll+1) : j;
20-
}
21-
int rr = n;
22-
ROF(j, 0, n) {
23-
if (a[i][j] == '0') rr = j;
24-
r[j] = h[j] ? min(h[j] == 1 ? n-1 : r[j], rr-1) : j;
25-
ans = max(ans, (r[j]-l[j]+1)*h[j]);
26-
}
15+
int ll = 0, rr = n-1;
16+
REP(j, n)
17+
if (a[i][j] == '1')
18+
l[j] = max(l[j], ll);
19+
else {
20+
l[j] = 0;
21+
ll = j+1;
22+
}
23+
ROF(j, 0, n)
24+
if (a[i][j] == '1') {
25+
h[j]++;
26+
r[j] = min(r[j], rr);
27+
ans = max(ans, (r[j]-l[j]+1)*h[j]);
28+
} else {
29+
h[j] = 0;
30+
r[j] = n-1;
31+
rr = j-1;
32+
}
2733
}
2834
return ans;
2935
}

maximal-square.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Maximal Square
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) for (int i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
int maximalSquare(vector<vector<char>> &a) {
8+
if (a.empty()) return 0;
9+
int m = a.size(), n = a[0].size(), ans = 0;
10+
vector<int> s(n, 0);
11+
REP(i, m) {
12+
int ul = 0;
13+
REP(j, n) {
14+
int t = ul;
15+
ul = s[j];
16+
s[j] = a[i][j] == '1' ? min(min(t, s[j-1]), s[j])+1 : 0;
17+
ans = max(ans, s[j]);
18+
}
19+
}
20+
return ans*ans;
21+
}
22+
};

0 commit comments

Comments
 (0)