Skip to content

Commit 33f93b2

Browse files
authored
Merge pull request DaleStudy#40 from bhyun-kim/main
[bhyun-kim, bh_kim] Solution for week1 assignments
2 parents 68992ee + 7fc8896 commit 33f93b2

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Iterate through the list in reverse.
6+
2. Keep track of the maximum value seen so far.
7+
3. Calculate the profit by subtracting the current value from the maximum value.
8+
4. Update the profit if it is greater than the current profit.
9+
10+
Time complexity: O(n)
11+
Space complexity: O(1)
12+
"""
13+
14+
15+
from typing import List
16+
17+
18+
class Solution:
19+
def maxProfit(self, prices: List[int]) -> int:
20+
profit = 0
21+
prev_max = 0
22+
23+
for i in reversed(range(len(prices) - 1)):
24+
prev_max = max(prev_max, prices[i + 1])
25+
profit = max(profit, prev_max - prices[i])
26+
27+
return profit
28+
29+
30+
def main():
31+
test_cases = [[[7, 1, 5, 3, 6, 4], 5], [[7, 6, 4, 3, 1], 0]]
32+
s = Solution()
33+
34+
for test_case in test_cases:
35+
prices_input, expected = test_case
36+
assert s.maxProfit(prices_input) == expected
37+
38+
39+
if __name__ == "__main__":
40+
main()

contains-duplicate/bhyun-kim.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Create a set from the list.
6+
2. If the length of the set is not equal to the length of the list, return True.
7+
3. Otherwise, return False.
8+
9+
Time complexity: O(n)
10+
Space complexity: O(n)
11+
"""
12+
13+
14+
from typing import List
15+
16+
17+
class Solution:
18+
def containsDuplicate(self, nums: List[int]) -> bool:
19+
return len(set(nums)) != len(nums)
20+
21+
22+
def main():
23+
test_cases = [
24+
[
25+
[1, 2, 3, 1],
26+
True,
27+
][[1, 2, 3, 4], False],
28+
[[1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True],
29+
]
30+
s = Solution()
31+
32+
for test_case in test_cases:
33+
nums_input, expected = test_case
34+
assert s.containsDuplicate(nums_input) == expected
35+
36+
37+
if __name__ == "__main__":
38+
main()

two-sum/bhyun-kim.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Create a hashmap to store the index of each element.
6+
2. Iterate through the list.
7+
3. Check if the remaining value is in the hashmap.
8+
4. If it is, return the current index and the index of the remaining value in the hashmap.
9+
10+
Time complexity: O(n)
11+
Space complexity: O(n)
12+
"""
13+
14+
from typing import List
15+
16+
17+
class Solution:
18+
def twoSum(self, nums: List[int], target: int) -> List[int]:
19+
hashmap = {}
20+
21+
for i in range(len(nums)):
22+
remaining = target - nums[i]
23+
if remaining in hashmap:
24+
return [i, hashmap[remaining]]
25+
26+
hashmap[nums[i]] = i
27+
28+
29+
def main():
30+
test_cases = [[2, 7, 11, 15], 9, [0, 1], [3, 2, 4], 6, [1, 2], [3, 3], 6, [0, 1]]
31+
s = Solution()
32+
33+
for test_case in test_cases:
34+
nums_input, target_input, expected = test_case
35+
assert sorted(s.twoSum(nums_input, target_input)) == expected
36+
37+
38+
if __name__ == "__main__":
39+
main()

valid-anagram/bhyun-kim.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Sort the strings and compare them.
6+
2. If they are equal, return True. Otherwise, return False.
7+
8+
Time complexity: O(nlogn)
9+
Space complexity: O(1)
10+
"""
11+
12+
13+
class Solution:
14+
def isAnagram(self, s: str, t: str) -> bool:
15+
return sorted(s) == sorted(t)
16+
17+
18+
def main():
19+
test_cases = [["anagram", "nagaram", True], ["rat", "car", False]]
20+
s = Solution()
21+
22+
for test_case in test_cases:
23+
s_input, t_input, expected = test_case
24+
assert s.isAnagram(s_input, t_input) == expected
25+
26+
27+
if __name__ == "__main__":
28+
main()

valid-palindrome/bhyun-kim.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Solution:
3+
4+
Algorithm:
5+
1. Convert the string to lowercase.
6+
2. Remove all non-alphanumeric characters.
7+
3. Check if the string is equal to its reverse.
8+
9+
Time complexity: O(n)
10+
Space complexity: O(n)
11+
"""
12+
13+
14+
class Solution:
15+
def isPalindrome(self, s: str) -> bool:
16+
s = s.lower()
17+
s = [c for c in s if c.isalpha() or c.isnumeric()]
18+
return s == s[::-1]
19+
20+
21+
def main():
22+
test_cases = [
23+
["A man, a plan, a canal: Panama", True],
24+
["race a car", False],
25+
[" ", True],
26+
]
27+
s = Solution()
28+
29+
for test_case in test_cases:
30+
s_input, expected = test_case
31+
assert s.isPalindrome(s_input) == expected
32+
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)