-
-
Notifications
You must be signed in to change notification settings - Fork 194
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2344925
[bhyun-kim] Solution for week1 assignments
bhyun-kim 23c97c0
Add line breaks
bhyun-kim 7246cf7
Add line break and remove unnecessary string conversion
bhyun-kim a6423fe
Update bhyun-kim.py
bhyun-kim 4e7da32
Fix line break
bhyun-kim 30d793f
Merge branch 'main' of https://github.com/bhyun-kim/leetcode-study
bhyun-kim 7fc8896
Shorten anagram solution
bhyun-kim 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,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() |
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,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() |
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,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() |
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,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() |
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,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() |
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.