File tree Expand file tree Collapse file tree 5 files changed +67
-0
lines changed
longest-consecutive-sequence
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def containsDuplicate (self , nums : List [int ]) -> bool :
3+
4+ return len (nums ) != len (set (nums ))
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def rob (self , nums : List [int ]) -> int :
3+ n = len (nums )
4+
5+ if n == 1 :
6+ return nums [0 ]
7+
8+ dp = [0 ] * n
9+ dp [0 ] = nums [0 ]
10+ dp [1 ] = max (nums [0 ], nums [1 ])
11+
12+ # i 번째를 털지 않은 조건과 i번째를 털은 조건 중 max를 비교
13+ for i in range (2 , n ):
14+ dp [i ] = max (dp [i - 1 ], dp [i - 2 ] + nums [i ])
15+
16+ return dp [- 1 ]
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestConsecutive (self , nums : List [int ]) -> int :
3+ if len (nums ) == 0 :
4+ return 0
5+
6+ nums = sorted (set (nums ))
7+
8+ currLength = 1
9+ lastNum = nums [0 ]
10+ result = 1
11+
12+ for i in range (1 , len (nums )):
13+ if nums [i ] == lastNum + 1 :
14+ currLength += 1
15+ else :
16+ currLength = 1
17+
18+ result = max (result , currLength )
19+ lastNum = nums [i ]
20+
21+ return result
Original file line number Diff line number Diff line change 1+ class Solution :
2+ # prefix, postfix probelm :(
3+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
4+ output = [1 ] * len (nums )
5+
6+ prefix = 1
7+ for i in range (len (nums )):
8+ output [i ] = prefix
9+ prefix *= nums [i ]
10+
11+ postfix = 1
12+ for i in range (len (nums ) - 1 , - 1 , - 1 ):
13+ output [i ] *= postfix
14+ postfix *= nums [i ]
15+
16+ return output
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
3+ hashmap = {}
4+ for i , num in enumerate (nums ):
5+ complement = target - num
6+ if complement in hashmap :
7+ return [hashmap [complement ], i ]
8+ hashmap [num ] = i
9+
10+
You can’t perform that action at this time.
0 commit comments