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 2e58403

Browse files
committed
feat: add solutions to lc problem: No.2300
No.2300.Successful Pairs of Spells and Potions
1 parent fa76593 commit 2e58403

File tree

8 files changed

+78
-152
lines changed

8 files changed

+78
-152
lines changed

‎solution/2200-2299/2296.Design a Text Editor/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class TextEditor:
2-
32
def __init__(self):
43
self.left = []
54
self.right = []
@@ -25,6 +24,7 @@ def cursorRight(self, k: int) -> str:
2524
self.left.append(self.right.pop())
2625
return ''.join(self.left[-10:])
2726

27+
2828
# Your TextEditor object will be instantiated and called as such:
2929
# obj = TextEditor()
3030
# obj.addText(text)

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

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
**方法一:二分查找**
55+
**方法一:排序 + 二分查找**
56+
57+
我们可以对药水数组进行排序,然后遍历咒语数组,对于每个咒语,利用二分查找找到第一个大于等于 `success / v` 的药水,下标记为 $i,ドル那么药水的长度减去 $i$ 即为能跟该咒语成功组合的药水数目。
58+
59+
时间复杂度 $O((m + n) \times \log m),ドル空间复杂度 $O(\log n)$。其中 $m$ 和 $n$ 分别为药水数组和咒语数组的长度。
5660

5761
<!-- tabs:start -->
5862

@@ -67,7 +71,7 @@ class Solution:
6771
) -> List[int]:
6872
potions.sort()
6973
m = len(potions)
70-
return [m - bisect_left(potions, success, key=lambdax: s * x) for s in spells]
74+
return [m - bisect_left(potions, success/ v) for v in spells]
7175
```
7276

7377
### **Java**
@@ -78,7 +82,7 @@ class Solution:
7882
class Solution {
7983
public int[] successfulPairs(int[] spells, int[] potions, long success) {
8084
Arrays.sort(potions);
81-
int m = potions.length, n = spells.length;
85+
int n = spells.length, m = potions.length;
8286
int[] ans = new int[n];
8387
for (int i = 0; i < n; ++i) {
8488
int left = 0, right = m;
@@ -104,18 +108,11 @@ class Solution {
104108
public:
105109
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
106110
sort(potions.begin(), potions.end());
107-
int m = potions.size();
108111
vector<int> ans;
109-
for (int& s : spells) {
110-
int left = 0, right = m;
111-
while (left < right) {
112-
int mid = (left + right) >> 1;
113-
if (1ll * s * potions[mid] >= success)
114-
right = mid;
115-
else
116-
left = mid + 1;
117-
}
118-
ans.push_back(m - left);
112+
int m = potions.size();
113+
for (int& v : spells) {
114+
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
115+
ans.push_back(m - i);
119116
}
120117
return ans;
121118
}
@@ -125,21 +122,12 @@ public:
125122
### **Go**
126123
127124
```go
128-
func successfulPairs(spells []int, potions []int, success int64) []int {
125+
func successfulPairs(spells []int, potions []int, success int64) (ans []int) {
129126
sort.Ints(potions)
130127
m := len(potions)
131-
var ans []int
132-
for _, s := range spells {
133-
left, right := 0, m
134-
for left < right {
135-
mid := (left + right) >> 1
136-
if int64(s*potions[mid]) >= success {
137-
right = mid
138-
} else {
139-
left = mid + 1
140-
}
141-
}
142-
ans = append(ans, m-left)
128+
for _, v := range spells {
129+
i := sort.Search(m, func(i int) bool { return int64(potions[i]*v) >= success })
130+
ans = append(ans, m-i)
143131
}
144132
return ans
145133
}
@@ -153,33 +141,23 @@ function successfulPairs(
153141
potions: number[],
154142
success: number,
155143
): number[] {
156-
const n = spells.length,
157-
m = potions.length;
158144
potions.sort((a, b) => a - b);
159-
let pairs = new Array(n);
160-
let hashMap = new Map();
161-
for (let i = 0; i < n; i++) {
162-
const target = Math.ceil(success / spells[i]);
163-
let idx = hashMap.get(target);
164-
if (!idx) {
165-
idx = searchLeft(potions, 0, m, target);
166-
hashMap.set(target, idx);
167-
}
168-
pairs[i] = m - idx;
169-
}
170-
return pairs;
171-
}
172-
173-
function searchLeft(nums, left, right, target) {
174-
while (left < right) {
175-
let mid = (left + right) >> 1;
176-
if (nums[mid] >= target) {
177-
right = mid;
178-
} else {
179-
left = mid + 1;
145+
const m = potions.length;
146+
const ans: number[] = [];
147+
for (const v of spells) {
148+
let left = 0;
149+
let right = m;
150+
while (left < right) {
151+
const mid = (left + right) >> 1;
152+
if (v * potions[mid] >= success) {
153+
right = mid;
154+
} else {
155+
left = mid + 1;
156+
}
180157
}
158+
ans.push(m - left);
181159
}
182-
return left;
160+
return ans;
183161
}
184162
```
185163

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

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Solution:
5959
) -> List[int]:
6060
potions.sort()
6161
m = len(potions)
62-
return [m - bisect_left(potions, success, key=lambdax: s * x) for s in spells]
62+
return [m - bisect_left(potions, success/ v) for v in spells]
6363
```
6464

6565
### **Java**
@@ -68,7 +68,7 @@ class Solution:
6868
class Solution {
6969
public int[] successfulPairs(int[] spells, int[] potions, long success) {
7070
Arrays.sort(potions);
71-
int m = potions.length, n = spells.length;
71+
int n = spells.length, m = potions.length;
7272
int[] ans = new int[n];
7373
for (int i = 0; i < n; ++i) {
7474
int left = 0, right = m;
@@ -94,18 +94,11 @@ class Solution {
9494
public:
9595
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
9696
sort(potions.begin(), potions.end());
97-
int m = potions.size();
9897
vector<int> ans;
99-
for (int& s : spells) {
100-
int left = 0, right = m;
101-
while (left < right) {
102-
int mid = (left + right) >> 1;
103-
if (1ll * s * potions[mid] >= success)
104-
right = mid;
105-
else
106-
left = mid + 1;
107-
}
108-
ans.push_back(m - left);
98+
int m = potions.size();
99+
for (int& v : spells) {
100+
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
101+
ans.push_back(m - i);
109102
}
110103
return ans;
111104
}
@@ -115,21 +108,12 @@ public:
115108
### **Go**
116109
117110
```go
118-
func successfulPairs(spells []int, potions []int, success int64) []int {
111+
func successfulPairs(spells []int, potions []int, success int64) (ans []int) {
119112
sort.Ints(potions)
120113
m := len(potions)
121-
var ans []int
122-
for _, s := range spells {
123-
left, right := 0, m
124-
for left < right {
125-
mid := (left + right) >> 1
126-
if int64(s*potions[mid]) >= success {
127-
right = mid
128-
} else {
129-
left = mid + 1
130-
}
131-
}
132-
ans = append(ans, m-left)
114+
for _, v := range spells {
115+
i := sort.Search(m, func(i int) bool { return int64(potions[i]*v) >= success })
116+
ans = append(ans, m-i)
133117
}
134118
return ans
135119
}
@@ -143,33 +127,23 @@ function successfulPairs(
143127
potions: number[],
144128
success: number,
145129
): number[] {
146-
const n = spells.length,
147-
m = potions.length;
148130
potions.sort((a, b) => a - b);
149-
let pairs = new Array(n);
150-
let hashMap = new Map();
151-
for (let i = 0; i < n; i++) {
152-
const target = Math.ceil(success / spells[i]);
153-
let idx = hashMap.get(target);
154-
if (!idx) {
155-
idx = searchLeft(potions, 0, m, target);
156-
hashMap.set(target, idx);
157-
}
158-
pairs[i] = m - idx;
159-
}
160-
return pairs;
161-
}
162-
163-
function searchLeft(nums, left, right, target) {
164-
while (left < right) {
165-
let mid = (left + right) >> 1;
166-
if (nums[mid] >= target) {
167-
right = mid;
168-
} else {
169-
left = mid + 1;
131+
const m = potions.length;
132+
const ans: number[] = [];
133+
for (const v of spells) {
134+
let left = 0;
135+
let right = m;
136+
while (left < right) {
137+
const mid = (left + right) >> 1;
138+
if (v * potions[mid] >= success) {
139+
right = mid;
140+
} else {
141+
left = mid + 1;
142+
}
170143
}
144+
ans.push(m - left);
171145
}
172-
return left;
146+
return ans;
173147
}
174148
```
175149

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@ class Solution {
22
public:
33
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
44
sort(potions.begin(), potions.end());
5-
int m = potions.size();
65
vector<int> ans;
7-
for (int& s : spells) {
8-
int left = 0, right = m;
9-
while (left < right) {
10-
int mid = (left + right) >> 1;
11-
if (1ll * s * potions[mid] >= success)
12-
right = mid;
13-
else
14-
left = mid + 1;
15-
}
16-
ans.push_back(m - left);
6+
int m = potions.size();
7+
for (int& v : spells) {
8+
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
9+
ans.push_back(m - i);
1710
}
1811
return ans;
1912
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
func successfulPairs(spells []int, potions []int, success int64) []int {
1+
func successfulPairs(spells []int, potions []int, success int64) (ans[]int) {
22
sort.Ints(potions)
33
m := len(potions)
4-
var ans []int
5-
for _, s := range spells {
6-
left, right := 0, m
7-
for left < right {
8-
mid := (left + right) >> 1
9-
if int64(s*potions[mid]) >= success {
10-
right = mid
11-
} else {
12-
left = mid + 1
13-
}
14-
}
15-
ans = append(ans, m-left)
4+
for _, v := range spells {
5+
i := sort.Search(m, func(i int) bool { return int64(potions[i]*v) >= success })
6+
ans = append(ans, m-i)
167
}
178
return ans
189
}

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public int[] successfulPairs(int[] spells, int[] potions, long success) {
33
Arrays.sort(potions);
4-
int m = potions.length, n = spells.length;
4+
int n = spells.length, m = potions.length;
55
int[] ans = new int[n];
66
for (int i = 0; i < n; ++i) {
77
int left = 0, right = m;

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ def successfulPairs(
44
) -> List[int]:
55
potions.sort()
66
m = len(potions)
7-
return [m - bisect_left(potions, success, key=lambdax: s*x) for s in spells]
7+
return [m - bisect_left(potions, success/v) for v in spells]

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts‎

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

0 commit comments

Comments
(0)

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