Skip to content

Commit b6678a1

Browse files
committed
Refactored Chapter 18
1 parent c51cf30 commit b6678a1

30 files changed

+846
-692
lines changed

src/chapter04/GraphFunctions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public static void DFS(GraphNode node, int data){
2929
}
3030
}
3131

32+
/* Don't forget:
33+
* 1) Use a Queue
34+
* 2) .visit() a node before we add it to queue
35+
*/
3236
public static void BFS(GraphNode node, int data){
3337
if (node == null)
3438
return;

src/chapter04/GraphNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* The edges themselves are saved in the GraphNodes as opposed to in their own structure.
77
*/
88
public class GraphNode {
9-
int data;
10-
boolean visited; // crucial for BFS, DFS
9+
public int data;
10+
public boolean visited; // crucial for BFS, DFS
1111
private HashSet<GraphNode> neighbors;
1212

1313
/* Constructor */

src/chapter11/Chapter11.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ public static void test_Sorts(){
2525

2626
System.out.println(Arrays.toString(arrayBubble) + " - Original Array");
2727

28-
Sort.bubbleSort(arrayBubble);
28+
Sorts.bubbleSort(arrayBubble);
2929
System.out.println(Arrays.toString(arrayBubble) + " - BubbleSort");
3030

31-
Sort.selectionSort(arraySelection);
31+
Sorts.selectionSort(arraySelection);
3232
System.out.println(Arrays.toString(arraySelection) + " - SelectionSort");
3333

34-
Sort.bubbleSort(arrayInsertion);
34+
Sorts.bubbleSort(arrayInsertion);
3535
System.out.println(Arrays.toString(arrayInsertion) + " - arrayInsertion");
3636

37-
Sort.mergeSort(arrayMerge);
37+
Sorts.mergeSort(arrayMerge);
3838
System.out.println(Arrays.toString(arrayMerge) + " - MergeSort");
3939

40-
Sort.quickSort(arrayQuick);
40+
Sorts.quickSort(arrayQuick);
4141
System.out.println(Arrays.toString(arrayQuick) + " - QuickSort");
4242
}
4343

src/chapter11/Sort.java renamed to src/chapter11/Sorts.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* 4) Merge Sort O(n log n) O(n)
99
* 5) Quick Sort O(n^2) O(log n) Average Case Runtime: O(n log n)
1010
*/
11-
public class Sort {
11+
public class Sorts {
1212
/***************/
1313
/* Bubble Sort */ // code from Wikipedia
1414
/***************/
@@ -136,7 +136,7 @@ private static void quickSort(int [] array, int start, int end){
136136
* 2) Right side has values larger than pivotValue
137137
* Returns pivotIndex
138138
*/
139-
private static int partition(int [] array, int start, int end){
139+
public static int partition(int [] array, int start, int end){
140140
int pivotIndex = (start + end) / 2; // there are many ways to choose a pivot
141141
int pivotValue = array[pivotIndex];
142142

src/chapter18/Cell.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chapter18;
2+
3+
public class Cell {
4+
public int blacksRight = 0;
5+
public int blacksDown = 0;
6+
7+
public Cell(int right, int below) {
8+
blacksRight = right;
9+
blacksDown = below;
10+
}
11+
}

src/chapter18/Chapter18.java

Lines changed: 110 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,130 @@
11
package chapter18;
22

3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
36
public class Chapter18 {
47
public static void main (String [] args){
5-
/********/
6-
/* 18.1 */
7-
/********/
8+
test_EighteenPoint1();
9+
// 18.2 - Can't test randomness
10+
// 18.3 - Can't test randomness
11+
test_EighteenPoint4();
12+
test_EighteenPoint5();
13+
test_EighteenPoint6();
14+
test_EighteenPoint7();
15+
// 18.8 - Skip (First learn tries. Then maybe do this suffix tree problem)
16+
test_EighteenPoint9();
17+
test_EighteenPoint10();
18+
test_EighteenPoint11();
19+
test_EighteenPoint12();
20+
// 18.13: Skip (too complicated for an interview)
21+
}
22+
23+
public static void test_EighteenPoint1(){
824
System.out.println("*** Test 18.1: Add 2 numbers (without using +)");
9-
System.out.println("17 + 34 = " + EighteenPoint1.add(17, 34));
10-
System.out.println("-17 + 34 = " + EighteenPoint1.add(-17, 34));
11-
System.out.println("17 + -34 = " + EighteenPoint1.add(17, -34));
12-
System.out.println("-17 + -34 = " + EighteenPoint1.add(-17, -34));
25+
System.out.println("17 + 34 = " + EighteenPoint01.add(17, 34));
26+
System.out.println("-17 + 34 = " + EighteenPoint01.add(-17, 34));
27+
System.out.println("17 + -34 = " + EighteenPoint01.add(17, -34));
28+
System.out.println("-17 + -34 = " + EighteenPoint01.add(-17, -34));
1329
System.out.println();
14-
System.out.println("17 + 34 = " + EighteenPoint1.addBook(17, 34));
15-
System.out.println("-17 + 34 = " + EighteenPoint1.addBook(-17, 34));
16-
System.out.println("17 + -34 = " + EighteenPoint1.addBook(17, -34));
17-
System.out.println("-17 + -34 = " + EighteenPoint1.addBook(-17, -34));
30+
System.out.println("17 + 34 = " + EighteenPoint01.addBook(17, 34));
31+
System.out.println("-17 + 34 = " + EighteenPoint01.addBook(-17, 34));
32+
System.out.println("17 + -34 = " + EighteenPoint01.addBook(17, -34));
33+
System.out.println("-17 + -34 = " + EighteenPoint01.addBook(-17, -34));
1834
System.out.println();
19-
System.out.println("9 + 234 = " + EighteenPoint1.add(9, 234));
20-
System.out.println("-9 + 234 = " + EighteenPoint1.add(-9, 234));
21-
System.out.println("9 + -234 = " + EighteenPoint1.add(9, -234));
22-
System.out.println("-9 + -234 = " + EighteenPoint1.add(-9, -234));
35+
System.out.println("9 + 234 = " + EighteenPoint01.add(9, 234));
36+
System.out.println("-9 + 234 = " + EighteenPoint01.add(-9, 234));
37+
System.out.println("9 + -234 = " + EighteenPoint01.add(9, -234));
38+
System.out.println("-9 + -234 = " + EighteenPoint01.add(-9, -234));
2339
System.out.println();
24-
System.out.println("9 + 234 = " + EighteenPoint1.addBook(9, 234));
25-
System.out.println("-9 + 234 = " + EighteenPoint1.addBook(-9, 234));
26-
System.out.println("9 + -234 = " + EighteenPoint1.addBook(9, -234));
27-
System.out.println("-9 + -234 = " + EighteenPoint1.addBook(-9, -234));
40+
System.out.println("9 + 234 = " + EighteenPoint01.addBook(9, 234));
41+
System.out.println("-9 + 234 = " + EighteenPoint01.addBook(-9, 234));
42+
System.out.println("9 + -234 = " + EighteenPoint01.addBook(9, -234));
43+
System.out.println("-9 + -234 = " + EighteenPoint01.addBook(-9, -234));
44+
}
45+
46+
public static void test_EighteenPoint4(){
47+
System.out.println("\n\n*** Test 18.4: Count 2s in range 1...n");
48+
System.out.println("(from 1 to 99): " + EighteenPoint04.count2s(99));
49+
}
50+
51+
public static void test_EighteenPoint5(){
52+
System.out.println("\n\n*** Test 18.5: Shortest distance between 2 words in a file");
53+
String [] file = {"hey", "there", "hi", "food", "there", "now", "hope", "hi", "food", "now", "gone", "hey"};
54+
System.out.println(Arrays.toString(file));
2855

29-
// 18.2 - Can't test true randomness
30-
// 18.3 - Can't test true randomness
56+
EighteenPoint05.preProcess(file);
3157

32-
/********/
33-
/* 18.4 */
34-
/********/
35-
System.out.println("\n *** Test 18.4: ");
36-
System.out.println("Solution 1 - Count 2s: " + EighteenPoint4.count2s(99));
37-
System.out.println("Solution 2 - Count 2s: " + EighteenPoint4.count2sInRange(99));
38-
System.out.println("Solution 1 - Count 2s: " + EighteenPoint4.count2s(23212));
39-
System.out.println("Solution 2 - Count 2s: " + EighteenPoint4.count2sInRange(23212));
58+
System.out.println("(Solution 1) (there, food) Distance = " + EighteenPoint05.shortest(file, "there", "food"));
59+
System.out.println("(Solution 2) (there, food) Distance = " + EighteenPoint05.shortest2("there", "food"));
4060

41-
//18.5 - Was too lazy to test since Solution 1 matched book/web code, and Solution 1 just has an important algorithm.
61+
System.out.println("\n(Solution 1) (now, hi) Distance = " + EighteenPoint05.shortest(file, "now", "hi"));
62+
System.out.println("(Solution 2) (now, hi) Distance = " + EighteenPoint05.shortest2("now", "hi"));
4263

43-
/********/
44-
/* 18.6 */
45-
/********/
46-
System.out.println("\n *** Test 18.6: n (well n-1) smallest elements");
64+
System.out.println("\n(Solution 1) (gone, hi) Distance = " + EighteenPoint05.shortest(file, "gone", "hi"));
65+
System.out.println("(Solution 2) (gone, hi) Distance = " + EighteenPoint05.shortest2("gone", "hi"));
66+
}
67+
68+
public static void test_EighteenPoint6(){
69+
System.out.println("\n\n*** Test 18.6: Find n smallest elements in an array");
4770
int [] array = {52, 14, 16, 17, 26, 31, 16, 0, 6, 19, 6, 26, 14, 26, 22, 25};
48-
for (int i = 0; i < array.length; i++){
49-
System.out.print(array[i] + " ");
50-
}
71+
System.out.println(Arrays.toString(array));
72+
5173
System.out.print("\n3 Smallest: ");
52-
EighteenPoint6.select(array, 3);
74+
EighteenPoint06.findNthSmallestNums(array, 3);
5375
System.out.print("\n8 Smallest: ");
54-
EighteenPoint6.select(array, 8);
76+
EighteenPoint06.findNthSmallestNums(array, 8);
5577
System.out.print("\n12 Smallest: ");
56-
EighteenPoint6.select(array, 12);
57-
58-
/********/
59-
/* 18.7 */
60-
/********/
61-
System.out.println("\n\n *** Test 18.7: word combinations");
62-
String [] array2 = {"cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "spectaculious"};
63-
String [] array3 = {"cat", "banana", "dog", "nana", "walk", "nanadogwalkercat", "walker", "dogwalker"};
78+
EighteenPoint06.findNthSmallestNums(array, 12);
79+
}
80+
81+
public static void test_EighteenPoint7(){
82+
System.out.println("\n\n\n*** Test 18.7: Find longest word composed of other words");
83+
String [] array1 = {"cat", "banana", "dog", "nana", "walk", "walker", "dogwalker", "spectaculous"};
84+
String [] array2 = {"cat", "banana", "dog", "nana", "walk", "nanadogwalkercat", "walker", "dogwalker"};
6485

65-
System.out.print("Array A: ");
66-
for (int i = 0; i < array2.length; i++){
67-
System.out.print(array2[i] + " ");
68-
}
69-
System.out.println();
70-
System.out.print("Array B: ");
71-
for (int i = 0; i < array3.length; i++){
72-
System.out.print(array3[i] + " ");
73-
}
86+
System.out.print(Arrays.toString(array1));
87+
System.out.println("\nSolution: " + EighteenPoint07.longestWord(array1));
7488

75-
String longestComboWord = EighteenPoint7.longestWord(array2);
76-
System.out.println("\n\nFind longest word composed of other words (Array A): " + longestComboWord + "");
77-
78-
longestComboWord = EighteenPoint7.longestWord(array3);
79-
System.out.println("Find longest word composed of other words (Array B): " + longestComboWord);
80-
81-
//18.8 - Suffix Tree (Skipped)
82-
//18.9 - No need to test. Code same as book
83-
//18.10 - No need to test. Code same as book
84-
//18.11 - No need to test. Code same as book
85-
//18.12 - Too lazy to test. Code is from book & website.
89+
System.out.print("\n" + Arrays.toString(array2));
90+
System.out.println("\nSolution: " + EighteenPoint07.longestWord(array2));
91+
}
92+
93+
public static void test_EighteenPoint9(){
94+
System.out.println("\n\n*** Test 18.9: Keep track of median");
95+
for (int i = 0; i < 5; i++) {
96+
EighteenPoint09.addNum(i);
97+
System.out.println("Add " + i + " to data structure. Median = " + EighteenPoint09.getMedian());
98+
}
99+
}
100+
101+
public static void test_EighteenPoint10(){
102+
System.out.println("\n\n*** Test 18.10: Transform 1 word into another (changing 1 letter at a time)");
103+
EighteenPoint10.setUpDict();
104+
LinkedList<String> solution = EighteenPoint10.convert("Damp", "Like");
105+
System.out.println(solution);
106+
}
107+
108+
public static void test_EighteenPoint11(){
109+
System.out.println("\n\n*** Test 18.11: Find maximum subsquare with black borders");
110+
int[][] matrix = {{1,0,1,1,1},
111+
{1,1,1,0,1},
112+
{1,1,1,1,1},
113+
{1,1,1,1,0},
114+
{1,0,1,0,1}};
115+
System.out.println(EighteenPoint11.findLargestSubsquare(matrix));
116+
System.out.println(EighteenPoint11.findLargestSubsquare2(matrix));
117+
}
118+
119+
public static void test_EighteenPoint12(){
120+
System.out.println("\n\n*** Test 18.12: Find submatrix with largest possible sum");
121+
int[][] matrix = {{ 1,-2, 3, 1},
122+
{ 1, 5,-4, 1},
123+
{ 1, 1, 0, 2},
124+
{-1, 1, 1,-8},
125+
{-8,-9, 9,-3}};
126+
System.out.println("(Solution 1): " + EighteenPoint12.findLargestSubmatrix(matrix));
127+
System.out.println("(Solution 2): " + EighteenPoint12.findLargestSubmatrix2(matrix));
128+
System.out.println("(Solution 3): " + EighteenPoint12.findLargestSubmatrix3(matrix));
86129
}
87130
}

src/chapter18/EighteenPoint1.java renamed to src/chapter18/EighteenPoint01.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
package chapter18;
22

3-
public class EighteenPoint1 {
4-
/* Rodney CS 233 Solution:
3+
/* Add 2 numbers without using "+" or any other arithmetic operators
4+
*
5+
* Solutions Preference
6+
* -------------------------------------------------
7+
* 1) Separating Carry Trick Favorite
8+
* 2) CS 233 Bit-by-bit solution Worth mentioning
9+
*/
10+
public class EighteenPoint01 {
11+
12+
/* Solution 1
13+
*
14+
* Book Solution - Split up "sum" and "carry" */
15+
public static int addBook(int a, int b){
16+
if (b == 0)
17+
return a;
18+
int sum = a ^ b;
19+
int carry = (a & b) << 1;
20+
return addBook(sum, carry);
21+
}
22+
23+
/* Solution 2
24+
*
25+
* Rodney CS 233 Solution (Just a good technique to know):
526
* c a b (digit) (carry)
627
* 0 0 0 0 0
728
* 0 0 1 1 0
@@ -12,7 +33,7 @@ public class EighteenPoint1 {
1233
* 1 1 0 0 1
1334
* 1 1 1 1 1
1435
*/
15-
public static int add(int a, int b){ // ACTUALLY DON'T EVEN NEED "ANDED" "XORED" "ORED". Could have just done getBit like getBit(a, i);
36+
public static int add(int a, int b){
1637
int anded = a & b;
1738
int xored = a ^ b;
1839
int ored = a | b;
@@ -40,14 +61,4 @@ private static int setBit(int num, int bit){
4061
private static boolean getBit(int num, int bit){
4162
return (num & (1 << bit)) != 0;
4263
}
43-
44-
45-
46-
/* Book Solution - CLEVER TRICK: Split up "sum" and "carry" (sum is "xor", carry is "and" */
47-
public static int addBook(int a, int b){
48-
if (b == 0) return a;
49-
int sum = a ^ b; //add without carrying
50-
int carry = (a & b) << 1; //carry but don't add
51-
return addBook(sum, carry);
52-
}
5364
}

src/chapter18/EighteenPoint2.java renamed to src/chapter18/EighteenPoint02.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package chapter18;
22

3-
/* A must-know algorithm. Can ignore the recursive version. BTW, I had coded it incorrectly in "million" */
4-
public class EighteenPoint2 {
3+
public class EighteenPoint02 {
54
public void shuffle(int [] cards){
65
for (int i = 0; i < cards.length; i++){
7-
int rand = (int) (Math.random() * (i+1)); //random number between 0 and i inclusive
6+
int rand = (int) (Math.random() * (i+1)); // random integer between 0 and i inclusive
87
swap(cards, i, rand);
98
}
109
}

src/chapter18/EighteenPoint03.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package chapter18;
2+
3+
/* (From book)
4+
Suppose we have an algorithm that can pull a random set of m elements from an array
5+
of size n - 1. How can we use this algorithm to pull a random set of m elements from
6+
an array of size n?
7+
8+
We can first pull a random set of size m from the first n - 1 elements. Then, we just need
9+
to decide if array [n] should be inserted into our subset (which would require pulling
10+
out a random element from it). An easy way to do this is to pick a random number k
11+
from 0 through n. If k < m, then insert array [n] into subset [k].This will both "fairly"
12+
(i.e., with proportional probability) insert array[n] into the subset and "fairly"remove
13+
a random element from the subset.
14+
15+
(Book's iterative solution of the above recursive explanation is below)
16+
*/
17+
public class EighteenPoint03 {
18+
public int[] generate(int [] array, int m){
19+
/* Copy first "m" elements into new array */
20+
int [] solution = new int[m];
21+
for (int i = 0; i < m; i++){
22+
solution[i] = array[i];
23+
}
24+
25+
/* 1 by 1, decide if array[i] (where i >= m) should be in solution[] */
26+
for (int i = m; i < array.length; i++){
27+
int rand = (int) (Math.random() * (i+1)); // random number between 0 and i inclusive
28+
if (rand < m) // There's "rand / m" percent chance that the new element will be put into array
29+
solution[rand] = array[i];
30+
}
31+
return solution;
32+
}
33+
}

0 commit comments

Comments
 (0)