From e599554c23e1695b943eb5a43f76927df6e124c4 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 27 Aug 2024 09:03:18 +0800 Subject: [PATCH] Add solution and test-cases for problem 398 --- .../301-400/0398.Random-Pick-Index/README.md | 34 ++++++++-------- .../0398.Random-Pick-Index/Solution.go | 39 ++++++++++++++++++- .../0398.Random-Pick-Index/Solution_test.go | 20 +++++----- 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/leetcode/301-400/0398.Random-Pick-Index/README.md b/leetcode/301-400/0398.Random-Pick-Index/README.md index c8b7d18e6..34d77b2f8 100644 --- a/leetcode/301-400/0398.Random-Pick-Index/README.md +++ b/leetcode/301-400/0398.Random-Pick-Index/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/301-400/0398.Random-Pick-Index/Solution.go b/leetcode/301-400/0398.Random-Pick-Index/Solution.go index d115ccf5e..fb4c3a73e 100644 --- a/leetcode/301-400/0398.Random-Pick-Index/Solution.go +++ b/leetcode/301-400/0398.Random-Pick-Index/Solution.go @@ -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 } diff --git a/leetcode/301-400/0398.Random-Pick-Index/Solution_test.go b/leetcode/301-400/0398.Random-Pick-Index/Solution_test.go index 14ff50eb4..bdcc6bf9f 100644 --- a/leetcode/301-400/0398.Random-Pick-Index/Solution_test.go +++ b/leetcode/301-400/0398.Random-Pick-Index/Solution_test.go @@ -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() { }