From d18fe4e721ff07e6ff24de2b7158a88bfd4e5415 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 12 Aug 2024 09:36:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 911 --- .../901-1000/0911.Online-Election/README.md | 37 +++++----- .../901-1000/0911.Online-Election/Solution.go | 71 ++++++++++++++++++- .../0911.Online-Election/Solution_test.go | 20 +++--- 3 files changed, 99 insertions(+), 29 deletions(-) diff --git a/leetcode/901-1000/0911.Online-Election/README.md b/leetcode/901-1000/0911.Online-Election/README.md index 408e9ea8c..61b4a762a 100644 --- a/leetcode/901-1000/0911.Online-Election/README.md +++ b/leetcode/901-1000/0911.Online-Election/README.md @@ -1,28 +1,33 @@ # [911.Online Election][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 +You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`. -**Example 1:** +For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Implement the `TopVotedCandidate` class: -## 题意 -> ... +- `TopVotedCandidate(int[] persons, int[] times)` Initializes the object with the `persons` and `times` arrays. +- `int q(int t)` Returns the number of the person that was leading the election at time `t` according to the mentioned rules. -## 题解 +**Example 1:** -### 思路1 -> ... -Online Election -```go ``` - +Input +["TopVotedCandidate", "q", "q", "q", "q", "q", "q"] +[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]] +Output +[null, 0, 1, 1, 0, 0, 1] + +Explanation +TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]); +topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading. +topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading. +topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.) +topVotedCandidate.q(15); // return 0 +topVotedCandidate.q(24); // return 0 +topVotedCandidate.q(8); // return 1 +``` ## 结语 diff --git a/leetcode/901-1000/0911.Online-Election/Solution.go b/leetcode/901-1000/0911.Online-Election/Solution.go index d115ccf5e..eecc7099b 100644 --- a/leetcode/901-1000/0911.Online-Election/Solution.go +++ b/leetcode/901-1000/0911.Online-Election/Solution.go @@ -1,5 +1,72 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "sort" +) + +type TopVotedCandidate struct { + matrix [][]int + mm []int + times []int +} + +func Constructor(persons []int, times []int) TopVotedCandidate { + l := len(times) + + matrix := make([][]int, l) + for i := 0; i < l; i++ { + matrix[i] = make([]int, l) + } + mm := make([]int, l) + voteTime := make([]int, l) + for i := range l { + voteTime[i] = -1 + } + matrix[0][persons[0]]++ + mm[0] = persons[0] + voteTime[persons[0]] = times[0] + + for i := 1; i < l; i++ { + matrix[i][persons[i]]++ + voteTime[persons[i]] = times[i] + + selectuser := 0 + maxvalue := 0 + for c := range matrix[i] { + matrix[i][c] += matrix[i-1][c] + if matrix[i][c] == maxvalue && maxvalue != 0 { + if voteTime[c] > voteTime[selectuser] { + selectuser = c + } + } + + if matrix[i][c] > maxvalue { + selectuser = c + maxvalue = matrix[i][c] + } + } + mm[i] = selectuser + } + return TopVotedCandidate{matrix: matrix, times: times, mm: mm} +} + +func (this *TopVotedCandidate) Q(t int) int { + l := len(this.times) + index := sort.Search(l, func(i int) bool { + return this.times[i] >= t + }) + if index == l || this.times[index] != t { + // 没有 + index-- + } + return this.mm[index] +} + +func Solution(persons, times []int, ops []int) []int { + ans := make([]int, len(ops)) + c := Constructor(persons, times) + for i := range ops { + ans[i] = c.Q(ops[i]) + } + return ans } diff --git a/leetcode/901-1000/0911.Online-Election/Solution_test.go b/leetcode/901-1000/0911.Online-Election/Solution_test.go index 14ff50eb4..58a126213 100644 --- a/leetcode/901-1000/0911.Online-Election/Solution_test.go +++ b/leetcode/901-1000/0911.Online-Election/Solution_test.go @@ -9,31 +9,29 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + persons, times, ops []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{0, 1, 1, 0, 0, 1, 0}, []int{0, 5, 10, 15, 20, 25, 30}, []int{3, 12, 25, 15, 24, 8}, []int{0, 1, 1, 0, 0, 1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.persons, c.times, c.ops) 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 %v", + c.expect, got, c.persons, c.times, c.ops) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }