Skip to content

Commit

Permalink
Add solution and test-cases for problem 911
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Aug 12, 2024
1 parent 9f83439 commit d18fe4e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 29 deletions.
37 changes: 21 additions & 16 deletions leetcode/901-1000/0911.Online-Election/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
71 changes: 69 additions & 2 deletions leetcode/901-1000/0911.Online-Election/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 9 additions & 11 deletions leetcode/901-1000/0911.Online-Election/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
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() {
}

0 comments on commit d18fe4e

Please sign in to comment.