Skip to content

Commit 479c0de

Browse files
committed
Merge branch 'develop'
Chapter 18
2 parents 38f0cbd + ddbdff7 commit 479c0de

19 files changed

+462
-0
lines changed

greedyalgorithms/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Chapter 18: Greedy Algorithms and Invariants
2+
3+
* 18.1 ComputeOptimumAssignment
4+
* 18.2 ScheduleMinimizedWaitingTime
5+
* 18.3 IntervalCoveringProblem
6+
* 18.4 ThreeSumProblem
7+
* 18.5 FindMajorityElement
8+
* 18.6 GasUpProblem
9+
* 18.7 ComputeMaximumWaterTrapped
10+
* 18.8 ComputeLargestRectangle
11+

greedyalgorithms/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>elements-of-programming-interviews</artifactId>
7+
<groupId>gardncl</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>greedyalgorithms</artifactId>
13+
<name>Chapter 18: Greedy Algorithms and Invariants</name>
14+
<dependencies>
15+
<dependency>
16+
<groupId>gardncl</groupId>
17+
<artifactId>datastructures</artifactId>
18+
<version>1.4</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>gardncl</groupId>
22+
<artifactId>utils</artifactId>
23+
<version>1.0</version>
24+
</dependency>
25+
</dependencies>
26+
27+
28+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.List;
2+
3+
public class ComputeLargestRectangle {
4+
5+
/*
6+
18.8
7+
8+
Let A be an array representing the heights of adjacent buildings
9+
of unit width. Design an algorithm to compute the area of the
10+
largest rectangle contained in this skyline.
11+
*/
12+
13+
public static int calculateLargestRectangle(List<Integer> heights) {
14+
15+
return 0;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.List;
2+
3+
public class ComputeMaximumWaterTrapped {
4+
5+
/*
6+
18.7
7+
8+
Write a program which takes as input an integer
9+
array and returns the prior of entries that
10+
trap the maximum amount of water.
11+
*/
12+
13+
public static int getMaxTrappedWater(List<Integer> heights) {
14+
15+
return 0;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class ComputeOptimumAssignment {
5+
6+
/*
7+
18.1
8+
9+
Design an algorithm that takes as input a set of tasks
10+
and returns an optimum assignment.
11+
*/
12+
13+
public static List<Tuple> optimumTaskAssignment(List<Integer> taskDurations) {
14+
15+
return Collections.emptyList();
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Iterator;
2+
3+
public class FindMajorityElement {
4+
5+
/*
6+
18.5
7+
8+
You are reading a sequence of strings. You know a priori
9+
that more than half the strings are repetitions of a
10+
single string (the "majority element") but the positions
11+
where the majority element occurs are unknown. Write a
12+
program that makes a single pass over the sequence and
13+
identifies the majority element.
14+
*/
15+
16+
public static String majoritySearch(Iterator<String> sequence) {
17+
18+
return "";
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.List;
2+
3+
public class GasUpProblem {
4+
5+
/*
6+
18.6
7+
8+
Given an instance of the gas up problem,
9+
how would you efficiently compute an ample
10+
city? You can assume that there exists an ample city.
11+
*/
12+
13+
public static int findAmpleCity(List<Integer> gallons, List<Integer> distances) {
14+
15+
return 0;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class IntervalCoveringProblem {
5+
6+
/*
7+
18.3
8+
9+
You are given a closed set of intervals. Design an efficient
10+
algorithm for finding a minimum sized set of numbers that
11+
cover all the intervals.
12+
*/
13+
14+
public static List<Integer> findMinimumVisits(List<Tuple> intervals) {
15+
16+
return Collections.emptyList();
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.List;
2+
3+
public class ScheduleMinimizedWaitingTime {
4+
5+
/*
6+
18.2
7+
8+
Given service times for a set of queries, compute a schedule
9+
for processing the queries that minimizes the total waiting
10+
time. Return the minimum waiting time.
11+
*/
12+
13+
public static int minimumTotalWaitingTime(List<Integer> serviceTime) {
14+
15+
return 0;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.List;
2+
3+
public class ThreeSumProblem {
4+
5+
/*
6+
18.4
7+
8+
Design an algorithm that takes as input an array and a number,
9+
and determines if there are three entries in the array
10+
(not necessarily distinct) which add up to the specified number.
11+
*/
12+
13+
public static boolean hasThreeSum(List<Integer> A, int t) {
14+
15+
return false;
16+
}
17+
}

0 commit comments

Comments
 (0)