Skip to content

Commit 14fe5e8

Browse files
committed
2020-08-22
1 parent 63e65b1 commit 14fe5e8

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def countGoodTriplets(self, arr, a, b, c):
3+
"""
4+
:type arr: List[int]
5+
:type a: int
6+
:type b: int
7+
:type c: int
8+
:rtype: int
9+
"""
10+
res = 0
11+
for i in range(len(arr)):
12+
for j in range(i + 1, len(arr)):
13+
for k in range(j + 1, len(arr)):
14+
if abs(arr[i] - arr[j]) <= a and \
15+
abs(arr[j] - arr[k]) <= b and \
16+
abs(arr[i] - arr[k]) <= c:
17+
res += 1
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def getWinner(self, arr, k):
3+
"""
4+
:type arr: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
pre = max(arr[0], arr[1])
9+
cnt = 1
10+
for num in arr[2:]:
11+
if cnt == k:
12+
return pre
13+
if pre > num:
14+
cnt += 1
15+
else:
16+
pre = num
17+
cnt = 1
18+
return pre
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
def minSwaps(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
from collections import defaultdict, deque
8+
dic = dict()
9+
n = len(grid)
10+
for i, row in enumerate(grid):
11+
cnt = 0
12+
for j in range(n - 1, -1, -1):
13+
if not row[j]:
14+
cnt += 1
15+
else:
16+
break
17+
dic[i] = cnt
18+
19+
res = 0
20+
for i in range(n):
21+
zero_cnt = dic[i]
22+
23+
if zero_cnt < n - i - 1:
24+
for j in range(i + 1, n):
25+
if dic[j] >= n - i - 1:
26+
break
27+
if dic[j] < n - i - 1:
28+
return -1
29+
tmp = dic[j]
30+
res += j - i
31+
for k in range(j, i, -1):
32+
dic[k] = dic[k - 1]
33+
34+
return res

0 commit comments

Comments
 (0)