Skip to content

Commit 76cf35f

Browse files
add examples
1 parent 088ef6f commit 76cf35f

36 files changed

+1324
-5
lines changed

HolczerUdemyCourses/src/hashtable/linearprobing/HashTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void resize(int newCapacity) {
120120
// have to rehash the table entries because the hash function relies heavily on the
121121
// size: ~ key.hashCode() % sizeOfTable
122122
// so it is a O(n) operation --> we should make as few resize operation as possible
123-
for (int i = 0; i < capacity; ++i) {
123+
for (int i = 0; i < capacity; i++) {
124124
if (keys[i] != null) {
125125
newTable.put(keys[i], values[i]);
126126
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problems.binpacking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import static java.util.stream.Collectors.joining;
7+
8+
public class Bin {
9+
private int id;
10+
private int capacity;
11+
private int currentSize;
12+
private List<Integer> items;
13+
14+
Bin(int capacity, int id) {
15+
this.capacity = capacity;
16+
this.id = id;
17+
this.items = new ArrayList<>();
18+
}
19+
20+
boolean put(Integer item) {
21+
if (currentSize + item > capacity) {
22+
return false;
23+
}
24+
25+
items.add(item);
26+
currentSize += item;
27+
28+
return true;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return items.stream()
34+
.map(item -> item + " ")
35+
.collect(joining("", "Items in bin #" + id + ": ", ""));
36+
}
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package problems.binpacking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import static java.util.Collections.reverseOrder;
7+
8+
public class FirstFitDecreasingAlgorithm {
9+
private List<Bin> bins;
10+
private List<Integer> items;
11+
private int binCapacity;
12+
private int binCounter = 1;
13+
14+
public FirstFitDecreasingAlgorithm(List<Integer> items, int binCapacity) {
15+
this.items = items;
16+
this.binCapacity = binCapacity;
17+
this.bins = new ArrayList<>(this.items.size());
18+
}
19+
20+
public void solve() {
21+
items.sort(reverseOrder());
22+
23+
if (items.get(0) > this.binCapacity) {
24+
System.out.println("No feasible solution...");
25+
return;
26+
}
27+
28+
bins.add(new Bin(binCapacity, binCounter)); // first bin
29+
30+
for (Integer currentItem : items) {
31+
boolean isOk = false; // track whether we have put the item into a bin or not
32+
int currentBin = 0;
33+
34+
while (!isOk) {
35+
if (currentBin == this.bins.size()) { // item does not fit in last bin -> try a new bin
36+
Bin newBin = new Bin(binCapacity, ++binCounter);
37+
newBin.put(currentItem);
38+
bins.add(newBin);
39+
isOk = true;
40+
} else if (bins.get(currentBin).put(currentItem)) { // current item fits in the given bin
41+
isOk = true;
42+
} else {
43+
currentBin++; // trying the next bin
44+
}
45+
}
46+
}
47+
}
48+
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problems.coinchange;
2+
3+
public class CoinChange {
4+
5+
// recursive coin change exponential running time O(2^n)
6+
public int naiveCoinChange(int m, int[] v, int index) {
7+
if (m < 0) {
8+
return 0;
9+
}
10+
if (m == 0) {
11+
return 1;
12+
}
13+
if (v.length == index) {
14+
return 0;
15+
}
16+
17+
return naiveCoinChange(m - v[index], v, index) + naiveCoinChange(m, v, index + 1);
18+
}
19+
20+
// O(n) running time
21+
public void dynamicProgrammingCoinChange(int[] v, int m) {
22+
int[][] dpTable = new int[v.length + 1][m + 1];
23+
24+
for (int i = 0; i <= v.length; i++) {
25+
dpTable[i][0] = 1;
26+
}
27+
28+
for (int i = 1; i <= m; i++) {
29+
dpTable[0][i] = 0;
30+
}
31+
32+
// O(v*m)
33+
for (int i = 1; i <= v.length; i++) {
34+
for (int j = 1; j <= m; j++) {
35+
if (v[i - 1] <= j) {
36+
dpTable[i][j] = dpTable[i - 1][j] + dpTable[i][j - v[i - 1]];
37+
} else {
38+
dpTable[i][j] = dpTable[i - 1][j];
39+
}
40+
}
41+
}
42+
43+
System.out.println("Solution: " + dpTable[v.length][m]);
44+
}
45+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package problems.coloring;
2+
3+
public class GraphColoring {
4+
private int numOfVertices;
5+
private int numOfColors;
6+
private int[] colors;
7+
private int[][] graphMatrix;
8+
9+
public void graphColor(int[][] graph, int numOfColors) {
10+
this.numOfVertices = graph.length;
11+
this.numOfColors = numOfColors;
12+
this.colors = new int[numOfVertices];
13+
this.graphMatrix = graph;
14+
}
15+
16+
public void solveColoringProblem() {
17+
if (solve(0)) {
18+
showResult();
19+
} else {
20+
System.out.println("No solution with the given parameters...");
21+
}
22+
}
23+
24+
public boolean solve(int nodeIndex) {
25+
if (nodeIndex == numOfVertices) {
26+
return true;
27+
}
28+
29+
// try all colours
30+
for (int colorIndex = 1; colorIndex <= numOfColors; colorIndex++) {
31+
if (isValidColor(nodeIndex, colorIndex)) {
32+
// assign and proceed with next vertex
33+
colors[nodeIndex] = colorIndex;
34+
if (solve(nodeIndex + 1)) {
35+
return true;
36+
}
37+
}
38+
}
39+
40+
return false;
41+
}
42+
43+
private boolean isValidColor(int nodeIndex, int colorIndex) {
44+
for (int i = 0; i < numOfVertices; i++) {
45+
if (graphMatrix[nodeIndex][i] == 1 && colorIndex == colors[i]) {
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
53+
private void showResult() {
54+
for (int i = 0; i < numOfVertices; i++) {
55+
System.out.println("Node " + (i + 1) + " has color index: " + colors[i] + " ");
56+
}
57+
System.out.println();
58+
}
59+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package problems.cpop;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ClosestPairPoints {
7+
private List<Point> points;
8+
9+
public ClosestPairPoints(List<Point> points) {
10+
this.points = points;
11+
}
12+
13+
public double solveProblem() {
14+
List<Point> sortedXPoints = new ArrayList<>();
15+
List<Point> yPoints = new ArrayList<>();
16+
17+
for (Point point : points) {
18+
sortedXPoints.add(point);
19+
yPoints.add(point);
20+
}
21+
22+
points.sort(Point::compareTo);
23+
24+
return findClosestPoints(sortedXPoints, yPoints, sortedXPoints.size());
25+
}
26+
27+
private double findClosestPoints(List<Point> pointsSortedX, List<Point> pointsY, int numOfPoints) {
28+
if (numOfPoints <= 3) {
29+
return bruteForceApproach(pointsSortedX);
30+
}
31+
32+
int middleIndex = numOfPoints / 2;
33+
34+
Point middlePoint = pointsSortedX.get(middleIndex);
35+
36+
List<Point> leftSubPointsY = new ArrayList<>();
37+
List<Point> leftSubPointsSortedX = new ArrayList<>();
38+
List<Point> rightSubPointsY = new ArrayList<>();
39+
List<Point> rightSubPointsSortedX = new ArrayList<>();
40+
41+
// divide the point to left and right sub-arrays
42+
for (int index = 0; index < numOfPoints; index++) {
43+
if (pointsY.get(index).getX() <= middlePoint.getX()) {
44+
leftSubPointsY.add(pointsY.get(index));
45+
leftSubPointsSortedX.add(pointsSortedX.get(index));
46+
} else {
47+
rightSubPointsY.add(pointsY.get(index));
48+
rightSubPointsSortedX.add(pointsSortedX.get(index));
49+
}
50+
}
51+
52+
double sigmaLeft = findClosestPoints(leftSubPointsSortedX, leftSubPointsY, middleIndex);
53+
double sigmaRight = findClosestPoints(rightSubPointsSortedX, rightSubPointsY, numOfPoints - middleIndex);
54+
double sigma = Math.min(sigmaLeft, sigmaRight);
55+
56+
List<Point> pointsInStrip = new ArrayList<>();
57+
58+
for (int index = 0; index < numOfPoints; index++) {
59+
if (Math.abs(pointsY.get(index).getX() - middlePoint.getX()) < sigma) {
60+
pointsInStrip.add(pointsY.get(index));
61+
}
62+
}
63+
64+
double minDistanceStrip = findMinimumDistanceInStrip(pointsInStrip, sigma);
65+
66+
return Math.min(sigma, minDistanceStrip);
67+
}
68+
69+
private double bruteForceApproach(List<Point> points) {
70+
double minDistance = Double.MAX_VALUE;
71+
72+
for (int i = 0; i < points.size(); i++) {
73+
for (int j = i + 1; j < points.size(); j++) {
74+
if (distance(points.get(i), points.get(j)) < minDistance) {
75+
minDistance = distance(points.get(i), points.get(j));
76+
}
77+
}
78+
}
79+
80+
return minDistance;
81+
}
82+
83+
private double findMinimumDistanceInStrip(List<Point> pointsInStrip, double sigma) {
84+
double minDistance = sigma;
85+
86+
for (int i = 0; i < pointsInStrip.size(); i++) {
87+
for (int j = i + 1; j < pointsInStrip.size() && (pointsInStrip.get(j).getY() - pointsInStrip.get(i).getY()) < minDistance; ++j) {
88+
if (distance(pointsInStrip.get(i), pointsInStrip.get(j)) < minDistance) {
89+
minDistance = distance(pointsInStrip.get(i), pointsInStrip.get(j));
90+
}
91+
}
92+
}
93+
94+
return minDistance;
95+
}
96+
97+
private double distance(Point point1, Point point2) {
98+
double xDistance = Math.abs(point1.getX() - point2.getX());
99+
double yDistance = Math.abs(point1.getY() - point2.getY());
100+
return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
101+
}
102+
103+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package problems.cpop;
2+
3+
import static java.lang.Double.compare;
4+
5+
public class Point implements Comparable<Point> {
6+
private double x;
7+
private double y;
8+
9+
public Point(double x, double y) {
10+
this.x = x;
11+
this.y = y;
12+
}
13+
14+
public double getX() {
15+
return x;
16+
}
17+
18+
public void setX(double x) {
19+
this.x = x;
20+
}
21+
22+
public double getY() {
23+
return y;
24+
}
25+
26+
public void setY(double y) {
27+
this.y = y;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "(" + this.x + ";" + this.y + ")";
33+
}
34+
35+
@Override
36+
public int compareTo(Point o) {
37+
return compare(this.getX(), o.getX());
38+
}
39+
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package problems.eggdropping;
2+
3+
class Constants {
4+
static final int NUM_OF_EGGS = 2;
5+
static final int NUM_OF_FLOORS = 100;
6+
7+
private Constants() {
8+
}
9+
10+
}

0 commit comments

Comments
 (0)