Skip to content

Commit 9a2f8b5

Browse files
solves heaters
1 parent f253181 commit 9a2f8b5

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-107/571-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-107/1571-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-32/1571-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-108/571-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-108/1571-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-108/1571-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77

88
🔒 = Subscription Content
@@ -125,7 +125,7 @@
125125
| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RepeatedSubstringPattern.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/repeated_substring_pattern.py) |
126126
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/HammingDistance.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/hamming_distance.py) |
127127
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IslandPerimeter.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/island_perimeter.py) |
128-
| 475 | [Heaters](https://leetcode.com/problems/heaters) | Medium | |
128+
| 475 | [Heaters](https://leetcode.com/problems/heaters) | Medium | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Heaters.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/heaters.py) |
129129
| 476 | [Number Complement](https://leetcode.com/problems/number-complement) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NumberComplement.java) |
130130
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LicenseKeyFormatting.java) |
131131
| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaxConsecutiveOnes.java) |

Diff for: python/heaters.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def binary_search(self, array: List[int], element: int) -> int:
6+
left, right = 0, len(array) - 1
7+
while left <= right:
8+
middle = (left + right) // 2
9+
if array[middle] == element: return middle
10+
elif array[middle] < element: left = middle + 1
11+
else: right = middle - 1
12+
return left
13+
14+
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
15+
heaters.sort()
16+
minRadius, infinity = 0, float('inf')
17+
for house in houses:
18+
index = self.binary_search(heaters, house)
19+
if index < len(heaters) and house == heaters[index]:
20+
continue
21+
leftRadius = infinity if index == 0 else house - heaters[index - 1]
22+
rightRadius = infinity if index == len(heaters) else heaters[index] - house
23+
minRadius = max(minRadius, min(leftRadius, rightRadius))
24+
return minRadius

Diff for: src/Heaters.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Arrays;
2+
3+
public class Heaters {
4+
private static int binarySearch(int[] array, int element) {
5+
int left = 0, right = array.length - 1, middle;
6+
while (left <= right) {
7+
middle = (left + right) / 2;
8+
if (array[middle] == element) return middle;
9+
else if (array[middle] < element) left = middle + 1;
10+
else right = middle - 1;
11+
}
12+
return left;
13+
}
14+
15+
public static void main(String[] args) {
16+
System.out.println(findRadius(new int[] {1, 2, 3, 4}, new int[] {1, 4}));
17+
}
18+
19+
public static int findRadius(int[] houses, int[] heaters) {
20+
Arrays.sort(heaters);
21+
int minRadius = 0, leftRadius, rightRadius;
22+
for (int house : houses) {
23+
int index = binarySearch(heaters, house);
24+
if (index < heaters.length && house == heaters[index]) {
25+
continue;
26+
}
27+
leftRadius = index > 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
28+
rightRadius = (index == heaters.length ? Integer.MAX_VALUE : heaters[index] - house);
29+
minRadius = Math.max(minRadius, Math.min(leftRadius, rightRadius));
30+
}
31+
return minRadius;
32+
}
33+
}

0 commit comments

Comments
 (0)