Skip to content

Commit bfd7843

Browse files
authored
Merge pull request DaleStudy#43 from mand2/main
[mand2, yapp] week1 ๋‹ต์•ˆ ์ œ์ถœ
2 parents 83ad247 + c63d0ca commit bfd7843

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
# value -> idx๋กœ ๋ฐ”๋กœ ์น˜ํ™˜ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต..
10+
if look in table and i != table[look]:
11+
look_idx = table[look]
12+
return [i, look_idx]
13+
14+
15+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
16+
# ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
17+
18+
answer_1 = solutions([2, 7, 11, 15], 9)
19+
answer_2 = solutions([3, 3], 6) # ์ค‘๋ณต๋œ์ˆ˜๊ฐ€๋‚˜์˜ค๋ฉด..?!?!?!?!
20+
answer_3 = solutions([3, 2, 4], 6)
21+
22+
print(answer_1 == [0, 1])
23+
print(answer_2 == [0, 1])
24+
print(answer_3 == [1, 2])

โ€Žvalid-anagram/mand2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ๋ฌธ์ œ: https://leetcode.com/problems/valid-anagram/
2+
from collections import defaultdict
3+
4+
5+
# ํ’€์ด: s ์™€ t ์— ์ž…๋ ฅ๋œ ์•ŒํŒŒ๋ฒณ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ฒดํฌํ•œ๋‹ค. ...
6+
# ์• ๋„ˆ๊ทธ๋žจ์ด๋ฉด T
7+
def is_anagram(s, t) -> bool:
8+
# ๊ธ€์ž์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋Š” ์กฐ๊ฑด์ด ์—†์Œ.
9+
if len(s) != len(t):
10+
return False
11+
12+
word_counter = defaultdict(int)
13+
# s ๋ฌธ์ž์—ด ๋ถ„ํ•ด
14+
for alpha in s:
15+
word_counter[alpha] += 1
16+
17+
for beta in t:
18+
if beta not in word_counter or word_counter[beta] == 0:
19+
return False
20+
word_counter[beta] -= 1
21+
return True
22+
23+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
24+
# ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
25+
26+
tc_1 = is_anagram("anagram", "nagaram") is True
27+
tc_2 = is_anagram("rat", "car") is False
28+
tc_3 = is_anagram("a", "ab") is False
29+
tc_4 = is_anagram("aacc", "ccac") is False
30+
31+
print('tc_1', tc_1)
32+
print('tc_2', tc_2)
33+
print('tc_3', tc_3)
34+
print('tc_4', tc_4)

โ€Ž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)