Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c72180

Browse files
committed
2020-02-11
1 parent 138745c commit 5c72180

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def kWeakestRows(self, mat, k):
3+
"""
4+
:type mat: List[List[int]]
5+
:type k: int
6+
:rtype: List[int]
7+
"""
8+
9+
res = []
10+
for i, row in enumerate(mat):
11+
res.append((sum(row), i))
12+
13+
return [i for s, i in sorted(res)[:k]]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from heapq import *
2+
from collections import Counter
3+
class Solution(object):
4+
def minSetSize(self, arr):
5+
"""
6+
:type arr: List[int]
7+
:rtype: int
8+
"""
9+
t = len(arr) // 2
10+
dic = Counter(arr)
11+
12+
queue = []
13+
for key, val in dic.items():
14+
heappush(queue, -val)
15+
16+
cnt = 0
17+
res = 0
18+
while cnt < t:
19+
tmp = heappop(queue)
20+
res += 1
21+
cnt += -tmp
22+
# print cnt, tmp, t
23+
return res
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def maxProduct(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
dic = {}
15+
16+
def SumOfTree(node):
17+
if not node:
18+
return 0
19+
ls, rs = SumOfTree(node.left), SumOfTree(node.right)
20+
21+
dic[node] = ls + rs + node.val
22+
return dic[node]
23+
24+
SumOfTree(root)
25+
TotalSum = dic[root]
26+
27+
self.res = 0
28+
def dfs(node):
29+
if not node:
30+
return
31+
32+
tmp = (TotalSum - dic[node]) * dic[node]
33+
self.res = max(self.res, tmp)
34+
35+
dfs(node.left)
36+
dfs(node.right)
37+
dfs(root)
38+
return self.res % (10 ** 9 + 7)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def maxJumps(self, arr, d):
3+
"""
4+
:type arr: List[int]
5+
:type d: int
6+
:rtype: int
7+
"""
8+
res = [(x, i) for i, x in enumerate(arr)]
9+
10+
res.sort()
11+
# print res
12+
dp = [1 for _ in res]
13+
14+
for k in range(len(arr)):
15+
i = res[k][1]
16+
for j in range(1, d + 1):
17+
if i + j == len(arr) or arr[i + j] >= arr[i]:
18+
break
19+
dp[i] = max(dp[i], dp[i + j] + 1)
20+
21+
for j in range(1, d + 1):
22+
if i - j < 0 or arr[i - j] >= arr[i]:
23+
break
24+
dp[i] = max(dp[i], dp[i - j] + 1)
25+
return max(dp)

0 commit comments

Comments
 (0)