Skip to content

Commit

Permalink
Merge pull request #4791 from freakin23/master
Browse files Browse the repository at this point in the history
py / java sol for Firehose
  • Loading branch information
SansPapyrus683 committed Sep 20, 2024
2 parents 748de1a + 0726ad3 commit 8009674
Showing 1 changed file with 137 additions and 3 deletions.
140 changes: 137 additions & 3 deletions solutions/silver/ccc-Firehose.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ We use a greedy algorithm to find $\texttt{possible}(i)$. Firstly, we want to so
**Time Complexity**: $\mathcal O(h^2\times log(\texttt{max}(H_i)))$

<LanguageSection>

<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;
Expand Down Expand Up @@ -71,7 +71,7 @@ int main() {
// Binary search for the lowest hose length
int l = 0;
int r = STREET_SIZE;
int ans = 0;
int ans = -1;
while (l <= r) {
int m = (l + r) / 2;
if (possible(m)) {
Expand All @@ -82,9 +82,143 @@ int main() {
}
}

cout << ans << "\n";
cout << ans << endl;
}
```

</CPPSection>
<JavaSection>

```java
import java.io.*;
import java.util.*;

public class Firehose {
private static final int MAX_H = 1000;
private static final int STREET_SIZE = 1000000;

private static boolean possible(int length, int numHydrants, int[] houses) {
for (int i = 0; i < houses.length; i++) {
int needed = 0; // Number of fire hydrants that are needed.
// The house that we need to connect a fire hydrant to.
int start = houses[i];
for (int j = 1; j < houses.length; j++) {
// Address of house at index j.
int end = houses[(i + j) % houses.length];
/*
* If the distance between the start and end houses is greater than
* two times the length of the hose, a single fire hydrant will not
* be enough to cover both houses.
*/
int dist = (end - start + STREET_SIZE) % STREET_SIZE;
if (dist > 2 * length) {
start = end;
needed++;
}
}

// Increment needed as we need another hydrant to reach the last houses.
needed++;

if (needed <= numHydrants) { return true; }
}
return false;
}

public static void main(String[] args) {
Kattio io = new Kattio();

int numHouses = io.nextInt();
int[] houses = new int[numHouses];

for (int i = 0; i < numHouses; i++) { houses[i] = io.nextInt(); }

int numHydrants = io.nextInt();

Arrays.sort(houses);

// Binary search for the lowest hose length
int left = 0;
int right = STREET_SIZE;
int ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (possible(mid, numHydrants, houses)) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}

System.out.println(ans);
io.close();
}

// CodeSnip{Kattio}
}
```

</JavaSection>
<PySection>

```py
MAX_H = 1000
STREET_SIZE = 1000000


def possible(length: int, num_hydrants: int, houses: list) -> bool:
for i in range(len(houses)):
# Number of fire hydrants that are needed.
needed = 0
# The house that we need to connect a fire hydrant to.
start = houses[i]
for j in range(1, len(houses)):
# Address of house at index j.
end = houses[(i + j) % len(houses)]

"""
If the distance between the start and end houses is greater than
two times the length of the hose, a single fire hydrant will not
be enough to cover both houses.
"""
dist = (end - start + STREET_SIZE) % STREET_SIZE
if dist > 2 * length:
start = end
needed += 1

# Increment needed as we need another hydrant to reach the last houses.
needed += 1

if needed <= num_hydrants:
return True

return False


num_houses = int(input())
houses = []
for _ in range(num_houses):
houses.append(int(input()))

num_hydrants = int(input())

houses.sort()

# Binary search for the lowest hose length
l = 0
r = STREET_SIZE
ans = -1
while l <= r:
m = (l + r) // 2
if possible(m, num_hydrants, houses):
r = m - 1
ans = m
else:
l = m + 1

print(ans)
```
</PySection>
</LanguageSection>

0 comments on commit 8009674

Please sign in to comment.