Skip to content

Commit

Permalink
2020-02-11
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Feb 12, 2020
1 parent 138745c commit 5c72180
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution(object):
def kWeakestRows(self, mat, k):
"""
:type mat: List[List[int]]
:type k: int
:rtype: List[int]
"""

res = []
for i, row in enumerate(mat):
res.append((sum(row), i))

return [i for s, i in sorted(res)[:k]]
23 changes: 23 additions & 0 deletions 1338.数组大小减半/1338-数组大小减半.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from heapq import *
from collections import Counter
class Solution(object):
def minSetSize(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
t = len(arr) // 2
dic = Counter(arr)

queue = []
for key, val in dic.items():
heappush(queue, -val)

cnt = 0
res = 0
while cnt < t:
tmp = heappop(queue)
res += 1
cnt += -tmp
# print cnt, tmp, t
return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def maxProduct(self, root):
"""
:type root: TreeNode
:rtype: int
"""
dic = {}

def SumOfTree(node):
if not node:
return 0
ls, rs = SumOfTree(node.left), SumOfTree(node.right)

dic[node] = ls + rs + node.val
return dic[node]

SumOfTree(root)
TotalSum = dic[root]

self.res = 0
def dfs(node):
if not node:
return

tmp = (TotalSum - dic[node]) * dic[node]
self.res = max(self.res, tmp)

dfs(node.left)
dfs(node.right)
dfs(root)
return self.res % (10 ** 9 + 7)
25 changes: 25 additions & 0 deletions 1340.跳跃游戏V/1340-跳跃游戏V.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution(object):
def maxJumps(self, arr, d):
"""
:type arr: List[int]
:type d: int
:rtype: int
"""
res = [(x, i) for i, x in enumerate(arr)]

res.sort()
# print res
dp = [1 for _ in res]

for k in range(len(arr)):
i = res[k][1]
for j in range(1, d + 1):
if i + j == len(arr) or arr[i + j] >= arr[i]:
break
dp[i] = max(dp[i], dp[i + j] + 1)

for j in range(1, d + 1):
if i - j < 0 or arr[i - j] >= arr[i]:
break
dp[i] = max(dp[i], dp[i - j] + 1)
return max(dp)

0 comments on commit 5c72180

Please sign in to comment.