-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from 2 commits
2344925
23c97c0
7246cf7
a6423fe
4e7da32
30d793f
7fc8896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
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: | ||
|
||
if sorted(s) == sorted(t): | ||
return True | ||
else: | ||
return False | ||
bhyun-kim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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() | ||
|
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 = list([c for c in s | ||||||||||
if c.isalpha() or c.isnumeric()]) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미 리스트인데,
Suggested change
|
||||||||||
s = "".join(s) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굳이 문자열로 다시 재변환 할 필요가 있을까요? 이 줄을 지우면 그냥 리스트 간 비교가 될 것 같네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문자열 변환과 리스트 변환 중복을 제거했습니다. 리트코드 홈페이지에서 실행하니 평균적으로 10% 정도 속도가 줄어들었네요! |
||||||||||
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() |
Uh oh!
There was an error while loading. Please reload this page.