File tree Expand file tree Collapse file tree 4 files changed +99
-0
lines changed Expand file tree Collapse file tree 4 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
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 ]]
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments