-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |