Skip to content

Commit c9df0f1

Browse files
authored
feat: add solutions to lc problem: No.1535 (doocs#1261)
No.1535.Find the Winner of an Array Game
1 parent 5080d41 commit c9df0f1

File tree

7 files changed

+270
-18
lines changed

7 files changed

+270
-18
lines changed

solution/1500-1599/1535.Find the Winner of an Array Game/README.md

+99-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,120 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:脑筋急转弯**
63+
64+
我们注意到,每次会比较数组的前两个元素,不管结果怎么样,下一次的比较,一定是轮到了数组中的下一个元素和当前的胜者进行比较。因此,如果循环了 $n-1$ 次,那么最后的胜者一定是数组中的最大元素。否则,如果某个元素连续胜出了 $k$ 次,那么这个元素就是最后的胜者。
65+
66+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
6571

6672
<!-- 这里可写当前语言的特殊实现逻辑 -->
6773

6874
```python
69-
75+
class Solution:
76+
def getWinner(self, arr: List[int], k: int) -> int:
77+
mx = arr[0]
78+
cnt = 0
79+
for x in arr[1:]:
80+
if mx < x:
81+
mx = x
82+
cnt = 1
83+
else:
84+
cnt += 1
85+
if cnt == k:
86+
break
87+
return mx
7088
```
7189

7290
### **Java**
7391

7492
<!-- 这里可写当前语言的特殊实现逻辑 -->
7593

7694
```java
95+
class Solution {
96+
public int getWinner(int[] arr, int k) {
97+
int mx = arr[0];
98+
for (int i = 1, cnt = 0; i < arr.length; ++i) {
99+
if (mx < arr[i]) {
100+
mx = arr[i];
101+
cnt = 1;
102+
} else {
103+
++cnt;
104+
}
105+
if (cnt == k) {
106+
break;
107+
}
108+
}
109+
return mx;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int getWinner(vector<int>& arr, int k) {
120+
int mx = arr[0];
121+
for (int i = 1, cnt = 0; i < arr.size(); ++i) {
122+
if (mx < arr[i]) {
123+
mx = arr[i];
124+
cnt = 1;
125+
} else {
126+
++cnt;
127+
}
128+
if (cnt == k) {
129+
break;
130+
}
131+
}
132+
return mx;
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func getWinner(arr []int, k int) int {
141+
mx, cnt := arr[0], 0
142+
for _, x := range arr[1:] {
143+
if mx < x {
144+
mx = x
145+
cnt = 1
146+
} else {
147+
cnt++
148+
}
149+
if cnt == k {
150+
break
151+
}
152+
}
153+
return mx
154+
}
155+
```
77156

157+
### **TypeScript**
158+
159+
```ts
160+
function getWinner(arr: number[], k: number): number {
161+
let mx = arr[0];
162+
let cnt = 0;
163+
for (const x of arr.slice(1)) {
164+
if (mx < x) {
165+
mx = x;
166+
cnt = 1;
167+
} else {
168+
++cnt;
169+
}
170+
if (cnt === k) {
171+
break;
172+
}
173+
}
174+
return mx;
175+
}
78176
```
79177

80178
### **...**

solution/1500-1599/1535.Find the Winner of an Array Game/README_EN.md

+93-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,105 @@ So we can see that 4 rounds will be played and 5 is the winner because it wins 2
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def getWinner(self, arr: List[int], k: int) -> int:
57+
mx = arr[0]
58+
cnt = 0
59+
for x in arr[1:]:
60+
if mx < x:
61+
mx = x
62+
cnt = 1
63+
else:
64+
cnt += 1
65+
if cnt == k:
66+
break
67+
return mx
5668
```
5769

5870
### **Java**
5971

6072
```java
73+
class Solution {
74+
public int getWinner(int[] arr, int k) {
75+
int mx = arr[0];
76+
for (int i = 1, cnt = 0; i < arr.length; ++i) {
77+
if (mx < arr[i]) {
78+
mx = arr[i];
79+
cnt = 1;
80+
} else {
81+
++cnt;
82+
}
83+
if (cnt == k) {
84+
break;
85+
}
86+
}
87+
return mx;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int getWinner(vector<int>& arr, int k) {
98+
int mx = arr[0];
99+
for (int i = 1, cnt = 0; i < arr.size(); ++i) {
100+
if (mx < arr[i]) {
101+
mx = arr[i];
102+
cnt = 1;
103+
} else {
104+
++cnt;
105+
}
106+
if (cnt == k) {
107+
break;
108+
}
109+
}
110+
return mx;
111+
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
func getWinner(arr []int, k int) int {
119+
mx, cnt := arr[0], 0
120+
for _, x := range arr[1:] {
121+
if mx < x {
122+
mx = x
123+
cnt = 1
124+
} else {
125+
cnt++
126+
}
127+
if cnt == k {
128+
break
129+
}
130+
}
131+
return mx
132+
}
133+
```
61134

135+
### **TypeScript**
136+
137+
```ts
138+
function getWinner(arr: number[], k: number): number {
139+
let mx = arr[0];
140+
let cnt = 0;
141+
for (const x of arr.slice(1)) {
142+
if (mx < x) {
143+
mx = x;
144+
cnt = 1;
145+
} else {
146+
++cnt;
147+
}
148+
if (cnt === k) {
149+
break;
150+
}
151+
}
152+
return mx;
153+
}
62154
```
63155

64156
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int getWinner(vector<int>& arr, int k) {
4+
int mx = arr[0];
5+
for (int i = 1, cnt = 0; i < arr.size(); ++i) {
6+
if (mx < arr[i]) {
7+
mx = arr[i];
8+
cnt = 1;
9+
} else {
10+
++cnt;
11+
}
12+
if (cnt == k) {
13+
break;
14+
}
15+
}
16+
return mx;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func getWinner(arr []int, k int) int {
2+
mx, cnt := arr[0], 0
3+
for _, x := range arr[1:] {
4+
if mx < x {
5+
mx = x
6+
cnt = 1
7+
} else {
8+
cnt++
9+
}
10+
if cnt == k {
11+
break
12+
}
13+
}
14+
return mx
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
class Solution {
2-
public int getWinner(int[] arr, int k) {
3-
int time = 0, max = arr[0];
4-
for (int i = 1; i < arr.length; i++) {
5-
if (max > arr[i]) {
6-
time++;
7-
} else {
8-
time = 1;
9-
max = arr[i];
10-
}
11-
if (time >= k) {
12-
return max;
13-
}
14-
}
15-
return max;
16-
}
1+
class Solution {
2+
public int getWinner(int[] arr, int k) {
3+
int mx = arr[0];
4+
for (int i = 1, cnt = 0; i < arr.length; ++i) {
5+
if (mx < arr[i]) {
6+
mx = arr[i];
7+
cnt = 1;
8+
} else {
9+
++cnt;
10+
}
11+
if (cnt == k) {
12+
break;
13+
}
14+
}
15+
return mx;
16+
}
1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def getWinner(self, arr: List[int], k: int) -> int:
3+
mx = arr[0]
4+
cnt = 0
5+
for x in arr[1:]:
6+
if mx < x:
7+
mx = x
8+
cnt = 1
9+
else:
10+
cnt += 1
11+
if cnt == k:
12+
break
13+
return mx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function getWinner(arr: number[], k: number): number {
2+
let mx = arr[0];
3+
let cnt = 0;
4+
for (const x of arr.slice(1)) {
5+
if (mx < x) {
6+
mx = x;
7+
cnt = 1;
8+
} else {
9+
++cnt;
10+
}
11+
if (cnt === k) {
12+
break;
13+
}
14+
}
15+
return mx;
16+
}

0 commit comments

Comments
 (0)