📌  相关文章
📜  球队需要获胜才能参加的最少比赛

📅  最后修改于: 2021-06-26 12:26:09             🧑  作者: Mango

给定两个整数XY ,其中X表示资格要求的分数Y表示剩下的比赛数。球队在比赛中获2分,输则1分。任务是找到球队赢得下一轮比赛所需的最少比赛数。
例子:

一个幼稚的方法是通过对从0Y的所有值进行迭代来进行检查,并找出给我们X点的第一个值。
一种有效的方法是对要赢得的匹配数执行二进制搜索,以找出最小的匹配数。最初低= 0高= X ,然后检查条件(mid * 2 +(y – mid))≥x 。如果情况仍然存在,则检查左半部分是否存在任何较低的值,即高=中– 1,否则检查右半部分中的任何低值,低=中+ 1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum number of
// matches to win to qualify for next round
int findMinimum(int x, int y)
{
 
    // Do a binary search to find
    int low = 0, high = y;
    while (low <= high) {
 
        // Find mid element
        int mid = (low + high) >> 1;
 
        // Check for condition
        // to qualify for next round
        if ((mid * 2 + (y - mid)) >= x)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return low;
}
 
// Driver Code
int main()
{
    int x = 6, y = 5;
    cout << findMinimum(x, y);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the minimum number of
// matches to win to qualify for next round
static int findMinimum(int x, int y)
{
 
    // Do a binary search to find
    int low = 0, high = y;
    while (low <= high)
    {
 
        // Find mid element
        int mid = (low + high) >> 1;
 
        // Check for condition
        // to qualify for next round
        if ((mid * 2 + (y - mid)) >= x)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return low;
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 6, y = 5;
    System.out.println(findMinimum(x, y));
}
}
 
// This code is contributed by ajit.


Python 3
# Python 3 implementation of the approach
 
# Function to return the minimum number of
# matches to win to qualify for next round
def findMinimum(x, y):
     
    # Do a binary search to find
    low = 0
    high = y
    while (low <= high):
         
        # Find mid element
        mid = (low + high) >> 1
 
        # Check for condition
        # to qualify for next round
        if ((mid * 2 + (y - mid)) >= x):
            high = mid - 1
        else:
            low = mid + 1
    return low
 
# Driver Code
if __name__ == '__main__':
    x = 6
    y = 5
    print(findMinimum(x, y))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
         
// Function to return the minimum number of
// matches to win to qualify for next round
static int findMinimum(int x, int y)
{
 
    // Do a binary search to find
    int low = 0, high = y;
    while (low <= high)
    {
 
        // Find mid element
        int mid = (low + high) >> 1;
 
        // Check for condition
        // to qualify for next round
        if ((mid * 2 + (y - mid)) >= x)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return low;
}
 
// Driver code
static public void Main()
{
    int x = 6, y = 5;
    Console.WriteLine(findMinimum(x, y));
}
}
 
// This Code is contributed by ajit.


PHP
> 1;
 
        // Check for condition$
        // to qualify for next round
        if (($mid * 2 + ($y - $mid)) >= $x)
            $high = $mid - 1;
        else
            $low = $mid + 1;
    }
    return $low;
}
 
// Driver Code
$x = 6; $y = 5;
echo findMinimum($x, $y);
 
// This code has been contributed
// by 29AjayKumar
?>


Javascript


输出:
1

时间复杂度: O(log y)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。