LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game

简介: LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game

LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game


Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

给你一个由 不同 整数组成的整数数组 arr 和一个整数 k 。

每回合游戏都在数组的前两个元素(即 arr[0] 和 arr[1] )之间进行。比较 arr[0] 与 arr[1] 的大小,较大的整数将会取得这一回合的胜利并保留在位置 0 ,较小的整数移至数组的末尾。当一个整数赢得 k 个连续回合时,游戏结束,该整数就是比赛的 赢家 。

返回赢得比赛的整数。

题目数据 保证 游戏存在赢家。

 

示例 1:

输入:arr = [2,1,3,5,4,6,7], k = 2

输出:5

解释:一起看一下本场游戏每回合的情况:

因此将进行 4 回合比赛,其中 5 是赢家,因为它连胜 2 回合。

示例 2:

输入:arr = [3,2,1], k = 10

输出:3

解释:3 将会在前 10 个回合中连续获胜。

示例 3:

输入:arr = [1,9,8,2,3,7,6,4,5], k = 7

输出:9

示例 4:

输入:arr = [1,11,22,33,44,55,66,77,88,99], k = 1000000000

输出:99

 

提示:

2 <= arr.length <= 10^5

1 <= arr[i] <= 10^6

arr 所含的整数 各不相同 。

1 <= k <= 10^9


二、英文版

Given an integer array arr of distinct integers and an integer k.

A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0 and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds.

Return the integer which will win the game.

It is guaranteed that there will be a winner of the game.

 

Example 1:
Input: arr = [2,1,3,5,4,6,7], k = 2
Output: 5
Explanation: Let's see the rounds of the game:
Round |       arr       | winner | win_count
  1   | [2,1,3,5,4,6,7] | 2      | 1
  2   | [2,3,5,4,6,7,1] | 3      | 1
  3   | [3,5,4,6,7,1,2] | 5      | 1
  4   | [5,4,6,7,1,2,3] | 5      | 2
So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.
Example 2:
Input: arr = [3,2,1], k = 10
Output: 3
Explanation: 3 will win the first 10 rounds consecutively.
Example 3:
Input: arr = [1,9,8,2,3,7,6,4,5], k = 7
Output: 9
Example 4:
Input: arr = [1,11,22,33,44,55,66,77,88,99], k = 1000000000
Output: 99
Constraints:
2 <= arr.length <= 10^5
1 <= arr[i] <= 10^6
arr contains distinct integers.
1 <= k <= 10^9


三、My answer

class Solution:
    def getWinner(self, arr: List[int], k: int) -> int:
        res = 0
        n = len(arr)
        cnt = 0 # 记录每个数胜利的次数
        i = 0
        idx = 1
        while i + idx < n and cnt < min(n,k):
            if arr[i] < arr[i+idx]:
                i = i+idx
                idx = 1
                cnt = 1
            else:
                cnt += 1
                if cnt == k:
                    return arr[i]
                idx += 1
        return arr[i]


四、解题报告

数据结构:数组

算法:遍历

实现:

1、根据题目限制,不能暴力遍历,肯定会超时,所以想办法只经过一次遍历即可。

2、如果 k 大于 arr 的长度,由于将 arr 中所有数字两两比较之后一定会选出 “赢家”,所以此时不需要遍历 k 次,一个数字只需要赢 min(n,k) 次即可,其中 n 是 arr 的长度。

3、每次比较两个数,如果是连胜,则 count += 1,判断 count 与 k 的关系,如果等于 k,则返回。

4、如果是新的数字,则 idx 和 count 都需要重新归1。 idx 表示当前 i 与往后第 idx 个数比较大小。

如果 i+idx 的数是赢家, i 直接跳到 i + idx 即可,即 i=i+idx。因为期间的数字都败给第 i 个数了

相关文章
|
5天前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
11 3
|
3月前
LeetCode题:174. 地下城游戏
LeetCode题:174. 地下城游戏
37 0
LeetCode题:174. 地下城游戏
|
3月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
7天前
|
存储 索引 Python
多数pythoneer只知有列表list却不知道python也有array数组
多数pythoneer只知有列表list却不知道python也有array数组
15 0
|
15天前
|
JavaScript 前端开发 索引
在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
【4月更文挑战第16天】在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
23 4
|
15天前
|
算法
【力扣】55.跳跃游戏
【力扣】55.跳跃游戏
|
2月前
|
存储 缓存 安全
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
16 0
|
3月前
Google Earth Engine(GEE)——reducer中进行array数组的获取和分析
Google Earth Engine(GEE)——reducer中进行array数组的获取和分析
35 0
|
2月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
2月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记

热门文章

最新文章