📌  相关文章
📜  使用单次移动由 k 移动的数组元素

📅  最后修改于: 2021-10-27 06:21:41             🧑  作者: Mango

给定一个由 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程