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 2 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Byte-co
# IntelliJ
/.idea/
/.idea/.gitignore
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))
25 changes: 25 additions & 0 deletions two-sum/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 문제: 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
print('look:', look)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print('look:', look)

사소한 부분이지만 솔루션 제출하실 땐 print 는 빼도 좋지 않을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

복붙하다가 같이 딸려 들어갔나봐요. 감사합니다 :)

# 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])
33 changes: 33 additions & 0 deletions valid-anagram/mand2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 문제: https://leetcode.com/problems/valid-anagram/

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

word_counter = {}
# s 문자열 분해
for alpha in s:
# 초기화
if alpha not in word_counter:
word_counter[alpha] = 0
word_counter[alpha] += 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
word_counter = {}
# s 문자열 분해
for alpha in s:
# 초기화
if alpha not in word_counter:
word_counter[alpha] = 0
word_counter[alpha] += 1
word_counter = defaultdict(int)
# s 문자열 분해
for alpha in s:
word_counter[alpha] += 1

위처럼 수정하면 #초기화 부분을 줄일 수 있는데 이건 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 감사합니다~! 이 부분을 미처 생각 못했었네요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultdict 모듈 없이도 get 메서드로 기본값을 설정해줄 수도 있기는 합니다. ㅎㅎ
원하시는 방법을 사용하시면 될 것 같아요.

word_counter = {}

for alpha in s:
    word_counter[alpha] = word_counter.get(alpha, 0) + 1

Copy link
Contributor Author

@mand2 mand2 May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WhiteHyun 다른 기능도 알려주셔서 감사합니다😊👍


for beta in t:
if beta not in word_counter:
return False
return True
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved


# 시간복잡도: 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

print(tc_1)
print(tc_2)
print(tc_3)
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)