File tree Expand file tree Collapse file tree 3 files changed +48
-3
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 3 files changed +48
-3
lines changed Original file line number Diff line number Diff line change 11"""
2- Blind75 - 3 . Contains Duplicate
2+ Blind75 - 1 . Contains Duplicate
33https://leetcode.com/problems/contains-duplicate/
44
55Counter를 사용한 풀이
Original file line number Diff line number Diff line change 1+ """
2+ blind75 - House Robber
3+ LeetCode Problem: https://leetcode.com/problems/house-robber/
4+
5+ 재귀
6+ F(x) = F(x+1), F(x+2) + nums[x] 중 큰 값을 계속 선택하면 된다
7+ -> 재귀를 2번씩 호출하기 때문에 시간복잡도는 O(2^n)으로 Time Limit Exceeded 발생
8+ 다음 주차 때 다시 풀어보자.
9+ dp로도 될 거 같다
10+ """
11+
12+ from typing import List
13+
14+ class Solution :
15+ def rob (self , nums : List [int ]) -> int :
16+ if not nums :
17+ return 0
18+
19+ def dfs (s ):
20+ if s >= len (nums ):
21+ return 0
22+ return max (dfs (s + 1 ), nums [s ] + dfs (s + 2 ))
23+
24+ return dfs (0 )
25+
26+
Original file line number Diff line number Diff line change 33https://leetcode.com/problems/longest-consecutive-sequence/
44
55조건 : O(n) 시간복잡도
6+ 1. 시작점이면 초기화
7+ 2. 아니면 cnt++
8+ 3. 끊기면 max 갱신
69"""
7-
10+ from typing import List
811
912class Solution :
1013 def longestConsecutive (self , nums : List [int ]) -> int :
11-
14+ if len (nums ) == 0 :
15+ return 0
16+ nums .sort ()
17+ length = 1
18+ max_length = 0
19+ for i in range (len (nums )- 1 ):
20+ if nums [i + 1 ] == nums [i ]:
21+ continue
22+ elif nums [i + 1 ] == nums [i ] + 1 :
23+ length += 1
24+ else :
25+ max_length = max (max_length , length )
26+ length = 1
27+
28+ max_length = max (max_length , length )
29+ return max_length
30+
You can’t perform that action at this time.
0 commit comments