Skip to content

Commit 0375d6b

Browse files
committed
Add solution #1535
1 parent af5a1f4 commit 0375d6b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@
411411
1512|[Number of Good Pairs](./1512-number-of-good-pairs.js)|Easy|
412412
1519|[Number of Nodes in the Sub-Tree With the Same Label](./1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
413413
1528|[Shuffle String](./1528-shuffle-string.js)|Easy|
414+
1535|[Find the Winner of an Array Game](./1535-find-the-winner-of-an-array-game.js)|Medium|
414415
1550|[Three Consecutive Odds](./1550-three-consecutive-odds.js)|Easy|
415416
1551|[Minimum Operations to Make Array Equal](./1551-minimum-operations-to-make-array-equal.js)|Medium|
416417
1566|[Detect Pattern of Length M Repeated K or More Times](./1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1535. Find the Winner of an Array Game
3+
* https://leetcode.com/problems/find-the-winner-of-an-array-game/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array arr of distinct integers and an integer k.
7+
*
8+
* A game will be played between the first two elements of the array (i.e. arr[0] and arr[1]).
9+
* In each round of the game, we compare arr[0] with arr[1], the larger integer wins and
10+
* remains at position 0, and the smaller integer moves to the end of the array. The game
11+
* ends when an integer wins k consecutive rounds.
12+
*
13+
* Return the integer which will win the game.
14+
*
15+
* It is guaranteed that there will be a winner of the game.
16+
*/
17+
18+
/**
19+
* @param {number[]} arr
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var getWinner = function(arr, k) {
24+
let result = arr[0];
25+
26+
for (let i = 1, count = 0; i < arr.length && count < k; i++) {
27+
if (result < arr[i]) {
28+
result = arr[i];
29+
count = 0;
30+
}
31+
count++;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)