Skip to content

Commit 0eed2b1

Browse files
committed
Updated 4 solutions
1 parent 887ba7d commit 0eed2b1

File tree

6 files changed

+150
-86
lines changed

6 files changed

+150
-86
lines changed
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
package _1_7_Rotate_Matrix;
22

33
public class RotateMatrix {
4-
/* Rotates square image[][] 90 degrees clockwise */
4+
private static int n;
5+
56
public static void rotate90clockwise(int[][] image) {
6-
int n = image.length;
7-
for (int layer = 0; layer < n/2; layer++) {
8-
int first = layer;
9-
int last = n - 1 - layer;
10-
for (int i = first; i < last; i++) {
11-
int offset = i - first;
12-
/* 4-way swap */
13-
int temp = image[first][first + offset];
14-
image[first][first + offset] = image[last - offset][first];
15-
image[last - offset][first] = image[last][last - offset];
16-
image[last][last - offset] = image[first + offset][last];
17-
image[first + offset][last] = temp;
7+
if (image == null || image.length != image[0].length) {
8+
return;
9+
}
10+
n = image.length;
11+
transpose(image);
12+
flipHorizontally(image);
13+
}
14+
15+
private static void transpose(int[][] image) {
16+
for (int row = 0; row < n; row++) {
17+
for (int col = row + 1; col < n; col++) {
18+
swap(image, row, col, col, row);
19+
}
20+
}
21+
}
22+
23+
private static void flipHorizontally(int[][] image) {
24+
for (int row = 0; row < n; row++) {
25+
for (int col = 0; col < n / 2; col++) {
26+
swap(image, row, col, row, n - 1 - col);
1827
}
1928
}
2029
}
2130

22-
/* For fun: I tried rotating image by 180 degrees */
31+
// Swaps 2 elements in a 2-D array
32+
private static void swap(int[][] image, int r1, int c1, int r2, int c2) {
33+
int temp = image[r1][c1];
34+
image[r1][c1] = image[r2][c2];
35+
image[r2][c2] = temp;
36+
}
37+
38+
// For fun: I tried rotating image by 180 degrees
2339
public static int[][] rotate180(int[][] image) {
2440
int n = image.length;
2541
for (int row = 0; row < n/2; row++) {
@@ -36,15 +52,8 @@ public static int[][] rotate180(int[][] image) {
3652
}
3753
return image;
3854
}
39-
40-
/* Swaps 2 elements in a 2-D array */
41-
private static void swap(int[][] image, int r1, int c1, int r2, int c2) {
42-
int temp = image[r1][c1];
43-
image[r1][c1] = image[r2][c2];
44-
image[r2][c2] = temp;
45-
}
4655
}
4756

4857
// Time Complexity: O(n^2). Impossible to do better since must touch all n^2 elements.
4958
// Space Complexity: O(1)
50-
// Time/Space complexities are the same for both the 90-degree rotation and 180-degree rotation.
59+
// Time/Space complexities are the same for both the 90-degree rotation and 180-degree rotation.

Chp. 08 - Recursion and Dynamic Programming/_8_01_Triple_Step/Tester.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
public class Tester {
44
public static void main(String[] args) {
55
System.out.println("*** Test 8.1: Triple Step\n");
6-
test(3);
7-
test(5);
6+
test(0); System.out.println();
7+
test(1); System.out.println();
8+
test(2); System.out.println();
9+
test(3); System.out.println();
10+
test(5); System.out.println();
811
}
912

10-
private static void test(int steps) {
11-
System.out.format("(Iterative Solution) %d Step Staircase: %2d ways \n", steps, TripleStep.numPathsIterative(steps));
13+
private static void test(int steps) {
1214
System.out.format("(Recursive Solution) %d Step Staircase: %2d ways \n", steps, TripleStep.numPathsRecursive(steps));
15+
System.out.format("(Iterative Solution) %d Step Staircase: %2d ways \n", steps, TripleStep.numPathsIterative(steps));
16+
System.out.format("(Iterative Solution) %d Step Staircase: %2d ways \n", steps, TripleStep.numPathsIterativeNoArray(steps));
1317
}
1418
}

Chp. 08 - Recursion and Dynamic Programming/_8_01_Triple_Step/TripleStep.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,65 @@
44
// --------------------------------------------------------
55
// 1) Recursive O(n) O(n)
66
// 2) Iterative using array O(n) O(n)
7-
// 3) Iterative, no array O(n) O(1) Favorite
7+
// 3) Iterative, no array O(n) O(1) Favorite
88

99
public class TripleStep {
10-
private final static int staircaseSize = 100;
11-
private static int[] cache = new int[staircaseSize];
1210

13-
/* Recursive Solution */
14-
public static int numPathsRecursive(int steps) {
15-
if (steps > staircaseSize) {
16-
return -1;
17-
}
18-
cache[0] = 1; // setting the value to 1 and not to 0 is crucial
19-
return numPathsRecursive_helper(steps);
11+
// Recursive Solution
12+
public static int numPathsRecursive(int n) {
13+
int[] cache = new int[n + 1];
14+
return numPathsRecursive(n, cache);
2015
}
2116

22-
private static int numPathsRecursive_helper(int steps) {
23-
if (steps < 0) {
17+
private static int numPathsRecursive(int n, int[] cache) {
18+
if (n < 0) { // this can happen due to our recursive calls
2419
return 0;
20+
} else if (n == 0 || n == 1) {
21+
return 1;
2522
}
26-
if (cache[steps] > 0) {
27-
return cache[steps];
23+
if (cache[n] > 0) {
24+
return cache[n];
2825
}
29-
cache[steps] = numPathsRecursive_helper(steps - 1)
30-
+ numPathsRecursive_helper(steps - 2)
31-
+ numPathsRecursive_helper(steps - 3);
26+
cache[n] = numPathsRecursive(n - 1, cache)
27+
+ numPathsRecursive(n - 2, cache)
28+
+ numPathsRecursive(n - 3, cache);
3229

33-
return cache[steps];
30+
return cache[n];
3431
}
3532

36-
/* Iterative Solution */
37-
public static int numPathsIterative(int staircaseSize) {
38-
int[] cache = new int[staircaseSize + 1];
33+
// Iterative Solution
34+
public static int numPathsIterative(int n) {
35+
if (n == 0 || n == 1) {
36+
return 1;
37+
} else if (n == 2) {
38+
return 2;
39+
}
40+
int[] cache = new int[n + 1];
3941
cache[0] = 1;
4042
cache[1] = 1;
4143
cache[2] = 2;
42-
for (int i = 3; i <= staircaseSize; i++) {
44+
for (int i = 3; i <= n; i++) {
4345
cache[i] = cache[i - 3] + cache[i - 2] + cache[i - 1];
4446
}
45-
return cache[staircaseSize];
47+
return cache[n];
4648
}
4749

48-
/*
49-
* Solution 3: Can be done in O(1) space by saving just the last 3 elements of
50-
* array, like in Fibonacci solution
51-
*/
50+
public static int numPathsIterativeNoArray(int n) {
51+
if (n == 0 || n == 1) {
52+
return 1;
53+
} else if (n == 2) {
54+
return 2;
55+
}
56+
int prev2 = 1;
57+
int prev = 1;
58+
int curr = 2;
59+
int next = 0;
60+
for (int i = 3; i <= n; i++) {
61+
next = prev2 + prev + curr;
62+
prev2 = prev;
63+
prev = curr;
64+
curr = next;
65+
}
66+
return curr;
67+
}
5268
}

Chp. 08 - Recursion and Dynamic Programming/__Intro_Fibonacci/Fibonacci.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,55 @@
1212

1313
public class Fibonacci {
1414

15-
/* Solution 1 */
16-
private final static int cacheSize = 100;
17-
public static int[] cache = new int[cacheSize];
18-
15+
// Solution 1
1916
public static int fibRecursive(int n) {
20-
if (n < 0) {
21-
return -1;
22-
}
23-
cache[0] = 1; // We are defining 0th Fibonacci number as 1
24-
cache[1] = 1;
25-
return fibRecursiveHelper(n);
17+
int[] cache = new int[n + 1];
18+
return fibRecursive(n, cache);
2619
}
2720

28-
public static int fibRecursiveHelper(int n) {
21+
private static int fibRecursive(int n, int[] cache) {
22+
if (n <= 0) {
23+
return 0;
24+
} else if (n == 1) {
25+
return 1;
26+
}
2927
if (cache[n] > 0) {
3028
return cache[n];
3129
}
32-
cache[n] = fibRecursiveHelper(n - 1) + fibRecursiveHelper(n - 2);
30+
cache[n] = fibRecursive(n - 1, cache)
31+
+ fibRecursive(n - 2, cache);
32+
3333
return cache[n];
3434
}
3535

36-
/* Solution 2 - omitted (but very similar to Solution 3) */
36+
// Solution 2
37+
public static int fibIterative(int n) {
38+
if (n <= 0) {
39+
return 0;
40+
} else if (n == 1) {
41+
return 1;
42+
}
43+
int[] cache = new int[n + 1];
44+
cache[0] = 0;
45+
cache[1] = 1;
46+
for (int i = 2; i <= n; i++) {
47+
cache[i] = cache[i - 2] + cache[i - 1];
48+
}
49+
return cache[n];
50+
}
3751

38-
/* Solution 3 */
39-
public static int fibIterative(int n) { // assumes 1st and 2nd Fibonacci numbers are defined as value 1.
52+
// Solution 3
53+
public static int fibIterativeNoArray(int n) {
54+
if (n == 0) {
55+
return 0;
56+
} else if (n == 1) {
57+
return 1;
58+
}
4059
int prev = 0;
4160
int curr = 1;
61+
int next = 0;
4262
for (int i = 2; i <= n; i++) {
43-
int next = prev + curr;
63+
next = prev + curr;
4464
prev = curr;
4565
curr = next;
4666
}

Chp. 08 - Recursion and Dynamic Programming/__Intro_Fibonacci/Tester.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ public static void main(String[] args) {
66
for (int i = 0; i < 16; i++) {
77
System.out.print(Fibonacci.fibRecursive(i) + " ");
88
}
9+
910
System.out.println();
10-
for (int i = 1; i < 16; i++) {
11+
for (int i = 0; i < 16; i++) {
1112
System.out.print(Fibonacci.fibIterative(i) + " ");
1213
}
14+
15+
System.out.println();
16+
for (int i = 0; i < 16; i++) {
17+
System.out.print(Fibonacci.fibIterativeNoArray(i) + " ");
18+
}
1319
}
1420
}
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
package _17_21_Volume_of_Histogram;
22

3-
// Main idea: For each index, the tallest wall anywhere to the left, and to the right, determine
4-
// the amount of water the index will hold. We can calculate this iteratively.
3+
// Main idea: For each index, 2 walls (the tallest wall anywhere to the left, and to the right), together
4+
// determine the amount of water the index will hold. We can calculate this iteratively.
55

66
public class VolumeOfHistogram {
7-
public static int computeHistogramVolume(int[] histo) {
8-
/* Calculate left maxes */
9-
int[] leftMax = new int[histo.length];
10-
leftMax[0] = histo[0];
11-
for (int i = 1; i < histo.length; i++) {
12-
leftMax[i] = Math.max(leftMax[i-1], histo[i]);
7+
public static int computeHistogramVolume(int[] height) {
8+
if (height == null || height.length <= 1) {
9+
return 0;
1310
}
11+
int size = height.length;
1412

15-
/* Calculate right maxes, along with "min" and "sum" */
16-
int sum = 0;
17-
int rightMax = 0;
18-
for (int i = histo.length - 1; i >= 0; i--) {
19-
rightMax = Math.max(rightMax, histo[i]);
20-
sum += Math.min(leftMax[i], rightMax) - histo[i];
13+
// Calculate left maxes
14+
int[] leftMax = new int[size];
15+
leftMax[0] = height[0];
16+
for (int i = 1; i < size; i++) {
17+
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
2118
}
22-
return sum;
19+
20+
// Calculate right maxes
21+
int[] rightMax = new int[size];
22+
rightMax[size - 1] = height[size - 1];
23+
for (int i = size - 2; i >= 0; i--) {
24+
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
25+
}
26+
27+
// Calculate amount of water that can be stored above each spot on histogram
28+
int water = 0;
29+
for (int i = 1; i < size - 1; i++) {
30+
water += Math.min(leftMax[i], rightMax[i]) - height[i];
31+
}
32+
return water;
2333
}
2434
}
2535

26-
2736
// Time Complexity: O(n)
2837
// Space Complexity: O(n)

0 commit comments

Comments
 (0)