-
-
Notifications
You must be signed in to change notification settings - Fork 304
[devyulbae] WEEK 03 solutions #2114
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
2f31db7
1 solved
devyulbae fa1a752
[week3]
devyulbae 25f5020
feature-week3-solved_1
devyulbae 7cbe849
feature-solved 2
devyulbae 43213a1
debug: add indent
devyulbae d459373
feat: solve 3
devyulbae 72b9c90
feat: solve 4
devyulbae 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,3 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
|
|
25 changes: 25 additions & 0 deletions
25
longest-substring-without-repeating-characters/devyulbae.py
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,25 @@ | ||
| """ | ||
| Blind75 - length of longest substring without repeating characters | ||
| https://leetcode.com/problems/longest-substring-without-repeating-characters/ | ||
| 시간복잡도 : O(n) | ||
| 공간복잡도 : O(min(m, n)) (문자 집합 char_index_map의 크기, 최대는 n = len(s)) | ||
| 풀이 : 슬라이딩 윈도우 기법을 사용한 문자열 순회 | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| max_count = 0 | ||
| start = 0 | ||
| char_index_map = {} | ||
|
|
||
| for i, char in enumerate(s): | ||
| if char in char_index_map and char_index_map[char] >= start: | ||
| start = char_index_map[char] + 1 | ||
| char_index_map[char] = i | ||
| else: | ||
| char_index_map[char] = i | ||
| max_count = max(max_count, i - start + 1) | ||
|
|
||
| return max_count | ||
|
|
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,20 @@ | ||
| """ | ||
| Blind75 - 5. Maximum Subarray | ||
| LeetCode Problem Link: https://leetcode.com/problems/maximum-subarray/ | ||
|
|
||
| 통과는 했는데 시간복잡도 O(n^2)라서 좀 아쉽다...투포인터로 풀 수도 있을 거 같은데... | ||
| """ | ||
| from typing import List | ||
|
|
||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_total = nums[0] | ||
|
|
||
| for s in range(len(nums)): | ||
| total = 0 | ||
| for e in range(s, len(nums)): | ||
| total += nums[e] | ||
| if total > max_total: | ||
| max_total = total | ||
| return max_total | ||
|
|
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,17 @@ | ||
| """ | ||
| Blind75 - Number of 1 Bits | ||
| https://leetcode.com/problems/number-of-1-bits/ | ||
| 시간복잡도 : O(log n) | ||
| 공간복잡도 : O(1) | ||
|
|
||
| 풀이 : | ||
| 파이썬의 내장함수 bin() -> O(log n) | ||
| 문자열 메서드 count() -> O(log n) | ||
|
|
||
| """ | ||
|
|
||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| return str(bin(n)).count('1') | ||
|
|
||
|
|
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,23 @@ | ||
| """ | ||
| Blind75 - Valid Palindrome | ||
| https://leetcode.com/problems/valid-palindrome/ | ||
|
|
||
| 시간복잡도 O(n), 공간복잡도 O(n) | ||
|
|
||
| isalnum() 함수는 시간복잡도 O(1)이므로 필터링하는 과정도 O(n)이다. | ||
| [::-1] 슬라이싱도 O(n)이다. | ||
| 따라서 전체 시간복잡도는 O(n)이다. | ||
|
|
||
|
|
||
| Runtime Beats | ||
| 7ms 82.67% | ||
|
|
||
| Memory Beats | ||
| 23.14 MB 17.23% | ||
| """ | ||
|
|
||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| filtered_s = ''.join(char.lower() for char in s if char.isalnum()) | ||
| return filtered_s == filtered_s[::-1] | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 구현은 문자열을 여러 차례 순회하며 중간 결과를 생성하기 때문에 전체 처리 비용이 커집니다.
투포인터 방식을 적용하면 문자 검사, 정규화, 앞뒤 비교를 하나의 루프에서 처리할 수 있어 순회 비용을 단일 O(N)으로 줄일 수 있습니다. 별도 메모리를 사용하지 않으며, 불일치 시 즉시 종료할 수 있어 전체 런타임 측면에서도 더 효율적입니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 감사합니다! 한번 최적화해볼게요ㅎㅎ