Skip to content

Commit a58e10d

Browse files
committed
refactoring
1 parent 1ecf4aa commit a58e10d

File tree

4 files changed

+149
-53
lines changed

4 files changed

+149
-53
lines changed

LargestRectangleinHistogram/LargestRectangleinHistogram.cpp

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,73 @@ using namespace std;
1616

1717
class Solution {
1818
public:
19-
int largestRectangleArea(vector<int> &height) {
20-
int n = height.size();
21-
int y[n];
22-
stack<int> stk;
23-
for (int i = 0; i < n; i++) {
24-
while (!stk.empty()) {
25-
if (height[i] <= height[stk.top()]) stk.pop();
26-
else break;
27-
}
28-
int j = (stk.empty()) ? -1 : stk.top();
29-
// Calculating number of bars on the left
30-
y[i] = i - j - 1;
31-
stk.push(i);
32-
}
33-
34-
while (!stk.empty()) stk.pop();
35-
36-
for (int i = n - 1; i > 0; i--) {
37-
while (!stk.empty()) {
38-
if (height[i] <= height[stk.top()]) stk.pop();
39-
else break;
40-
}
41-
int j = (stk.empty()) ? n : stk.top();
42-
// Calculating number of bars on the left + right
43-
y[i] += (j - i - 1);
44-
stk.push(i);
45-
}
46-
47-
int res = 0;
48-
for (int i = 0; i < n; i++) {
49-
// Calculating height * width
50-
y[i] = height[i] * (y[i] + 1);
51-
if (y[i] > res) res = y[i];
52-
}
53-
return res;
19+
int largestRectangleArea(vector<int> &height)
20+
{
21+
return largestRectangleArea2(height);
22+
}
23+
24+
// based on brute-force, takes O(n^2) time
25+
int largestRectangleArea1(vector<int> &height)
26+
{
27+
int n = height.size();
28+
int area[n];
29+
int res = 0;
30+
for (int i = 0; i < n; i++)
31+
{
32+
int j = i;
33+
while (j >= 0 && height[j] >= height[i]) j--;
34+
area[i] = i-j-1;
35+
j = i;
36+
while (j < n && height[j] >= height[i]) j++;
37+
area[i] += (j-i-1);
38+
area[i] = height[i] * (area[i]+1);
39+
if (area[i] > res) res = area[i];
40+
}
41+
return res;
42+
}
43+
44+
// based on stack, takes O(n) time
45+
int largestRectangleArea2(vector<int> &height)
46+
{
47+
int n = height.size();
48+
int area[n];
49+
stack<int> stk;
50+
for (int i = 0; i < n; i++)
51+
{
52+
while (!stk.empty() && height[i] <= height[stk.top()]) stk.pop();
53+
int j = (stk.empty()) ? -1 : stk.top();
54+
// Calculating number of bars on the left
55+
area[i] = i - j - 1;
56+
stk.push(i);
57+
}
58+
59+
while (!stk.empty()) stk.pop();
60+
61+
for (int i = n - 1; i >= 0; i--)
62+
{
63+
while (!stk.empty() && height[i] <= height[stk.top()]) stk.pop();
64+
int j = (stk.empty()) ? n : stk.top();
65+
// Calculating number of bars on the left + right
66+
area[i] += (j - i - 1);
67+
stk.push(i);
68+
}
69+
70+
int res = 0;
71+
for (int i = 0; i < n; i++)
72+
{
73+
// Calculating height * width
74+
area[i] = height[i] * (area[i] + 1);
75+
if (area[i] > res) res = area[i];
76+
}
77+
return res;
5478
}
5579
};
5680

57-
int main() {
58-
return 0;
81+
int main()
82+
{
83+
int x[] = {2,1,5,6,2,3};
84+
vector<int> height(x, x + sizeof(x)/sizeof(int));
85+
Solution sol;
86+
cout << sol.largestRectangleArea(height) << endl;
87+
return 0;
5988
}

MaximalRectangle/MaximalRectangle.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,49 @@ using namespace std;
1212
class Solution {
1313
public:
1414
int maximalRectangle(vector<vector<char> > &matrix) {
15+
return maximalRectangle2(matrix);
16+
}
17+
18+
// based on dynamic programming, takes O(n^3) time
19+
int maximalRectangle1(vector<vector<char> > &matrix) {
20+
int M = matrix.size();
21+
if (M == 0) return 0;
22+
int N = matrix[0].size();
23+
if (N == 0) return 0;
24+
int dp[M][N];
25+
fill(&dp[0][0], &dp[M][0], 0);
26+
27+
for (int i = 0; i < M; i++) {
28+
for (int j = 0; j < N; j++) {
29+
if (matrix[i][j] == '1') {
30+
dp[i][j] = (j > 0) ? (dp[i][j-1] + 1) : 1;
31+
}
32+
}
33+
}
34+
35+
int res = 0;
36+
for (int i = 0; i < M; i++) {
37+
for (int j = 0; j < N; j++) {
38+
int min = dp[i][j];
39+
int k = i;
40+
while (k >= 0) {
41+
if (dp[k][j] < min) min = dp[k][j];
42+
res = max(res, min * (i - k + 1));
43+
k--;
44+
}
45+
}
46+
}
47+
return res;
48+
}
49+
50+
// based on stack, takes O(n^2) time
51+
int maximalRectangle2(vector<vector<char> > &matrix) {
1552
int M = matrix.size();
1653
if (M == 0) return 0;
1754
int N = matrix[0].size();
1855
if (N == 0) return 0;
1956
int h[N];
20-
memset(h, 0, sizeof(int)*N);
57+
fill(h, h + N, 0);
2158
int res = 0;
2259
for (int i = 0; i < M; i++) {
2360
for (int j = 0; j < N; j++) {

SearchforaRange/SearchforaRange.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,47 @@
1414

1515
#include <iostream>
1616
#include <vector>
17-
#include <algorithm>
1817
using namespace std;
1918

2019
class Solution {
2120
public:
2221
vector<int> searchRange(int A[], int n, int target) {
23-
int l = lower_bound(A, A + n, target) - A;
24-
int u = upper_bound(A, A + n, target) - A - 1;
22+
int l = lower_bound(A, n, target);
23+
int u = upper_bound(A, n, target) - 1;
2524
vector<int> res(2, -1);
2625
if (l > u) return res;
2726
res[0] = l;
2827
res[1] = u;
2928
return res;
3029
}
30+
31+
int lower_bound(int A[], int n, int target) {
32+
int l = 0;
33+
int u = n - 1;
34+
while (l <= u) {
35+
int m = l + (u - l) / 2;
36+
if (A[m] >= target) u = m - 1;
37+
else l = m + 1;
38+
}
39+
return u + 1;
40+
}
41+
42+
int upper_bound(int A[], int n, int target) {
43+
int l = 0;
44+
int u = n - 1;
45+
while (l <= u) {
46+
int m = l + (u - l) / 2;
47+
if (A[m] <= target) l = m + 1;
48+
else u = m - 1;
49+
}
50+
return l;
51+
}
3152
};
3253

3354
int main() {
34-
return 0;
55+
int x[] = {5, 7, 7, 8, 8, 10};
56+
Solution sol;
57+
vector<int> res = sol.searchRange(x, sizeof(x)/sizeof(int), 8);
58+
cout << res[0] << "," << res[1] << endl;
59+
return 0;
3560
}

ValidPalindrome/ValidPalindrome.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//============================================================================
2-
// Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2+
// Given a string, determine if it is a palindrome, considering only
3+
// alphanumeric characters and ignoring cases.
34
//
45
// For example,
56
//
@@ -15,21 +16,23 @@ class Solution {
1516
public:
1617
bool isPalindrome(string s) {
1718
int begin = 0, end = s.size()-1;
18-
while (begin < end) {
19-
if (!isAlphanumeric(s[begin])) {
19+
while (begin < end)
20+
{
21+
if (!isAlphanumeric(s[begin]))
22+
{
2023
begin++;
2124
continue;
2225
}
23-
26+
2427
if (!isAlphanumeric(s[end]))
2528
{
2629
end--;
2730
continue;
2831
}
29-
32+
3033
int val = s[begin] - s[end];
31-
// cout << s[begin] << "," << s[end] << val << endl;
32-
if (val != 0 && abs(val) != 32) {
34+
if (val != 0 && abs(val) != 32)
35+
{
3336
return false;
3437
}
3538
begin++;
@@ -38,14 +41,16 @@ class Solution {
3841
return true;
3942
}
4043

41-
bool isAlphanumeric(char c) {
44+
bool isAlphanumeric(char c)
45+
{
4246
return ((c >= 'a' && c <= 'z') ||
43-
(c >= 'A' && c <= 'Z') ||
44-
(c >= '0' && c <= '9'));
47+
(c >= 'A' && c <= 'Z') ||
48+
(c >= '0' && c <= '9'));
4549
}
4650
};
4751

48-
int main() {
52+
int main()
53+
{
4954
Solution sol;
5055
cout << sol.isPalindrome("1b2") << endl;
5156
return 0;

0 commit comments

Comments
 (0)