Skip to content

Commit e6917d7

Browse files
committed
refine the comments
1 parent e5b6946 commit e6917d7

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

algorithms/largestRectangleInHistogram/largestRectangleInHistogram.cpp

+47-15
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,52 @@
33
// Date : 2014-07-20
44

55
/**********************************************************************************
6-
*
7-
* Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
8-
* find the area of largest rectangle in the histogram.
9-
*
10-
* Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
11-
*
12-
* The largest rectangle is shown in the shaded area, which has area = 10 unit.
13-
*
14-
* For example,
15-
* Given height = [2,1,5,6,2,3],
16-
* return 10.
17-
*
18-
*
19-
**********************************************************************************/
6+
*
7+
* Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
8+
* find the area of largest rectangle in the histogram.
9+
*
10+
* 6
11+
* +---+
12+
* 5 | |
13+
* +---+ |
14+
* | | |
15+
* | | |
16+
* | | | 3
17+
* | | | +---+
18+
* 2 | | | 2 | |
19+
* +---+ | | +---+ |
20+
* | | 1 | | | | |
21+
* | +---+ | | | |
22+
* | | | | | | |
23+
* +---+---+---+---+---+---+
24+
*
25+
* Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
26+
*
27+
*
28+
* 6
29+
* +---+
30+
* 5 | |
31+
* +-------|
32+
* |-------|
33+
* |-------|
34+
* |-------| 3
35+
* |-------| +---+
36+
* 2 |-------| 2 | |
37+
* +---+ |-------|---+ |
38+
* | | 1 |-------| | |
39+
* | +---|-------| | |
40+
* | | |-------| | |
41+
* +---+---+---+---+---+---+
42+
*
43+
*
44+
* The largest rectangle is shown in the shaded area, which has area = 10 unit.
45+
*
46+
* For example,
47+
* Given height = [2,1,5,6,2,3],
48+
* return 10.
49+
*
50+
*
51+
**********************************************************************************/
2052

2153
#include <iostream>
2254
#include <vector>
@@ -132,7 +164,7 @@ void test(int a[], int n)
132164

133165
int main()
134166
{
135-
#define TEST(a) test(a, sizeof(a)/sizeof(int))
167+
#define TEST(a) test(a, sizeof(a)/sizeof(int))
136168

137169
int a0[] = {2,1,3,1};
138170
TEST(a0);

algorithms/uniquePaths/uniquePaths.II.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <vector>
3030
using namespace std;
3131

32+
//As same as DP solution with "Unique Path I", just need to consider the obstacles.
3233
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
3334
vector< vector<int> > v = obstacleGrid;
3435
unsigned int max=0;

algorithms/uniquePaths/uniquePaths.cpp

+37-14
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,48 @@
33
// Date : 2014-06-25
44

55
/**********************************************************************************
6-
*
7-
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
8-
*
9-
* The robot can only move either down or right at any point in time. The robot is trying to reach
10-
* the bottom-right corner of the grid (marked 'Finish' in the diagram below).
11-
*
12-
* How many possible unique paths are there?
13-
*
14-
* Above is a 3 x 7 grid. How many possible unique paths are there?
15-
*
16-
* Note: m and n will be at most 100.
17-
*
18-
**********************************************************************************/
6+
*
7+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
8+
*
9+
* The robot can only move either down or right at any point in time. The robot is trying to reach
10+
* the bottom-right corner of the grid (marked 'Finish' in the diagram below).
11+
*
12+
*
13+
* start  
14+
* +---------+----+----+----+----+----+
15+
* |----| | | | | | |
16+
* |----| | | | | | |
17+
* +----------------------------------+
18+
* | | | | | | | |
19+
* | | | | | | | |
20+
* +----------------------------------+
21+
* | | | | | | |----|
22+
* | | | | | | |----|
23+
* +----+----+----+----+----+---------+
24+
* finish
25+
*
26+
*
27+
* How many possible unique paths are there?
28+
*
29+
* Above is a 3 x 7 grid. How many possible unique paths are there?
30+
*
31+
* Note: m and n will be at most 100.
32+
*
33+
**********************************************************************************/
1934

2035
#include <stdio.h>
2136
#include <stdlib.h>
2237

2338
void printMatrix(int*a, int m, int n);
2439

40+
/*
41+
* Dynamic Programming
42+
*
43+
* We have a dp[i][j] represents how many paths from [0][0] to hear. So, we have the following DP formuler:
44+
*
45+
* dp[i][j] = 1 if i==0 || j==0 //the first row/column only have 1 uniqe path.
46+
* = dp[i-1][j] + dp[i][j-1] //the path can be from my top cell and left cell.
47+
*/
2548
int uniquePaths(int m, int n) {
2649
int* matrix = new int[m*n];
2750
printMatrix(matrix, m, n);
@@ -58,7 +81,7 @@ int main(int argc, char** argv)
5881
m = atoi(argv[1]);
5982
n = atoi(argv[2]);
6083
}
61-
84+
6285
printf("uniquePaths=%d\n", uniquePaths(m,n));
6386
return 0;
6487
}

0 commit comments

Comments
 (0)