Skip to content

Commit 52b74e9

Browse files
committed
Add
1 parent 3d64419 commit 52b74e9

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

JumpGameII.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*
3+
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
4+
*
5+
* Each element in the array represents your maximum jump length at that position.
6+
*
7+
* Your goal is to reach the last index in the minimum number of jumps.
8+
*
9+
* For example:
10+
*
11+
* Given array A = [2,3,1,1,4]
12+
*
13+
* The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
14+
*/
15+
16+
public class JumpGameII {
17+
public int jump(int[] A) {
18+
int ret = 0;
19+
int last = 0;
20+
int curr = 0;
21+
for (int i = 0; i < A.length; ++i) {
22+
if (i > last) {
23+
last = curr;
24+
++ret;
25+
}
26+
curr = Math.max(curr, i + A[i]);
27+
}
28+
return ret;
29+
}
30+
}

NQueensII.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Follow up for N-Queens problem.
3+
*
4+
* Now, instead outputting board configurations, return the total number of distinct solutions.
5+
*
6+
*/
7+
8+
public class NQueensII {
9+
public int solveNQueens(int n) {
10+
if (n == 0)
11+
return 0;
12+
StringBuffer line = new StringBuffer();
13+
for (int i = 0; i < n; i++) {
14+
line.append('.');
15+
}
16+
StringBuffer[] sol = new StringBuffer[n];
17+
for (int i = 0; i < n; i++) {
18+
sol[i] = new StringBuffer(line.toString());
19+
}
20+
boolean[] cols = new boolean[n];
21+
int[] row = new int[n];
22+
int[] count = new int[1];
23+
findSolutions(n, 0, sol, row, cols, count);
24+
return count[0];
25+
}
26+
27+
private void findSolutions(int n, int start, StringBuffer[] sol, int[] row,
28+
boolean[] cols, int[] count) {
29+
if (start == n) {
30+
count[0]++;
31+
} else {
32+
for (int i = 0; i < n; i++) {
33+
if (cols[i])
34+
continue;
35+
boolean ok = true;
36+
for (int j = 0; j < start; j++) {
37+
if (Math.abs(start - j) == Math.abs(i - row[j])) {
38+
ok = false;
39+
break;
40+
}
41+
}
42+
if (ok) {
43+
cols[i] = true;
44+
sol[start].setCharAt(i, 'Q');
45+
row[start] = i;
46+
findSolutions(n, start + 1, sol, row, cols, count);
47+
row[start] = 0;
48+
sol[start].setCharAt(i, '.');
49+
cols[i] = false;
50+
}
51+
}
52+
}
53+
}
54+
}

TrappingRainWater.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Given n non-negative integers representing an elevation map where the width
3+
* of each bar is 1, compute how much water it is able to trap after raining.
4+
*
5+
* For example,
6+
*
7+
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
8+
*
9+
* The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In
10+
* this case, 6 units of rain water (blue section) are being trapped. Thanks
11+
* Marcos for contributing this image!
12+
*
13+
*/
14+
15+
public class TrappingRainWater {
16+
public int trap(int A[], int n) {
17+
if (n <= 2)
18+
return 0;
19+
20+
int[] lmh = new int[n];
21+
lmh[0] = 0;
22+
int maxh = A[0];
23+
for (int i = 1; i < n; ++i) {
24+
lmh[i] = maxh;
25+
if (maxh < A[i])
26+
maxh = A[i];
27+
}
28+
int trapped = 0;
29+
maxh = A[n - 1];
30+
for (int i = n - 2; i > 0; --i) {
31+
int left = lmh[i];
32+
int right = maxh;
33+
int container = Math.min(left, right);
34+
if (container > A[i]) {
35+
trapped += container - A[i];
36+
}
37+
if (maxh < A[i])
38+
maxh = A[i];
39+
}
40+
return trapped;
41+
}
42+
}

0 commit comments

Comments
 (0)