-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 18
- Loading branch information
Showing
19 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Chapter 18: Greedy Algorithms and Invariants | ||
|
||
* 18.1 ComputeOptimumAssignment | ||
* 18.2 ScheduleMinimizedWaitingTime | ||
* 18.3 IntervalCoveringProblem | ||
* 18.4 ThreeSumProblem | ||
* 18.5 FindMajorityElement | ||
* 18.6 GasUpProblem | ||
* 18.7 ComputeMaximumWaterTrapped | ||
* 18.8 ComputeLargestRectangle | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>elements-of-programming-interviews</artifactId> | ||
<groupId>gardncl</groupId> | ||
<version>1.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>greedyalgorithms</artifactId> | ||
<name>Chapter 18: Greedy Algorithms and Invariants</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>utils</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
greedyalgorithms/src/main/java/ComputeLargestRectangle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class ComputeLargestRectangle { | ||
|
||
/* | ||
18.8 | ||
Let A be an array representing the heights of adjacent buildings | ||
of unit width. Design an algorithm to compute the area of the | ||
largest rectangle contained in this skyline. | ||
*/ | ||
|
||
public static int calculateLargestRectangle(List<Integer> heights) { | ||
|
||
return 0; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
greedyalgorithms/src/main/java/ComputeMaximumWaterTrapped.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class ComputeMaximumWaterTrapped { | ||
|
||
/* | ||
18.7 | ||
Write a program which takes as input an integer | ||
array and returns the prior of entries that | ||
trap the maximum amount of water. | ||
*/ | ||
|
||
public static int getMaxTrappedWater(List<Integer> heights) { | ||
|
||
return 0; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
greedyalgorithms/src/main/java/ComputeOptimumAssignment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComputeOptimumAssignment { | ||
|
||
/* | ||
18.1 | ||
Design an algorithm that takes as input a set of tasks | ||
and returns an optimum assignment. | ||
*/ | ||
|
||
public static List<Tuple> optimumTaskAssignment(List<Integer> taskDurations) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.util.Iterator; | ||
|
||
public class FindMajorityElement { | ||
|
||
/* | ||
18.5 | ||
You are reading a sequence of strings. You know a priori | ||
that more than half the strings are repetitions of a | ||
single string (the "majority element") but the positions | ||
where the majority element occurs are unknown. Write a | ||
program that makes a single pass over the sequence and | ||
identifies the majority element. | ||
*/ | ||
|
||
public static String majoritySearch(Iterator<String> sequence) { | ||
|
||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class GasUpProblem { | ||
|
||
/* | ||
18.6 | ||
Given an instance of the gas up problem, | ||
how would you efficiently compute an ample | ||
city? You can assume that there exists an ample city. | ||
*/ | ||
|
||
public static int findAmpleCity(List<Integer> gallons, List<Integer> distances) { | ||
|
||
return 0; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
greedyalgorithms/src/main/java/IntervalCoveringProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class IntervalCoveringProblem { | ||
|
||
/* | ||
18.3 | ||
You are given a closed set of intervals. Design an efficient | ||
algorithm for finding a minimum sized set of numbers that | ||
cover all the intervals. | ||
*/ | ||
|
||
public static List<Integer> findMinimumVisits(List<Tuple> intervals) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
greedyalgorithms/src/main/java/ScheduleMinimizedWaitingTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class ScheduleMinimizedWaitingTime { | ||
|
||
/* | ||
18.2 | ||
Given service times for a set of queries, compute a schedule | ||
for processing the queries that minimizes the total waiting | ||
time. Return the minimum waiting time. | ||
*/ | ||
|
||
public static int minimumTotalWaitingTime(List<Integer> serviceTime) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class ThreeSumProblem { | ||
|
||
/* | ||
18.4 | ||
Design an algorithm that takes as input an array and a number, | ||
and determines if there are three entries in the array | ||
(not necessarily distinct) which add up to the specified number. | ||
*/ | ||
|
||
public static boolean hasThreeSum(List<Integer> A, int t) { | ||
|
||
return false; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
greedyalgorithms/src/test/java/ComputeLargestRectangleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeLargestRectangleTest { | ||
|
||
private int expected; | ||
private List<Integer> heights; | ||
|
||
@Test | ||
public void calculateLargestRectangle1() throws Exception { | ||
expected = 20; | ||
heights = Arrays.asList( | ||
0,1,4,2,5,6,3,2,6,6,5,2,1,3 | ||
); | ||
|
||
test(expected, heights); | ||
} | ||
|
||
private void test(int expected, List<Integer> heights) { | ||
assertEquals(expected, ComputeLargestRectangle.calculateLargestRectangle(heights)); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
greedyalgorithms/src/test/java/ComputeMaximumWaterTrappedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeMaximumWaterTrappedTest { | ||
|
||
private Tuple expected; | ||
private List<Integer> heights; | ||
|
||
@Test | ||
public void getMaxTrappedWater1() throws Exception { | ||
expected = new Tuple(4,16); | ||
heights = Arrays.asList( | ||
1,2,1,3,4,4,5,6,2,1,3,1,3,2,1,2,4,1 | ||
); | ||
|
||
test(expected, heights); | ||
} | ||
|
||
@Test | ||
public void getMaxTrappedWater2() throws Exception { | ||
expected = new Tuple(7,15); | ||
heights = Arrays.asList( | ||
1,2,1,3,4,4,5,6,2,1,3,1,3,2,1,6,4,1 | ||
); | ||
|
||
test(expected, heights); | ||
} | ||
|
||
private void test(Tuple expected, List<Integer> heights) { | ||
assertEquals(expected, ComputeMaximumWaterTrapped.getMaxTrappedWater(heights)); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
greedyalgorithms/src/test/java/ComputeOptimumAssignmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeOptimumAssignmentTest { | ||
|
||
private List<Tuple> expected; | ||
private List<Integer> taskDurations; | ||
|
||
@Test | ||
public void optimumTaskAssignment1() throws Exception { | ||
expected = Arrays.asList( | ||
new Tuple(2,5), | ||
new Tuple(1,6), | ||
new Tuple(4,4) | ||
); | ||
taskDurations = Arrays.asList(1,2,4,4,5,6); | ||
|
||
test(expected, taskDurations); | ||
} | ||
|
||
private void test(List<Tuple> expected, List<Integer> taskDurations) { | ||
assertEquals(expected, ComputeOptimumAssignment.optimumTaskAssignment(taskDurations)); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
greedyalgorithms/src/test/java/FindMajorityElementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class FindMajorityElementTest { | ||
|
||
private Character expected; | ||
private String sequence; | ||
|
||
@Test | ||
public void majoritySearch1() throws Exception { | ||
expected = 'a'; | ||
sequence = "aca"; | ||
|
||
test(expected, sequence); | ||
} | ||
|
||
@Test | ||
public void majoritySearch2() throws Exception { | ||
expected = 'a'; | ||
sequence = "bacaabaaca"; | ||
|
||
test(expected, sequence); | ||
} | ||
|
||
private void test(Character expected, String sequence) { | ||
assertEquals(expected, FindMajorityElement.majoritySearch(Arrays.asList(sequence).iterator())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class GasUpProblemTest { | ||
|
||
private int expected; | ||
private List<Integer> gallons; | ||
private List<Integer> distances; | ||
|
||
@Test | ||
public void findAmpleCity1() throws Exception { | ||
expected = 3; | ||
gallons = Arrays.asList( | ||
50,20,5,30,25,10,10 | ||
); | ||
distances = Arrays.asList( | ||
900,600,200,400,600,200,100 | ||
); | ||
|
||
test(expected,gallons,distances); | ||
} | ||
|
||
private void test(int expected, List<Integer> gallons, List<Integer> distances) { | ||
assertEquals(expected, GasUpProblem.findAmpleCity(gallons, distances)); | ||
} | ||
|
||
} |
Oops, something went wrong.