Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package algorithms.curated170.hard;

public class MinimizeMaxDistanceToGasStation {

public double minmaxGasDist(int[] stats, int k) {
double left = 0, right = stats[stats.length - 1] - stats[0];

while (left + 1e-6 < right) {
double mid = (left + right) / 2;
int count = 0;

for (int i = 0; i < stats.length - 1; i++) {
count += (int) ((stats[i + 1] - stats[i]) / mid);
}

if (count > k) {
left = mid;
} else {
right = mid;
}
}
return right;
}

public static void main(String[] args) {
var solution = new MinimizeMaxDistanceToGasStation();

System.out.println(solution.minmaxGasDist(new int[] { 1, 2, 3, 4, 5 }, 4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package algorithms.curated170.hard;

import java.util.Comparator;
import java.util.PriorityQueue;

public class MinimizeMaxDistanceToGasStationPriorityQueue {

PriorityQueue<Interval> pq;

public double minmaxGasDist(final int[] stats, final int K) {

pq = new PriorityQueue<Interval>(new Comparator<Interval>() {
public int compare(Interval a, Interval b) {

double diff = a.distance() - b.distance();
if (diff < 0) {
return +1;
} else if (diff > 0) {
return -1;
} else {
return 0;
}
}
});



int remaining = distributeStations(stats, K);

shareTheRest(remaining);

Interval last = pq.poll();
return last.distance();

}

private void shareTheRest(int remaining) {
while (remaining > 0) {
Interval interval = pq.poll();
interval.numInsertions++;
pq.add(interval);
remaining--;
}
}

private int distributeStations(final int[] stats, final int K) {
int remaining = K;
final double intervalPlacementSubrange = K / (stats[stats.length - 1] - stats[0]);

for (int i = 0; i < stats.length - 1; i++) {
int numInsertions = (int) ((stats[i + 1] - stats[i])*intervalPlacementSubrange);

pq.add(new Interval(stats[i], stats[i + 1], numInsertions));

remaining -= numInsertions;
}
return remaining;
}

class Interval {
final double length;
int numInsertions;

double distance() {
return length / (numInsertions + 1);
}

Interval(double left, double right, int numInsertions) {
length = right - left;
this.numInsertions = numInsertions;
}
}

public static void main(String[] args) {
var solution = new MinimizeMaxDistanceToGasStationPriorityQueue();

System.out.println(solution.minmaxGasDist(new int[] { 1, 2, 3, 4, 5 }, 4));
System.out.println(solution.minmaxGasDist(new int[] { 0, 100, 150}, 4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package algorithms.curated170.hard;

import java.util.PriorityQueue;

public class MinimizeMaxDistanceToGasStationPriorityQueueDiscrete {

public double minmaxGasDist(int[] stats, int k) {
PriorityQueue<double[]> pq = new PriorityQueue<>((a, b) -> Double.compare(b[0], a[0]));

for (int i = 0; i < stats.length - 1; i++) {
pq.offer(new double[] { stats[i + 1] - stats[i], 1 });
}
double maxDistance = (stats[stats.length-1] - stats[0])/(double)(k+1);
while (k>0) {
double[] pair = pq.poll();
double nextLargest = pq.peek()[0];
while(k>0 && (pair[0]>=nextLargest || pair[0]>maxDistance)){
pair[0] = pair[0] * pair[1] / (++pair[1]);
k--;
}

pq.offer(pair);

}

return pq.poll()[0];
}

public static void main(String[] args) {
var solution = new MinimizeMaxDistanceToGasStationPriorityQueueDiscrete();

System.out.println(solution.minmaxGasDist(new int[] { 1, 2, 3, 4, 5 }, 4));
}
}