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 6e8af70

Browse files
feat: add typescript solution to lc problem: No.2300
No.2300.Successful Pairs of Spells and Potions
1 parent fa844b1 commit 6e8af70

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,34 @@ func successfulPairs(spells []int, potions []int, success int64) []int {
157157
### **TypeScript**
158158

159159
```ts
160+
function successfulPairs(spells: number[], potions: number[], success: number): number[] {
161+
const n = spells.length, m = potions.length;
162+
potions.sort((a, b) => a - b);
163+
let pairs = new Array(n);
164+
let hashMap = new Map();
165+
for (let i = 0; i < n; i++) {
166+
const target = Math.ceil(success / spells[i]);
167+
let idx = hashMap.get(target);
168+
if (!idx) {
169+
idx = searchLeft(potions, 0, m, target);
170+
hashMap.set(target, idx);
171+
}
172+
pairs[i] = m - idx;
173+
}
174+
return pairs;
175+
};
160176

177+
function searchLeft(nums, left, right, target) {
178+
while (left < right) {
179+
let mid = (left + right) >> 1;
180+
if (nums[mid] >= target) {
181+
right = mid;
182+
} else {
183+
left = mid + 1;
184+
}
185+
}
186+
return left;
187+
}
161188
```
162189

163190
### **...**

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,34 @@ func successfulPairs(spells []int, potions []int, success int64) []int {
147147
### **TypeScript**
148148

149149
```ts
150+
function successfulPairs(spells: number[], potions: number[], success: number): number[] {
151+
const n = spells.length, m = potions.length;
152+
potions.sort((a, b) => a - b);
153+
let pairs = new Array(n);
154+
let hashMap = new Map();
155+
for (let i = 0; i < n; i++) {
156+
const target = Math.ceil(success / spells[i]);
157+
let idx = hashMap.get(target);
158+
if (!idx) {
159+
idx = searchLeft(potions, 0, m, target);
160+
hashMap.set(target, idx);
161+
}
162+
pairs[i] = m - idx;
163+
}
164+
return pairs;
165+
};
150166

167+
function searchLeft(nums, left, right, target) {
168+
while (left < right) {
169+
let mid = (left + right) >> 1;
170+
if (nums[mid] >= target) {
171+
right = mid;
172+
} else {
173+
left = mid + 1;
174+
}
175+
}
176+
return left;
177+
}
151178
```
152179

153180
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function successfulPairs(spells: number[], potions: number[], success: number): number[] {
2+
const n = spells.length, m = potions.length;
3+
potions.sort((a, b) => a - b);
4+
let pairs = new Array(n);
5+
let hashMap = new Map();
6+
for (let i = 0; i < n; i++) {
7+
const target = Math.ceil(success / spells[i]);
8+
let idx = hashMap.get(target);
9+
if (!idx) {
10+
idx = searchLeft(potions, 0, m, target);
11+
hashMap.set(target, idx);
12+
}
13+
pairs[i] = m - idx;
14+
}
15+
return pairs;
16+
};
17+
18+
function searchLeft(nums, left, right, target) {
19+
while (left < right) {
20+
let mid = (left + right) >> 1;
21+
if (nums[mid] >= target) {
22+
right = mid;
23+
} else {
24+
left = mid + 1;
25+
}
26+
}
27+
return left;
28+
}

0 commit comments

Comments
(0)

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