Skip to content

Commit 7bcf9bc

Browse files
committed
feat: 1주차 문제 풀이
1 parent 7db46d5 commit 7bcf9bc

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3+
"""
4+
5+
class Solution:
6+
def maxProfit(self, prices: List[int]) -> int:
7+
now = prices[0]
8+
diff = [0] * len(prices)
9+
10+
for i, v in enumerate(prices[1:]):
11+
if now > v:
12+
now = v
13+
else:
14+
diff[i+1] = v - now
15+
16+
return max(diff)

contains-duplicate/saysimple.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
https://leetcode.com/problems/contains-duplicate/
3+
"""
4+
5+
class Solution:
6+
def containsDuplicate(self, nums: List[int]) -> bool:
7+
visited = {}
8+
for n in nums:
9+
if n in visited:
10+
return True
11+
visited[n] = True
12+
return False

two-sum/saysimple.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
https://leetcode.com/problems/two-sum/
3+
"""
4+
5+
class Solution:
6+
def twoSum(self, nums: List[int], target: int) -> List[int]:
7+
s, e = 0, len(nums) - 1
8+
arr = [[n, i] for i, n in enumerate(nums)]
9+
10+
arr.sort(key=lambda x: x[0])
11+
12+
while s <= e:
13+
now = arr[s][0] + arr[e][0]
14+
if now == target:
15+
return [arr[s][1], arr[e][1]]
16+
if now < target:
17+
s += 1
18+
else:
19+
e -= 1

valid-anagram/saysimple.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
https://leetcode.com/problems/valid-anagram/
3+
"""
4+
5+
from collections import Counter
6+
7+
class Solution:
8+
def isAnagram(self, s: str, t: str) -> bool:
9+
return Counter(s) == Counter(t)

valid-palindrome/saysimple.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
https://leetcode.com/problems/valid-palindrome/
3+
"""
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
s = "".join([i for i in s if i.isalnum()]).lower()
8+
return s == s[::-1]

0 commit comments

Comments
 (0)