-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sun]Week4 문제풀이 #426
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
[sun]Week4 문제풀이 #426
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fe64669
two-sum solution
sun912 6ef3bee
Merge remote-tracking branch 'upstream/main'
sun912 5740518
[W4]
sun912 76ce972
[W4]
sun912 405f6e6
longest-consecutive-sequence solution
sun912 e66691e
word-search solution
sun912 a3b4539
maximum-product-subarray solution
sun912 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,19 @@ | ||
""" | ||
TC: O(n) | ||
SC: O(n) | ||
""" | ||
|
||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
longest = 0 | ||
num_set = set(nums) | ||
print(num_set) | ||
|
||
for n in num_set: | ||
if (n-1) not in num_set: | ||
length = 1 | ||
while (n+length) in num_set: | ||
length += 1 | ||
longest = max(longest, length) | ||
|
||
return longest |
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,15 @@ | ||
""" | ||
TC: O(n) | ||
SC: O(1) | ||
""" | ||
class Solution: | ||
def maxProduct(self, nums: List[int]) -> int: | ||
minimum, maximum = 1, 1 | ||
result = nums[0] | ||
|
||
for num in nums: | ||
minimum, maximum = min(minimum * num, maximum * num, num), max(minimum * num, maximum * num, num) | ||
result = max(maximum, result) | ||
|
||
return result | ||
|
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,12 @@ | ||
""" | ||
TC: O(n) | ||
SC: O(n) | ||
""" | ||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nums_set = set(nums) | ||
for i in range(len(nums)): | ||
if i not in nums_set: | ||
return i | ||
return len(nums) |
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,22 @@ | ||
""" | ||
TC: O(n) | ||
SC: O(n) | ||
""" | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
str = list(s.lower()) | ||
chars = [] | ||
for c in str: | ||
if (ord(c) >= ord("a") and ord(c) <= ord("z")) or (ord(c) >= ord("0") and ord(c) <= ord("9")): | ||
chars.append(c) | ||
|
||
for i in range(len(chars)//2): | ||
|
||
if len(c)%2 == 0: | ||
if chars[i] != chars[-(i+1)]: | ||
return False | ||
else: | ||
if chars[i] != chars[-(i+1)]: | ||
return False | ||
|
||
return True |
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,41 @@ | ||
""" | ||
TC: O(ROWS*COLS*4^word length) | ||
SC: O(ROWS*COLS*word length) | ||
""" | ||
|
||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
ROWS = len(board) | ||
COLS = len(board[0]) | ||
path = set() | ||
|
||
def dfs(r,c,i): | ||
if i == len(word): | ||
return True | ||
|
||
if (r < 0 or | ||
c < 0 or | ||
r >= ROWS or | ||
c >= COLS or | ||
word[i] != board[r][c] or | ||
(r,c) in path | ||
): | ||
return False | ||
|
||
path.add((r,c)) | ||
|
||
res = (dfs(r+1, c, i+1) or | ||
dfs(r, c+1, i+1) or | ||
dfs(r-1, c, i+1) or | ||
dfs(r, c-1, i+1) | ||
) | ||
path.remove((r,c)) | ||
return res | ||
|
||
for r in range(ROWS): | ||
for c in range(COLS): | ||
if dfs(r,c, 0): return True | ||
|
||
return False | ||
|
||
|
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.
공간 복잡도를 다른 분들보다 좀 크게 보시는 것 같네요? 설명 좀 해주실 수 있으실까요?