-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3175-FindTheFirstPlayerToWinKGamesInARow.go
More file actions
82 lines (70 loc) · 3.95 KB
/
3175-FindTheFirstPlayerToWinKGamesInARow.go
File metadata and controls
82 lines (70 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
// 3175. Find The First Player to win K Games in a Row
// A competition consists of n players numbered from 0 to n - 1.
// You are given an integer array skills of size n and a positive integer k, where skills[i] is the skill level of player i.
// All integers in skills are unique.
// All players are standing in a queue in order from player 0 to player n - 1.
// The competition process is as follows:
// 1. The first two players in the queue play a game, and the player with the higher skill level wins.
// 2. After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.
// The winner of the competition is the first player who wins k games in a row.
// Return the initial index of the winning player.
// Example 1:
// Input: skills = [4,2,6,3,9], k = 2
// Output: 2
// Explanation:
// Initially, the queue of players is [0,1,2,3,4]. The following process happens:
// Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is [0,2,3,4,1].
// Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is [2,3,4,1,0].
// Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is [2,4,1,0,3].
// Player 2 won k = 2 games in a row, so the winner is player 2.
// Example 2:
// Input: skills = [2,5,4], k = 3
// Output: 1
// Explanation:
// Initially, the queue of players is [0,1,2]. The following process happens:
// Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
// Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is [1,0,2].
// Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
// Player 1 won k = 3 games in a row, so the winner is player 1.
// Constraints:
// n == skills.length
// 2 <= n <= 10^5
// 1 <= k <= 10^9
// 1 <= skills[i] <= 10^6
// All integers in skills are unique.
import "fmt"
// 双指针
func findWinningPlayer(skills []int, k int) int {
low, high, count := 0, 1, 0
for ; high < len(skills) && count < k; high++ {
if skills[low] > skills[high] {
count++
} else {
count, low = 1, high
}
}
return low
}
func main() {
// Example 1:
// Input: skills = [4,2,6,3,9], k = 2
// Output: 2
// Explanation:
// Initially, the queue of players is [0,1,2,3,4]. The following process happens:
// Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is [0,2,3,4,1].
// Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is [2,3,4,1,0].
// Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is [2,4,1,0,3].
// Player 2 won k = 2 games in a row, so the winner is player 2.
fmt.Println(findWinningPlayer([]int{4,2,6,3,9}, 2)) // 2
// Example 2:
// Input: skills = [2,5,4], k = 3
// Output: 1
// Explanation:
// Initially, the queue of players is [0,1,2]. The following process happens:
// Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
// Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is [1,0,2].
// Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is [1,2,0].
// Player 1 won k = 3 games in a row, so the winner is player 1.
fmt.Println(findWinningPlayer([]int{2,5,4}, 3)) // 1
}