-
-
Notifications
You must be signed in to change notification settings - Fork 195
[mand2, yapp] week1 답안 제출 #43
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.