Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 398 #970

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions leetcode/301-400/0398.Random-Pick-Index/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# [398.Random Pick Index][title]

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

## Description
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `targett` number. You can assume that the given target number must exist in the array.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Implement the `Solution` class:

## 题意
> ...
- `Solution(int[] nums)` Initializes the object with the array `nums`.
- `int pick(int target)` Picks a random index `i` from `nums` where `nums[i] == target`. If there are multiple valid i's, then each index should have an equal probability of returning.

## 题解
**Example 1:**

### 思路1
> ...
Random Pick Index
```go
```

Input
["Solution", "pick", "pick", "pick"]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
Output
[null, 4, 0, 2]

Explanation
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
```

## 结语

Expand Down
39 changes: 37 additions & 2 deletions leetcode/301-400/0398.Random-Pick-Index/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
package Solution

func Solution(x bool) bool {
return x
type item398 struct {
index int
list []int
}

type Solution1 struct {
data map[int]item398
}

func Constructor(nums []int) Solution1 {
s := Solution1{data: make(map[int]item398)}
for i, n := range nums {
if _, ok := s.data[n]; !ok {
s.data[n] = item398{}
}
l := s.data[n]
l.list = append(l.list, i)
s.data[n] = l
}
return s
}

func (this *Solution1) Pick(target int) int {
v := this.data[target]
cur := v.index
v.index = (v.index + 1) % len(v.list)
this.data[target] = v
return this.data[target].list[cur]
}

func Solution(nums []int, check []int) []int {
c := Constructor(nums)
ans := make([]int, len(check))
for i, n := range check {
ans[i] = c.Pick(n)
}
return ans
}
20 changes: 9 additions & 11 deletions leetcode/301-400/0398.Random-Pick-Index/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,29 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums, check []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 3, 3}, []int{3, 1, 3}, []int{2, 0, 3}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.check)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.nums, c.check)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading