Skip to content

Commit eda2579

Browse files
author
turingfly
committed
Recursion and Dynamic Programming
1 parent e60b1dc commit eda2579

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package chapter08RecursionAndDynamicProgramming;
2+
3+
/**
4+
*
5+
* Problem: A magic index in an array A[0...n - 1] is defined to be an index
6+
* such that A[i] = i. Given a sorted array of distinct integers, write a method
7+
* to find a magic index, if one exits, in array A.
8+
*
9+
* Follow up: what is the value are not distinct?
10+
*
11+
*/
12+
public class MagicIndex {
13+
14+
public int magic(int[] array) {
15+
return magic(array, 0, array.length - 1);
16+
}
17+
18+
private int magic(int[] array, int left, int right) {
19+
if (right < left) {
20+
return -1;
21+
}
22+
int mid = left + (right - left) / 2;
23+
if (array[mid] == mid) {
24+
return mid;
25+
} else if (array[mid] > mid) {
26+
return magic(array, left, mid - 1);
27+
} else {
28+
return magic(array, mid + 1, right);
29+
}
30+
}
31+
32+
// follow up
33+
public int magic2(int[] array) {
34+
return magic2(array, 0, array.length - 1);
35+
}
36+
37+
private int magic2(int[] array, int left, int right) {
38+
if (right < left) {
39+
return -1;
40+
}
41+
int mid = left + (right - left) / 2;
42+
int midVal = array[mid];
43+
if (midVal == mid) {
44+
return mid;
45+
}
46+
47+
// search left
48+
int leftIndex = Math.min(mid - 1, midVal);
49+
int l = magic2(array, left, leftIndex);
50+
if (l >= 0) {
51+
return l;
52+
}
53+
54+
// search right
55+
int rightIndex = Math.max(mid + 1, midVal);
56+
int r = magic2(array, rightIndex, right);
57+
return r;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,115 @@
11
package chapter08RecursionAndDynamicProgramming;
22

3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
38
/**
49
*
5-
* Problem:
10+
* Problem: Imagine a robot sitting on the upper left corner of grid with r rows
11+
* and c columns. The robot can only move in two directions, right and down, but
12+
* certain cells are "off limits" such that the robot cannot step on them.
13+
* Design an algorithm to find a path for the robot from the top left to the
14+
* bottom right.
615
*
716
*/
817
public class RobotInAGrid {
918

19+
/**
20+
* Method 1: Duplicate work
21+
*
22+
* Time Complexity: O(2(r + c)). each path has r + c steps and there are two
23+
* choices we can make at each step.
24+
*/
25+
26+
public List<Point> getPath1(boolean[][] maze) {
27+
if (maze == null || maze.length == 0) {
28+
return null;
29+
}
30+
List<Point> res = new ArrayList<>();
31+
if (getPath1(maze, maze.length - 1, maze[0].length - 1, res)) {
32+
return res;
33+
}
34+
return null;
35+
}
36+
37+
private boolean getPath1(boolean[][] maze, int row, int col, List<Point> path) {
38+
// out of bounds or not available
39+
if (!maze[row][col] || col < 0 || row < 0) {
40+
return false;
41+
}
42+
boolean isAtOrigin = (row == 0) && (col == 0);
43+
if (getPath1(maze, row, col - 1, path) || getPath1(maze, row - 1, col, path) || isAtOrigin) {
44+
Point p = new Point(row, col);
45+
path.add(p);
46+
return true;
47+
}
48+
return false;
49+
}
50+
51+
/**
52+
* Method 2:
53+
*
54+
* Time Complexity: O(rc), we hit each cell just once.
55+
*/
56+
public List<Point> getPath2(boolean[][] maze) {
57+
if (maze == null || maze.length == 0) {
58+
return null;
59+
}
60+
List<Point> res = new ArrayList<>();
61+
Set<Point> failedPoints = new HashSet<>();
62+
if (getPath2(maze, maze.length - 1, maze[0].length - 1, res, failedPoints)) {
63+
return res;
64+
}
65+
return null;
66+
}
67+
68+
private boolean getPath2(boolean[][] maze, int row, int col, List<Point> path, Set<Point> failedPoints) {
69+
// out of bounds or not available
70+
if (!maze[row][col] || col < 0 || row < 0) {
71+
return false;
72+
}
73+
Point p = new Point(row, col);
74+
if (failedPoints.contains(p)) {
75+
return false;
76+
}
77+
boolean isAtOrigin = (row == 0) && (col == 0);
78+
if (getPath2(maze, row, col - 1, path, failedPoints) || getPath2(maze, row - 1, col, path, failedPoints)
79+
|| isAtOrigin) {
80+
path.add(p);
81+
return true;
82+
}
83+
return false;
84+
}
85+
1086
}
87+
88+
class Point {
89+
int row;
90+
int col;
91+
92+
public Point(int row, int col) {
93+
this.row = row;
94+
this.col = col;
95+
}
96+
97+
@Override
98+
public String toString() {
99+
return "(" + row + "," + col + ")";
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
return this.toString().hashCode();
105+
}
106+
107+
@Override
108+
public boolean equals(Object o) {
109+
if ((o instanceof Point) && ((Point) o).row == this.row && ((Point) o).col == this.col) {
110+
return true;
111+
} else {
112+
return false;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)