File tree Expand file tree Collapse file tree 5 files changed +82
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ class Solution :
4+ def containsDuplicate (self , nums : List [int ]) -> bool :
5+ dict = {}
6+ for num in nums :
7+ dict [num ] = dict .get (num , 0 ) + 1
8+ if dict [num ] > 1 :
9+ return True
10+ return False
11+
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def rob (self , nums : List [int ]) -> int :
6+ dp = [0 ] * len (nums )
7+
8+ for i in range (len (nums )- 1 , - 1 , - 1 ):
9+ dpMax = 0
10+ for j in range (i + 2 , len (nums )):
11+ dpMax = max (dpMax , dp [j ])
12+ dp [i ] = nums [i ] + dpMax
13+
14+ return max (dp )
15+
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Node :
5+ def __init__ (self , value ):
6+ self .value = value
7+ self .parent = None
8+ self .child = None
9+
10+ class Solution :
11+ def longestConsecutive (self , nums : List [int ]) -> int :
12+ answer = 0
13+ dict = {}
14+
15+ for num in nums :
16+ if dict .get (num ) is None :
17+ dict [num ] = Node (num )
18+ if dict .get (num + 1 ) is not None :
19+ dict [num + 1 ].child = dict [num ]
20+ dict [num ].parent = dict [num + 1 ]
21+
22+ if dict .get (num - 1 ) is not None :
23+ dict [num ].child = dict [num - 1 ]
24+ dict [num - 1 ].parent = dict [num ]
25+
26+ for key in dict .keys ():
27+ if dict [key ].parent is None :
28+ node = dict [key ]
29+ count = 1
30+
31+ while node .child is not None :
32+ count += 1
33+ node = node .child
34+
35+ answer = max (answer , count )
36+
37+ return answer
38+
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ class Solution :
4+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
5+ dict = {}
6+ for num in nums :
7+ dict [num ] = dict .get (num , 0 ) + 1
8+
9+ return sorted (dict .keys (), key = lambda x : dict [x ], reverse = True )[:k ]
10+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isPalindrome (self , s : str ) -> bool :
3+ s = s .lower ()
4+
5+ s = '' .join (filter (str .isalnum , s ))
6+
7+ return s == s [::- 1 ]
8+
You can’t perform that action at this time.
0 commit comments