Determine the Winner of a Bowling Game — LeetCode 2660 Python Solution

EasyArraySimulation
Problem
#2660
Reading time
2 min

The problem

You are given two 0-indexed integer arrays player1 and player2, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling game consists of n turns, and the number of pins in each turn is exactly 10.

Python solution

Python
class Solution:
    def isWinner(self, player1: List[int], player2: List[int]) -> int:
        def f(arr: List[int]) -> int:
            s = 0
            for i, x in enumerate(arr):
                k = 2 if (i and arr[i - 1] == 10) or (i > 1 and arr[i - 2] == 10) else 1
                s += k * x
            return s

        a, b = f(player1), f(player2)
        return 1 if a > b else (2 if b > a else 0)

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2660. Determine the Winner of a Bowling Game?
LeetCode 2660. Determine the Winner of a Bowling Game is rated Easy on LeetCode.
What is the time complexity of LeetCode 2660. Determine the Winner of a Bowling Game?
The Python solution on this page runs in O(n), where n is the length of the array.
What is the space complexity of LeetCode 2660. Determine the Winner of a Bowling Game?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2660. Determine the Winner of a Bowling Game cover?
LeetCode 2660. Determine the Winner of a Bowling Game is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview