Skip to content

Commit 1c298b3

Browse files
authored
Merge pull request #23 from saysimple0828/week1-saysimple
[saysimple] 1주차 문제 풀이입니다.
2 parents 72712e8 + e089cae commit 1c298b3

File tree

5 files changed

+74
-0
lines changed

5 files changed

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

contains-duplicate/saysimple.py

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

two-sum/saysimple.py

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

valid-anagram/saysimple.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
https://leetcode.com/problems/valid-anagram/
3+
"""
4+
# - time complexity : O(n)
5+
# - space complexity : O(n)
6+
7+
from collections import Counter
8+
9+
class Solution:
10+
def isAnagram(self, s: str, t: str) -> bool:
11+
return Counter(s) == Counter(t)

valid-palindrome/saysimple.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
https://leetcode.com/problems/valid-palindrome/
3+
"""
4+
# - time complexity : O(n)
5+
# - space complexity : O(n)
6+
7+
class Solution:
8+
def isPalindrome(self, s: str) -> bool:
9+
s = "".join([i for i in s if i.isalnum()]).lower()
10+
return s == s[::-1]

0 commit comments

Comments
 (0)