Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc4dbb8

Browse files
feat: add typescript solution to lc problem: No.0475
No.0475.Heaters
1 parent d05dc03 commit dc4dbb8

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

‎solution/0400-0499/0475.Heaters/README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ class Solution {
115115
}
116116
```
117117

118+
### **TypeScript**
119+
120+
```ts
121+
function findRadius(houses: number[], heaters: number[]): number {
122+
houses.sort((a, b) => a - b);
123+
heaters.sort((a, b) => a - b);
124+
const m = houses.length, n = heaters.length;
125+
let ans = 0;
126+
for (let i = 0, j = 0; i < m; i++) {
127+
let cur = Math.abs(houses[i] - heaters[j]);
128+
while (j + 1 < n && (Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1]))) {
129+
cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur);
130+
}
131+
ans = Math.max(cur, ans);
132+
}
133+
return ans;
134+
};
135+
```
136+
118137
### **C++**
119138

120139
```cpp

‎solution/0400-0499/0475.Heaters/README_EN.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ class Solution {
103103
}
104104
```
105105

106+
### **TypeScript**
107+
108+
```ts
109+
function findRadius(houses: number[], heaters: number[]): number {
110+
houses.sort((a, b) => a - b);
111+
heaters.sort((a, b) => a - b);
112+
const m = houses.length, n = heaters.length;
113+
let ans = 0;
114+
for (let i = 0, j = 0; i < m; i++) {
115+
let cur = Math.abs(houses[i] - heaters[j]);
116+
while (j + 1 < n && (Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1]))) {
117+
cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur);
118+
}
119+
ans = Math.max(cur, ans);
120+
}
121+
return ans;
122+
};
123+
```
124+
106125
### **C++**
107126

108127
```cpp
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function findRadius(houses: number[], heaters: number[]): number {
2+
houses.sort((a, b) => a - b);
3+
heaters.sort((a, b) => a - b);
4+
const m = houses.length, n = heaters.length;
5+
let ans = 0;
6+
for (let i = 0, j = 0; i < m; i++) {
7+
let cur = Math.abs(houses[i] - heaters[j]);
8+
while (j + 1 < n && (Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1]))) {
9+
cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur);
10+
}
11+
ans = Math.max(cur, ans);
12+
}
13+
return ans;
14+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /