File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def rob (self , nums : List [int ]) -> int :
3
+ memo = {}
4
+
5
+ def getAmount (start : int ) -> int :
6
+ if not start < len (nums ):
7
+ memo [start ] = 0
8
+ if start in memo :
9
+ return memo [start ]
10
+
11
+ memo [start ] = max (nums [start ] + getAmount (start + 2 ), getAmount (start + 1 ))
12
+
13
+ return memo [start ]
14
+
15
+ return getAmount (0 )
16
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def longestConsecutive (self , nums : List [int ]) -> int :
3
+ result , last = 0 , None
4
+ candidates = []
5
+
6
+ for num in sorted (set (nums )):
7
+ if last is None or last + 1 == num :
8
+ result += 1
9
+ else :
10
+ candidates .append (result )
11
+ result = 1
12
+ last = num
13
+
14
+ if result is not 0 :
15
+ candidates .append (result )
16
+
17
+ return max (candidates ) if len (candidates ) > 0 else 0
18
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
3
+ counters = {}
4
+
5
+ for num in nums :
6
+ if counters .get (num ):
7
+ counters [num ] += 1
8
+ else :
9
+ counters [num ] = 1
10
+
11
+ return [val [0 ] for val in sorted (counters .items (), key = lambda x : x [1 ], reverse = True )[:k ]]
12
+
You can’t perform that action at this time.
0 commit comments