给定n个以随机方式包含数字1-n的整数和一个整数K的列表。N人排在队中打羽毛球。首先,队列中的前两个玩家玩游戏。然后,失败者进入队列的尽头,获胜者与队伍中的下一个人进行比赛,依此类推。他们一直玩到有人连续赢得k场比赛为止。该玩家成为赢家。
例子 :
Input: arr[] = {2, 1, 3, 4, 5}
k = 2
Output: 5
Explanation:
2 plays with 1, 1 goes to end of queue.
2 plays with 3, 3 wins, 2 goes to end of queue.
3 plays with 4, so 3 goes to the end of the queue.
5 plays with everyone and wins as it is the
largest of all elements.
Input: arr[] = {3, 1, 2}
k = 2
Output: 3
Explanation :
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. 3 wins twice in a row.
一个幼稚的方法是运行两个嵌套的for循环,并检查每个元素,从i到n是第一个循环,第二个是从i + 1到n,然后从0到n-1,并计数连续的较小元素并获得答案。
这将效率不高,因为它需要O(n * n) 。
一种有效的方法是运行从1到n的循环,并跟踪迄今为止的最佳(或最大元素)以及小于该最大值的较小元素的数量。如果当前最差,则将较大的值初始化为最佳,并将计数初始化为1,因为获胜者已经赢得1次。如果在任何步骤中赢得了k次,您都会得到答案。但是,如果k> = n-1,则最大的数目将是唯一的答案,因为它将最多的次数是最大的。如果您在迭代时没有找到赢得过k次的任何玩家,那么列表中的最大人数将永远是我们的答案。
下面是上述方法的实现
C++
// C++ program to find winner of game
#include
using namespace std;
int winner(int a[], int n, int k)
{
// if the number of steps is more then
// n-1,
if (k >= n - 1)
return n;
// initially the best is 0 and no of
// wins is 0.
int best = 0, times = 0;
// traverse through all the numbers
for (int i = 0; i < n; i++) {
// if the value of array is more
// then that of previous best
if (a[i] > best) {
// best is replaced by a[i]
best = a[i];
// if not the first index
if (i)
times = 1; // no of wins is 1 now
}
else
times += 1; // if it wins
// if any position has more then k wins
// then return
if (times >= k)
return best;
}
// Maximum element will be winner because
// we move smaller element at end and repeat
// the process.
return best;
}
// driver program to test the above function
int main()
{
int a[] = { 2, 1, 3, 4, 5 };
int n = sizeof(a) / sizeof(a[0]);
int k = 2;
cout << winner(a, n, k);
return 0;
}
Java
// Java program to find winner of game
import java.io.*;
class GFG {
static int winner(int a[], int n, int k)
{
// if the number of steps is more then
// n-1,
if (k >= n - 1)
return n;
// initially the best is 0 and no of
// wins is 0.
int best = 0, times = 0;
// traverse through all the numbers
for (int i = 0; i < n; i++) {
// if the value of array is more
// then that of previous best
if (a[i] > best) {
// best is replaced by a[i]
best = a[i];
// if not the first index
if (i == 1)
// no of wins is 1 now
times = 1;
}
else
// if it wins
times += 1;
// if any position has more then
// k wins then return
if (times >= k)
return best;
}
// Maximum element will be winner
// because we move smaller element
// at end and repeat the process.
return best;
}
// driver program to test the above function
public static void main(String args[])
{
int a[] = { 2, 1, 3, 4, 5 };
int n = a.length;
int k = 2;
System.out.println(winner(a, n, k));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python3
# Python3 code to find
# winner of game
# function to find the winner
def winner( a, n, k):
# if the number of steps is
# more then n-1
if k >= n - 1:
return n
# initially the best is 0
# and no of wins is 0.
best = 0
times = 0
# traverse through all the numbers
for i in range(n):
# if the value of array is more
# then that of previous best
if a[i] > best:
# best is replaced by a[i]
best = a[i]
# if not the first index
if i == True:
# no of wins is 1 now
times = 1
else:
times += 1 # if it wins
# if any position has more
# then k wins then return
if times >= k:
return best
# Maximum element will be winner
# because we move smaller element
# at end and repeat the process.
return best
# driver code
a = [ 2, 1, 3, 4, 5 ]
n = len(a)
k = 2
print(winner(a, n, k))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# program to find winner of game
using System;
class GFG {
static int winner(int[] a, int n, int k)
{
// if the number of steps is more
// then n-1,
if (k >= n - 1)
return n;
// initially the best is 0 and no of
// wins is 0.
int best = 0, times = 0;
// traverse through all the numbers
for (int i = 0; i < n; i++) {
// if the value of array is more
// then that of previous best
if (a[i] > best) {
// best is replaced by a[i]
best = a[i];
// if not the first index
if (i == 1)
// no of wins is 1 now
times = 1;
}
else
// if it wins
times += 1;
// if any position has more
// then k wins then return
if (times >= k)
return best;
}
// Maximum element will be winner
// because we move smaller element
// at end and repeat the process.
return best;
}
// driver program to test the above
// function
public static void Main()
{
int[] a = { 2, 1, 3, 4, 5 };
int n = a.Length;
int k = 2;
Console.WriteLine(winner(a, n, k));
}
}
// This code is contributed by vt_m.
PHP
= $n - 1)
return $n;
// initially the best is 0
// and no. of wins is 0.
$best = 0; $times = 0;
// traverse through all the numbers
for ($i = 0; $i < $n; $i++)
{
// if the value of array is more
// then that of previous best
if ($a[$i] > $best)
{
// best is replaced by a[i]
$best = $a[$i];
// if not the first index
if ($i)
// no of wins is 1 now
$times = 1;
}
else
// if it wins
$times += 1;
// if any position has more then
// k wins then return
if ($times >= $k)
return $best;
}
// Maximum element will be winner
// because we move smaller element
// at end and repeat the process.
return $best;
}
// Driver Code
$a = array( 2, 1, 3, 4, 5 );
$n = sizeof($a);
$k = 2;
echo(winner($a, $n, $k));
// This code is contributed by Ajit.
?>
Javascript
输出 :
5
时间复杂度:O(n)