Skip to content

Commit e5c0669

Browse files
authored
[chordpli] WEEK 02 solutions (#1776)
1 parent d96744f commit e5c0669

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

3sum/chordpli.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
answer = []
7+
nums.sort()
8+
nums_len = len(nums)
9+
10+
for i in range(nums_len - 2):
11+
if i > 0 and nums[i] == nums[i - 1]:
12+
continue
13+
left, right = i + 1, nums_len - 1
14+
15+
while left < right:
16+
current_sum = nums[i] + nums[left] + nums[right]
17+
if current_sum < 0:
18+
left += 1
19+
elif current_sum > 0:
20+
right -= 1
21+
22+
else:
23+
answer.append([nums[i], nums[left], nums[right]])
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+
left += 1
29+
right -= 1
30+
31+
return answer

climbing-stairs/chordpli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
if n <= 2:
4+
return n
5+
6+
dp = [0] * (n + 1)
7+
dp[1] = 1
8+
dp[2] = 2
9+
10+
for i in range(3, n + 1):
11+
dp[i] = dp[i - 1] + dp[i - 2]
12+
13+
return dp[n]

valid-anagram/chordpli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# dictionary 사용 11ms
3+
def isAnagram(self, s: str, t: str) -> bool:
4+
check = {}
5+
for char in s:
6+
if char not in check:
7+
check[char] = 0
8+
check[char] += 1
9+
for char in t:
10+
if char not in check:
11+
return False
12+
check[char] -= 1
13+
if check[char] == 0:
14+
del check[char]
15+
return not check
16+
17+
# 정렬 사용 15 - 20ms
18+
# def isAnagram(self, s: str, t: str) -> bool:
19+
# return sorted(s) == sorted(t)

0 commit comments

Comments
 (0)