Skip to content

Commit 9e141c3

Browse files
committed
20181230
1 parent 02b6bdb commit 9e141c3

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

code/lc11.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* 题意:数组下标代表横坐标,数组中的值代表纵坐标,求最大面积
55
* 难度:Medium
66
* 分类:Array, Two Pointers
7-
* Tips:复杂度可以为O(N), 指针往里走, 若值也小了,则面积一定不会增大
7+
* Tips:复杂度可以为O(N), 指针往里走, 若值也小了,则面积一定不会增大。和lc42做比较
88
*/
99
public class lc11 {
1010
public static void main(String[] args) {

code/lc42.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package code;
2+
3+
import java.util.Stack;
4+
25
/*
36
* 42. Trapping Rain Water
47
* 题意:能盛多少水
@@ -11,6 +14,7 @@ public class lc42 {
1114
public static void main(String[] args) {
1215
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
1316
System.out.println(trap(height));
17+
System.out.println(trap2(height));
1418
}
1519
public static int trap(int[] height) {
1620
if(height.length<3)
@@ -40,4 +44,23 @@ public static int trap(int[] height) {
4044
}
4145
return res;
4246
}
47+
48+
public static int trap2(int[] A) {
49+
//栈方法
50+
if (A==null) return 0;
51+
Stack<Integer> s = new Stack<Integer>();
52+
int i = 0, maxWater = 0, maxBotWater = 0;
53+
while (i < A.length){
54+
if (s.isEmpty() || A[i]<=A[s.peek()]){
55+
s.push(i++);
56+
}
57+
else {
58+
int bot = s.pop();
59+
maxBotWater = s.isEmpty()? // empty means no il
60+
0:(Math.min(A[s.peek()],A[i])-A[bot])*(i-s.peek()-1);
61+
maxWater += maxBotWater;
62+
}
63+
}
64+
return maxWater;
65+
}
4366
}

code/lc79.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package code;
2+
/*
3+
* 79. Word Search
4+
* 题意:在字符数组中搜索字符串
5+
* 难度:Medium
6+
* 分类:Array, Backtracking
7+
* 思路:回溯法
8+
* Tips:访问过的格子要标记,不能重复访问。回溯法注意回来的时候要重置标志位。向下找的时候直接找4个方向的,回来的时候不用再找了,只需重置标志位。
9+
*/
10+
public class lc79 {
11+
public static void main(String[] args) {
12+
char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
13+
System.out.println(exist(board,"SEE"));
14+
}
15+
16+
public static boolean exist(char[][] board, String word) {
17+
if(board.length==0)
18+
return false;
19+
boolean [][] flag = new boolean[board.length][board[0].length];
20+
for (int i = 0; i < board.length ; i++) {
21+
for (int j = 0; j < board[0].length ; j++) {
22+
if(search(board,word,i,j,0,flag))
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
29+
public static boolean search(char[][] board, String word, int i, int j, int sum, boolean[][] flag){
30+
if(sum==word.length())
31+
return true;
32+
if(i<0||j<0||i>=board.length||j>=board[0].length)
33+
return false;
34+
if(board[i][j]!=word.charAt(sum)||flag[i][j]==true)
35+
return false;
36+
flag[i][j] = true;
37+
sum++;
38+
boolean r = search(board, word, i, j+1, sum, flag) || search(board, word, i, j-1, sum, flag) || search(board, word, i+1, j, sum, flag) ||search(board, word, i-1, j, sum, flag);
39+
flag[i][j] = false;
40+
return r;
41+
}
42+
}

code/lc84.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package code;
2+
3+
import java.util.Stack;
4+
5+
/*
6+
* 84. Largest Rectangle in Histogram
7+
* 题意:直方图中最大矩形面积
8+
* 难度:Hard
9+
* 分类:Array, Stack
10+
* 思路:两种方法:1.用dp找到边界,再遍历一遍; 2.用栈,栈内存索引,保证栈内索引对应的高度是递增的,若减了即找到了右边界,出栈开始计算。因为栈内是递增的,左边界就是上个栈内的元素。若栈为空,左边界就是-1。
11+
* Tips:和lc42做比较,都可以用栈或者dp来做. 很难,栈的操作很难想到.
12+
*
13+
*/
14+
public class lc84 {
15+
public static void main(String[] args) {
16+
int[] heights = {2,1,5,6,2,3};
17+
System.out.println(largestRectangleArea(heights));
18+
System.out.println(largestRectangleArea2(heights));
19+
}
20+
21+
public static int largestRectangleArea(int[] heights) {
22+
Stack<Integer> st = new Stack();
23+
int res = 0;
24+
for (int i = 0; i <= heights.length ; i++) {
25+
int h = (i == heights.length ? 0 : heights[i]);
26+
if( st.size()==0 || h>heights[st.peek()] ){ //递增入栈,保证栈内索引对应的Height递增
27+
st.push(i);
28+
}else{
29+
int n = st.pop();
30+
int left;
31+
if(st.isEmpty())
32+
left = -1; //若为空,则到最左边
33+
else
34+
left = st.peek();
35+
res = Math.max(res, heights[n] * (i-left-1)); //i之前的,要-1
36+
i--;
37+
}
38+
}
39+
return res;
40+
}
41+
42+
public static int largestRectangleArea2(int[] heights) {
43+
int[] leftMax = new int[heights.length];
44+
int[] rightMax = new int[heights.length];
45+
for (int i = 0; i < heights.length ; i++) { //get leftMax 注意这样dp时数组中保存的边界必须是i-1或i+1,否则无法dp传递
46+
int p = i-1;
47+
while( p>=0 && heights[p]>=heights[i]){
48+
p = leftMax[p];
49+
}
50+
leftMax[i] = p;
51+
}
52+
for (int i = heights.length-1; i >= 0 ; i--) { //get rightMax
53+
rightMax[i] = i;
54+
int p = i+1;
55+
while( p<heights.length && heights[p]>=heights[i]){
56+
p = rightMax[p];
57+
}
58+
rightMax[i] = p;
59+
}
60+
int res = 0;
61+
for (int i = 0; i < heights.length ; i++) {
62+
res = Math.max(res,(rightMax[i]-leftMax[i]-1)*heights[i]);
63+
}
64+
return res;
65+
}
66+
}

code/lc85.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package code;
2+
3+
import java.util.Stack;
4+
5+
/*
6+
* 85. Maximal Rectangle
7+
* 题意:为1的组成的最大矩形面积
8+
* 难度:Hard
9+
* 分类:Array, Hash Table, Dynamic Programming, Stack
10+
* 思路:将问题转化为 84.Largest Rectangle in Histogram
11+
* Tips:很难,lc84就够难了,没写过的谁能想到。。。
12+
*/
13+
public class lc85 {
14+
public static void main(String[] args) {
15+
char[][] matrix = {{'1','0','1','0','0'},
16+
{'1','0','1','1','1'},
17+
{'1','1','1','1','1'},
18+
{'1','0','0','1','0'}};
19+
System.out.println(maximalRectangle(matrix));
20+
}
21+
public static int maximalRectangle(char[][] matrix) {
22+
if(matrix.length==0||matrix[0].length==0)
23+
return 0;
24+
int col = matrix[0].length;
25+
int[] row = new int[col]; // 用来记录直方图高度
26+
int res =0;
27+
for (int i = 0; i < matrix.length; i++) {
28+
for (int j = 0; j < col ; j++) {
29+
if(matrix[i][j]=='0')
30+
row[j] =0;
31+
else
32+
row[j] = row[j]+1;
33+
}
34+
// 求直方图最大面积
35+
Stack<Integer> st = new Stack();
36+
for (int j = 0; j <= col ; j++) {
37+
int h = ( j==col ? 0 : row[j] );
38+
if(st.isEmpty() || h>row[st.peek()]){
39+
st.add(j);
40+
}else{
41+
int index = st.pop();
42+
res = Math.max(res, row[index]*(j- ( st.isEmpty() ? -1 : st.peek() ) -1));
43+
j--;
44+
}
45+
}
46+
}
47+
return res;
48+
}
49+
}

0 commit comments

Comments
 (0)