Skip to content

[saysimple] 1주차 문제 풀이입니다. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
"""
# - time complexity : O(n)
# - space complexity : O(n)

class Solution:
def maxProfit(self, prices: List[int]) -> int:
now = prices[0]
diff = [0] * len(prices)

for i, v in enumerate(prices[1:]):
if now > v:
now = v
else:
diff[i+1] = v - now

return max(diff)
14 changes: 14 additions & 0 deletions contains-duplicate/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
https://leetcode.com/problems/contains-duplicate/
"""
# - time complexity : O(n)
# - space complexity : O(n)

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
visited = {}
for n in nums:
if n in visited:
return True
visited[n] = True
return False
21 changes: 21 additions & 0 deletions two-sum/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
https://leetcode.com/problems/two-sum/
"""
# - time complexity : O(nlogn)
# - space complexity : O(n)

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
s, e = 0, len(nums) - 1
arr = [nums[i]:=[n,i] for i, n in enumerate(nums)]

arr.sort(key=lambda x: x[0])

while s <= e:
now = arr[s][0] + arr[e][0]
if now == target:
return [arr[s][1], arr[e][1]]
if now < target:
s += 1
else:
e -= 1
11 changes: 11 additions & 0 deletions valid-anagram/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
https://leetcode.com/problems/valid-anagram/
"""
# - time complexity : O(n)
# - space complexity : O(n)

from collections import Counter

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)
10 changes: 10 additions & 0 deletions valid-palindrome/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
https://leetcode.com/problems/valid-palindrome/
"""
# - time complexity : O(n)
# - space complexity : O(n)

class Solution:
def isPalindrome(self, s: str) -> bool:
s = "".join([i for i in s if i.isalnum()]).lower()
return s == s[::-1]