Skip to content
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

[mand2, yapp] week1 답안 제출 #43

Merged
merged 4 commits into from
May 5, 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
20 changes: 20 additions & 0 deletions best-time-to-buy-and-sell-stock/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 문제: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/

def solution(prices):
profit, buy = 0, prices[0]
for price in prices:
diff = price - buy
buy = min(price, buy)
profit = max(profit, diff)
return profit


# 시간복잡도: O(n)
# 공간복잡도: O(1)


answer_1 = solution([7, 1, 5, 3, 6, 4])
answer_2 = solution([7, 6, 4, 3, 1])

print(answer_1 == 5)
print(answer_2 == 0)
16 changes: 16 additions & 0 deletions contains-duplicate/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 문제: https://leetcode.com/problems/contains-duplicate/
def containsDuplicate(nums) -> bool:
done = set()
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved
for num in nums:
if num in done:
return True
done.add(num)
return False


# 시간복잡도: O(n)
# 공간복잡도: O(n)

print((containsDuplicate([1, 2, 3, 1]) is True))
print((containsDuplicate([1, 2, 3, 4]) is False))
print((containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) is True))
24 changes: 24 additions & 0 deletions two-sum/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 문제: https://leetcode.com/problems/two-sum/description/

# targetNum - list[i] 값이 list에 있는지 확인만 하면 끝. -> 이 아니고 i. j 리턴
def solutions(nums, target_num):
table = {num: idx for idx, num in enumerate(nums)}

for i, value in enumerate(nums):
look = target_num - value
# value -> idx로 바로 치환하기가 어렵..
if look in table and i != table[look]:
look_idx = table[look]
return [i, look_idx]


# 시간복잡도: O(n)
# 공간복잡도: O(n)

answer_1 = solutions([2, 7, 11, 15], 9)
answer_2 = solutions([3, 3], 6) # 중복된수가나오면..?!?!?!?!
answer_3 = solutions([3, 2, 4], 6)

print(answer_1 == [0, 1])
print(answer_2 == [0, 1])
print(answer_3 == [1, 2])
34 changes: 34 additions & 0 deletions valid-anagram/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 문제: https://leetcode.com/problems/valid-anagram/
from collections import defaultdict


# 풀이: s 와 t 에 입력된 알파벳의 갯수를 체크한다. ...
# 애너그램이면 T
def is_anagram(s, t) -> bool:
# 글자수가 같다는 조건이 없음.
if len(s) != len(t):
return False

word_counter = defaultdict(int)
# s 문자열 분해
for alpha in s:
word_counter[alpha] += 1

for beta in t:
if beta not in word_counter or word_counter[beta] == 0:
return False
word_counter[beta] -= 1
return True

# 시간복잡도: O(n)
# 공간복잡도: O(n)

tc_1 = is_anagram("anagram", "nagaram") is True
tc_2 = is_anagram("rat", "car") is False
tc_3 = is_anagram("a", "ab") is False
tc_4 = is_anagram("aacc", "ccac") is False

print('tc_1', tc_1)
print('tc_2', tc_2)
print('tc_3', tc_3)
print('tc_4', tc_4)
26 changes: 26 additions & 0 deletions valid-palindrome/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 문제: https://leetcode.com/problems/valid-palindrome/description/

def solution(sentence):
# removing all non-alphanumeric characters (숫자는 ㅇㅋ)
selected = ''
for s in sentence:
if s.isalnum():
selected += s.lower()
return selected == selected[::-1]


# 시간복잡도: O(n)
# 공간복잡도: O(n)


answer_1 = solution("A man, a plan, a canal: Panama")
answer_2 = solution("0P")
answer_3 = solution("race a car")
answer_4 = solution(" ")
answer_5 = solution("a")

print(answer_1 is True)
print(answer_2 is False)
print(answer_3 is False)
print(answer_4 is True)
print(answer_5 is True)