Skip to content

[bhyun-kim, bh_kim] Solution for week1 assignments #40

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 7 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions best-time-to-buy-and-sell-stock/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Solution

Algorithm:
1. Iterate through the list in reverse.
2. Keep track of the maximum value seen so far.
3. Calculate the profit by subtracting the current value from the maximum value.
4. Update the profit if it is greater than the current profit.

Time complexity: O(n)
Space complexity: O(1)
"""


from typing import List


class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
prev_max = 0

for i in reversed(range(len(prices) - 1)):
prev_max = max(prev_max, prices[i + 1])
profit = max(profit, prev_max - prices[i])

return profit


def main():
test_cases = [[[7, 1, 5, 3, 6, 4], 5], [[7, 6, 4, 3, 1], 0]]
s = Solution()

for test_case in test_cases:
prices_input, expected = test_case
assert s.maxProfit(prices_input) == expected


if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions contains-duplicate/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Solution

Algorithm:
1. Create a set from the list.
2. If the length of the set is not equal to the length of the list, return True.
3. Otherwise, return False.

Time complexity: O(n)
Space complexity: O(n)
"""


from typing import List


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) != len(nums)


def main():
test_cases = [
[
[1, 2, 3, 1],
True,
][[1, 2, 3, 4], False],
[[1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True],
]
s = Solution()

for test_case in test_cases:
nums_input, expected = test_case
assert s.containsDuplicate(nums_input) == expected


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions two-sum/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Solution

Algorithm:
1. Create a hashmap to store the index of each element.
2. Iterate through the list.
3. Check if the remaining value is in the hashmap.
4. If it is, return the current index and the index of the remaining value in the hashmap.

Time complexity: O(n)
Space complexity: O(n)
"""

from typing import List


class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap = {}

for i in range(len(nums)):
remaining = target - nums[i]
if remaining in hashmap:
return [i, hashmap[remaining]]

hashmap[nums[i]] = i


def main():
test_cases = [[2, 7, 11, 15], 9, [0, 1], [3, 2, 4], 6, [1, 2], [3, 3], 6, [0, 1]]
s = Solution()

for test_case in test_cases:
nums_input, target_input, expected = test_case
assert sorted(s.twoSum(nums_input, target_input)) == expected


if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions valid-anagram/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Solution

Algorithm:
1. Sort the strings and compare them.
2. If they are equal, return True. Otherwise, return False.

Time complexity: O(nlogn)
Space complexity: O(1)
"""


class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return sorted(s) == sorted(t)


def main():
test_cases = [["anagram", "nagaram", True], ["rat", "car", False]]
s = Solution()

for test_case in test_cases:
s_input, t_input, expected = test_case
assert s.isAnagram(s_input, t_input) == expected


if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions valid-palindrome/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Solution:

Algorithm:
1. Convert the string to lowercase.
2. Remove all non-alphanumeric characters.
3. Check if the string is equal to its reverse.

Time complexity: O(n)
Space complexity: O(n)
"""


class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower()
s = [c for c in s if c.isalpha() or c.isnumeric()]
return s == s[::-1]


def main():
test_cases = [
["A man, a plan, a canal: Panama", True],
["race a car", False],
[" ", True],
]
s = Solution()

for test_case in test_cases:
s_input, expected = test_case
assert s.isPalindrome(s_input) == expected


if __name__ == "__main__":
main()