Skip to content

Instantly share code, notes, and snippets.

@yehee
Last active May 8, 2019 21:46
Show Gist options
  • Save yehee/51ada6ad258dd2353089d0878378f4fa to your computer and use it in GitHub Desktop.
Save yehee/51ada6ad258dd2353089d0878378f4fa to your computer and use it in GitHub Desktop.
Cracking the Coding: Practice Problem
// ch 8.7
import java.util.*;
public class Main {
public static void main(String args[]) {
System.out.println(getPerms("ab")); // [ab, ba]
System.out.println(getPerms("abc")); // [abc, acb, bac, bca, cab, cba]
System.out.println(getPerms("love")); // [love, loev, lvoe, lveo, leov, levo, olve, olev, ovle, ovel, oelv, oevl, vloe, vleo, vole, voel, velo, veol, elov, elvo, eolv, eovl, evlo, evol]
}
static ArrayList < String > getPerms(String input) {
ArrayList < String > perms = new ArrayList < > ();
if (input.length() == 0) {
perms.add(input);
} else {
for (int i = 0; i < input.length(); i++) {
ArrayList < String > sub = getPerms(input.substring(0, i) + input.substring(i + 1, input.length()));
for (String s: sub) {
perms.add(input.charAt(i) + s);
}
}
}
return perms;
}
}
// ch 8.13
import java.util.*;
public class Main {
public static void main(String args[]) {
System.out.println("Hello");
ArrayList<Box> boxes = new ArrayList<>();
boxes.add(new Box(1, 1, 1));
boxes.add(new Box(2, 2, 2));
boxes.add(new Box(3, 3, 3));
System.out.println(createStack(boxes)); // 6
boxes.add(new Box(3, 4, 3));
System.out.println(createStack(boxes)); // 7
boxes.add(new Box(6, 4, 4));
boxes.add(new Box(7, 5, 5));
boxes.add(new Box(7, 7, 7));
System.out.println(createStack(boxes)); // 17
}
static class Box implements Comparable<Box> {
int w, h, d;
Box(int w, int h, int d) {
this.w = w;
this.h = h;
this.d = d;
}
public int compareTo(Box b) {
if (this.w < b.w && this.h < b.h && this.d < b.d) return -1;
if (this.w > b.w && this.h > b.h && this.d > b.d) return 1;
return 0;
}
}
static int createStack(ArrayList<Box> boxes) {
int maxheight = 0;
for (Box b: boxes)
maxheight = Math.max(maxheight, createStack(b, boxes));
return maxheight;
}
static int createStack(Box bottom, ArrayList<Box> boxes) {
int maxheight = 0;
for (Box b: boxes)
if (bottom.compareTo(b) > 0)
maxheight = Math.max(maxheight, createStack(b, boxes));
return bottom.h + maxheight;
}
}
// ch 13.8
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String args[]) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(getRandomSubset(list));
}
static List<Integer> getRandomSubset(List<Integer> list) {
Random random = new Random();
return list.stream().filter(k -> random.nextBoolean())
.collect(Collectors.toList());
}
}
// 16.7: Number Max
import java.util.*;
public class Main {
public static void main(String args[]) {
System.out.println(getMax(10000, 10001)); // 10001
}
static int getMax(int a, int b) {
int sum = a + b;
int dff = Math.abs(a - b);
int min = (sum - dff) / 2;
return sum - min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment