Skip to content

Commit c17a4b3

Browse files
committed
[mand2] week1 답안 제출
- [x] Contains Duplicate - [x] Valid Anagram - [x] Two Sum - [x] Valid Palindrome - [x] Best Time to Buy and Sell Stock
1 parent df37585 commit c17a4b3

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Byte-co
2+
# IntelliJ
3+
/.idea/
4+
/.idea/.gitignore
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 문제: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
2+
3+
def solution(prices):
4+
profit, buy = 0, prices[0]
5+
for price in prices:
6+
diff = price - buy
7+
buy = min(price, buy)
8+
profit = max(profit, diff)
9+
return profit
10+
11+
12+
# 시간복잡도: O(n)
13+
# 공간복잡도: O(1)
14+
15+
16+
answer_1 = solution([7, 1, 5, 3, 6, 4])
17+
answer_2 = solution([7, 6, 4, 3, 1])
18+
19+
print(answer_1 == 5)
20+
print(answer_2 == 0)

contains-duplicate/mand2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 문제: https://leetcode.com/problems/contains-duplicate/
2+
def containsDuplicate(nums) -> bool:
3+
done = set()
4+
for num in nums:
5+
if num in done:
6+
return True
7+
done.add(num)
8+
return False
9+
10+
11+
# 시간복잡도: O(n)
12+
# 공간복잡도: O(n)
13+
14+
print((containsDuplicate([1, 2, 3, 1]) is True))
15+
print((containsDuplicate([1, 2, 3, 4]) is False))
16+
print((containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) is True))

two-sum/mand2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 문제: https://leetcode.com/problems/two-sum/description/
2+
3+
# targetNum - list[i] 값이 list에 있는지 확인만 하면 끝. -> 이 아니고 i. j 리턴
4+
def solutions(nums, target_num):
5+
table = {num: idx for idx, num in enumerate(nums)}
6+
7+
for i, value in enumerate(nums):
8+
look = target_num - value
9+
print('look:', look)
10+
# value -> idx로 바로 치환하기가 어렵..
11+
if look in table and i != table[look]:
12+
look_idx = table[look]
13+
return [i, look_idx]
14+
15+
16+
# 시간복잡도: O(n)
17+
# 공간복잡도: O(n)
18+
19+
answer_1 = solutions([2, 7, 11, 15], 9)
20+
answer_2 = solutions([3, 3], 6) # 중복된수가나오면..?!?!?!?!
21+
answer_3 = solutions([3, 2, 4], 6)
22+
23+
print(answer_1 == [0, 1])
24+
print(answer_2 == [0, 1])
25+
print(answer_3 == [1, 2])

valid-anagram/mand2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 문제: https://leetcode.com/problems/valid-anagram/
2+
3+
# 풀이: s 와 t 에 입력된 알파벳의 갯수를 체크한다. ...
4+
# 애너그램이면 T
5+
def is_anagram(s, t) -> bool:
6+
# 글자수가 같다는 조건이 없음.
7+
if len(s) != len(t):
8+
return False
9+
10+
word_counter = {}
11+
# s 문자열 분해
12+
for alpha in s:
13+
# 초기화
14+
if alpha not in word_counter:
15+
word_counter[alpha] = 0
16+
word_counter[alpha] += 1
17+
18+
for beta in t:
19+
if beta not in word_counter:
20+
return False
21+
return True
22+
23+
24+
# 시간복잡도: O(n)
25+
# 공간복잡도: O(n)
26+
27+
tc_1 = is_anagram("anagram", "nagaram") is True
28+
tc_2 = is_anagram("rat", "car") is False
29+
tc_3 = is_anagram("a", "ab") is False
30+
31+
print(tc_1)
32+
print(tc_2)
33+
print(tc_3)

valid-palindrome/mand2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 문제: https://leetcode.com/problems/valid-palindrome/description/
2+
3+
def solution(sentence):
4+
# removing all non-alphanumeric characters (숫자는 ㅇㅋ)
5+
selected = ''
6+
for s in sentence:
7+
if s.isalnum():
8+
selected += s.lower()
9+
return selected == selected[::-1]
10+
11+
12+
# 시간복잡도: O(n)
13+
# 공간복잡도: O(n)
14+
15+
16+
answer_1 = solution("A man, a plan, a canal: Panama")
17+
answer_2 = solution("0P")
18+
answer_3 = solution("race a car")
19+
answer_4 = solution(" ")
20+
answer_5 = solution("a")
21+
22+
print(answer_1 is True)
23+
print(answer_2 is False)
24+
print(answer_3 is False)
25+
print(answer_4 is True)
26+
print(answer_5 is True)

0 commit comments

Comments
 (0)