File tree Expand file tree Collapse file tree
longest-consecutive-sequence
product-of-array-except-self Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
6+ result : List [List [int ]] = []
7+ nums .sort ()
8+
9+ for i in range (len (nums ) - 2 ):
10+ if i > 0 and nums [i ] == nums [i - 1 ]:
11+ continue
12+
13+ left = i + 1
14+ right = len (nums ) - 1
15+
16+ while left < right :
17+ total = nums [i ] + nums [left ] + nums [right ]
18+
19+ if total == 0 :
20+ result .append ([nums [i ], nums [left ], nums [right ]])
21+ left += 1
22+ right -= 1
23+
24+ while left < right and nums [left ] == nums [left - 1 ]:
25+ left += 1
26+ while left < right and nums [right ] == nums [right + 1 ]:
27+ right -= 1
28+
29+ elif total < 0 :
30+ left += 1
31+ else :
32+ right -= 1
33+
34+ return result
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def climbStairs (self , n : int ) -> int :
3+ stairs = {1 : 1 , 2 : 2 }
4+
5+ for i in range (3 , n + 1 ):
6+ stairs [i ] = stairs [i - 1 ] + stairs [i - 2 ]
7+
8+ return stairs [n ]
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ """
4+ Ideation:
5+ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋ค, ์ธ์ ํ ์ซ์๋ค์ ๋น๊ตํ๋ฉด์ ๊ฐ์ฅ ๊ธด ์ฐ์ ์์ด์ ๊ธธ์ด๋ฅผ ๊ตฌํ๋ค.
6+
7+ - ๊ฐ์ ์ซ์๋ ์ค๋ณต์ด๋ฏ๋ก ๊ฑด๋๋ด๋ค.
8+ - ํ์ฌ ์ซ์ + 1 ์ด ๋ค์ ์ซ์์ ๊ฐ์ผ๋ฉด ์ฐ์๋ ์์ด๋ฏ๋ก ๊ธธ์ด๋ฅผ ์ฆ๊ฐ์ํจ๋ค.
9+ - ์ฐ์์ด ๋๊ธฐ๋ฉด ์ต๋ ๊ธธ์ด๋ฅผ ๊ฐฑ์ ํ๊ณ ๊ธธ์ด๋ฅผ 1๋ก ์ด๊ธฐํํ๋ค.
10+
11+ Time Complexity: O(n log n)
12+ Space Complexity: O(1)
13+ """
14+
15+
16+ class Solution :
17+
18+ def longestConsecutive (self , nums : List [int ]) -> int :
19+ if len (nums ) == 0 :
20+ return 0
21+
22+ longest = 0
23+ length = 1
24+
25+ nums .sort ()
26+
27+ for idx in range (len (nums ) - 1 ):
28+ if nums [idx ] == nums [idx + 1 ]:
29+ continue
30+ if nums [idx ] + 1 == nums [idx + 1 ]:
31+ length += 1
32+ else :
33+ longest = max (longest , length )
34+ length = 1
35+ longest = max (longest , length )
36+ return longest
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
6+ result = [0 ] * len (nums )
7+
8+ prefix_product = 1
9+ for index in range (len (nums )):
10+ result [index ] = prefix_product
11+ prefix_product *= nums [index ]
12+
13+ suffix_product = 1
14+ for index in range (len (nums ) - 1 , - 1 , - 1 ):
15+ result [index ] *= suffix_product
16+ suffix_product *= nums [index ]
17+
18+ return result
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ """
4+ Ideation
5+ ํด์๋งต์ ๊ฐ ์์๋ณ ๋น๋์๋ฅผ ์ ์ฅํ ๋ค, ๋น๋์ ๋์ ์์ผ๋ก ์ ๋ ฌํ ๋ค์ ์์์ k๊ฐ๋ฅผ ๋ฐํํฉ๋๋ค.
6+ Time Complexity: O(n)
7+ Space Complexity: O(n + m log m)
8+ """
9+
10+ class Solution :
11+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
12+ frequency = {}
13+ for num in nums :
14+ frequency [num ] = frequency .get (num , 0 ) + 1
15+ sorted_nums = sorted (frequency , key = lambda num : frequency [num ], reverse = True )
16+
17+ return sorted_nums [:k ]
18+
19+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ Ideation:
4+ ์ ๋๊ทธ๋จ์ ๋ฌธ์์ด ๋ด ๋ฐฐ์น๋ง ๋ค๋ฅผ ๋ฟ, ๋์ผํ ๋ฌธ์๋ค์ด ๋์ผํ ์นด์ดํธ๋ก ๋ณด์ฅ๋์ด์ผ ํ๋ค.
5+ ์ด๋ฅผ ์ํด ๋ฌธ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ๊ฐ์์ง ํ์ธํฉ๋๋ค.
6+ Time Complexity: O(n log n)
7+ Space Complexity: O(1)
8+ """
9+
10+ def isAnagram (self , s : str , t : str ) -> bool :
11+ return sorted (s ) == sorted (t )
You canโt perform that action at this time.
0 commit comments