Leetcode Problem 1535. Find the Winner of an Array Game

1535. Find the Winner of an Array Game

Leetcode Solutions

Approach: Simulate Process With Queue

  1. Find the maximum element in arr and store it in maxElement.
  2. Initialize queue with all elements of arr except the first one.
  3. Set curr to the first element of arr and winstreak to 0.
  4. While queue is not empty: a. Dequeue an element from queue and store it in opponent. b. If curr is greater than opponent: i. Increment winstreak. ii. Enqueue opponent to the back of queue. c. Else: i. Set curr to opponent. ii. Reset winstreak to 1. iii. Enqueue the previous curr to the back of queue. d. If winstreak equals k or curr equals maxElement, return curr.
UML Thumbnail

Approach: No Queue

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...