找到以二进制字符串形式给出分数的游戏的获胜者 |设置 2
给定一个表示排球比赛得分的二进制字符串str 。任务是根据以下条件找到比赛的获胜者:
- 在排球比赛中,两队互相比赛,先得到15分的球队获胜,除非两队都达到14分。
- 如果两队都达到 14 分,则保持领先 2 分的球队获胜。
在给定的二进制字符串中, 0表示GEEK 的团队失去一分, 1表示GEEK 的团队赢得一分。任务是找出 GEEK 的团队是赢了还是输了比赛。
例子:
Input: str = “01011111111110110101”
Output: GEEK’S won
Explanation: GEEK wins with score of 15-5
Input: str = “010101010101010101010101010100”
Output: GEEK’s lost
Explanation: The opponent wins with score of 16-14
朴素方法:本题的Set-1中提到了朴素方法。
高效方法:无论时间线如何,最后得分的玩家将获胜。原因如下:
The game ends with someone winning the set and meeting the required conditions to win the game.
- If the game ends by someone reaching the 15 points first then he will be one to win the last set.
- If the game is won by someone maintaining two points lead after reaching 15-14 state, then also the winner will be the one to win the last set maintaining a two point lead.
下面是上述方法的实现。
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the winner
// from given timeline
string findTheWinner(string str, int N)
{
// If last point scored is 1 then
// GEEK is winner
if (str[N - 1] == '1') {
return "GEEK's won";
}
// Else GEEK lost
return "GEEK's lost";
}
// Driver Code
int main()
{
// Input score timeline
string str1 = "01011111111110110101";
int N1 = str1.size();
string str2 = "010101010101010101010101010100";
int N2 = str2.size();
// Print the winner
cout << findTheWinner(str1, N1) << endl;
cout << findTheWinner(str2, N2) << endl;
return 0;
}
Java
// C# implementation of the above approach
class GFG {
// Function to find the winner
// from given timeline
static String findTheWinner(String str, int N)
{
// If last point scored is 1 then
// GEEK is winner
if (str.charAt(N - 1) == '1')
{
return "GEEK's won";
}
// Else GEEK lost
return "GEEK's lost";
}
// Driver Code
public static void main(String args[])
{
// Input score timeline
String str1 = "01011111111110110101";
int N1 = str1.length();
String str2 = "010101010101010101010101010100";
int N2 = str2.length();
// Print the winner
System.out.println(findTheWinner(str1, N1));
System.out.println(findTheWinner(str2, N2));
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python code for the above approach
# Function to find the winner
# from given timeline
def findTheWinner(str, N):
# If last point scored is 1 then
# GEEK is winner
if (str[N - 1] == '1'):
return "GEEK's won"
# Else GEEK lost
return "GEEK's lost"
# Driver Code
# Input score timeline
str1 = "01011111111110110101"
N1 = len(str1)
str2 = "010101010101010101010101010100"
N2 = len(str2)
# Print the winner
print(findTheWinner(str1, N1))
print(findTheWinner(str2, N2))
# This code is contributed by Saurabh Jaiswal
C#
// C# implementation of the above approach
using System;
class GFG{
// Function to find the winner
// from given timeline
static string findTheWinner(string str, int N)
{
// If last point scored is 1 then
// GEEK is winner
if (str[N - 1] == '1')
{
return "GEEK's won";
}
// Else GEEK lost
return "GEEK's lost";
}
// Driver Code
public static void Main()
{
// Input score timeline
string str1 = "01011111111110110101";
int N1 = str1.Length;
string str2 = "010101010101010101010101010100";
int N2 = str2.Length;
// Print the winner
Console.WriteLine(findTheWinner(str1, N1));
Console.WriteLine(findTheWinner(str2, N2));
}
}
// This code is contributed by ukasp
Javascript
输出
GEEK's won
GEEK's lost
时间复杂度: O(1)
辅助空间: O(1)