File tree Expand file tree Collapse file tree 5 files changed +106
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode id=217 lang=python3
3+ #
4+ # [217] Contains Duplicate
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def containsDuplicate (self , nums : List [int ]) -> bool :
10+ # list로 하니까 시간 초과 떠서 set으로 변경
11+ idx = set ()
12+ for i in nums :
13+ if i in idx :
14+ return True
15+ idx .add (i )
16+ # null 방지
17+ return False
18+
19+ # @lc code=end
20+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode id=198 lang=python3
3+ #
4+ # [198] House Robber
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def rob (self , nums : List [int ]) -> int :
10+ pre = 0
11+ cur = 0
12+ for num in nums :
13+ pre , cur = cur , max (num + pre , cur )
14+ return cur
15+
16+ # @lc code=end
17+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode id=128 lang=python3
3+ #
4+ # [128] Longest Consecutive Sequence
5+ #
6+ from typing import List
7+ # @lc code=start
8+ class Solution :
9+ def longestConsecutive (self , nums : List [int ]) -> int :
10+ nums .sort ()
11+ long = 0
12+
13+ leng = 1
14+ for i in range (len (nums )- 1 ):
15+ if nums [i ]== nums [i + 1 ]:
16+ continue
17+ if nums [i ]+ 1 == nums [i + 1 ]:
18+ leng += 1
19+ else :
20+ long = max (long ,leng )
21+ leng = 1
22+ # 아래를 추가해야 for문 다 돌았을 때 leng이 최고인 경우 반영
23+ long = max (long ,leng )
24+ return long
25+
26+
27+ # @lc code=end
28+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode id=347 lang=python3
3+ #
4+ # [347] Top K Frequent Elements
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
10+ sol = {}
11+ for i in nums :
12+ sol [i ] = sol .get (i , 0 ) + 1
13+
14+ sorted_items = sorted (sol .items (), key = lambda x : x [1 ], reverse = True )
15+
16+ ssol = []
17+ for i in range (k ):
18+ ssol .append (sorted_items [i ][0 ])
19+
20+ return ssol
21+
22+
23+
24+ # @lc code=end
25+
Original file line number Diff line number Diff line change 1+ #
2+ # @lc app=leetcode id=1 lang=python3
3+ #
4+ # [1] Two Sum
5+ #
6+
7+ # @lc code=start
8+ class Solution :
9+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
10+ for i ,num in enumerate (nums ):
11+ for j in range (i + 1 ,len (nums )):
12+ if num + nums [j ] == target :
13+ return [i ,j ]
14+
15+ # @lc code=end
16+
You can’t perform that action at this time.
0 commit comments