Movatterモバイル変換


[0]ホーム

URL:


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

Commitd159c85

Browse files
authored
Merge pull request6boris#474 from 0xff-dev/master
Add solution and test-cases for problem 2300
2 parents5f412ee +2768b1f commitd159c85

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

‎leetcode/2201-2300/2300.Successful-Pairs-of-Spells-and-Potions/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
#[2300.Successful Pairs of Spells and Potions][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
53

64
##Description
5+
You are given two positive integer arrays`spells` and`potions`, of length`n` and`m` respectively, where`spells[i]` represents the strength of the i<sup>th</sup> spell and`potions[j]` represents the strength of the j<sup>th</sup> potion.
6+
7+
You are also given an integer`success`. A spell and potion pair is considered**successful** if the**product** of their strengths is**at least** success.
8+
9+
Return an integer array`pairs` of length`n` where`pairs[i]` is the number of**potions** that will form a successful pair with the i<sup>th</sup> spell.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
15+
Output: [4,0,3]
16+
Explanation:
17+
- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
18+
- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
19+
- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
20+
Thus, [4,0,3] is returned.
1321
```
1422

15-
##题意
16-
>...
17-
18-
##题解
23+
**Example 2:**
1924

20-
###思路1
21-
>...
22-
Successful Pairs of Spells and Potions
23-
```go
2425
```
25-
26+
Input: spells = [3,1,2], potions = [8,5,8], success = 16
27+
Output: [2,0,2]
28+
Explanation:
29+
- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
30+
- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
31+
- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
32+
Thus, [2,0,2] is returned.
33+
```
2634

2735
##结语
2836

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
import"sort"
4+
5+
funcSolution(spells []int,potions []int,successint64) []int {
6+
ans:=make([]int,len(spells))
7+
sort.Ints(potions)
8+
fori,n:=rangespells {
9+
idx:=0
10+
for ;idx<len(potions);idx++ {
11+
ifint64(potions[idx])*int64(n)>=success {
12+
break
13+
}
14+
}
15+
ans[i]=len(potions)-idx
16+
}
17+
returnans
518
}

‎leetcode/2201-2300/2300.Successful-Pairs-of-Spells-and-Potions/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
funcTestSolution(t*testing.T) {
1010
//测试用例
1111
cases:= []struct {
12-
namestring
13-
inputsbool
14-
expectbool
12+
namestring
13+
s,p []int
14+
successint64
15+
expect []int
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase1", []int{5,1,3}, []int{1,2,3,4,5},7, []int{4,0,3}},
18+
{"TestCase2", []int{3,1,2}, []int{8,5,8},16, []int{2,0,2}},
1919
}
2020

2121
//开始测试
2222
fori,c:=rangecases {
2323
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
24+
got:=Solution(c.s,c.p,c.success)
2525
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect,got,c.s,c.p,c.success)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
funcBenchmarkSolution(b*testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
funcExampleSolution() {
3939
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp