-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Leo] 6th Week solutions + CONTRIBUTING.md update #119
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 all commits
63a7635
841f937
3c58daa
ed64619
6499125
0934286
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,19 @@ | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
left = 0 | ||
right = len(height) - 1 | ||
|
||
maxWater = 0 | ||
|
||
while left <= right: | ||
hori = right - left | ||
vert = min(height[left], height[right]) | ||
maxWater = max(maxWater, hori * vert) | ||
|
||
if height[left] < height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return maxWater | ||
# TC: O(n) SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
|
||
l = 0 | ||
r = len(nums) - 1 | ||
|
||
while l <= r: | ||
mid = (l + r) // 2 | ||
if nums[mid] < nums[r]: | ||
r = mid | ||
else: | ||
l = mid + 1 | ||
|
||
return nums[r] | ||
|
||
# TC: O(logn), SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Solution: | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
l = 0 | ||
c_frequency = {} | ||
longest_str_len = 0 | ||
|
||
for r in range(len(s)): | ||
if not s[r] in c_frequency: | ||
c_frequency[s[r]] = 0 | ||
c_frequency[s[r]] += 1 | ||
|
||
cells_count = r - l + 1 | ||
if cells_count - max(c_frequency.values()) <= k: | ||
longest_str_len = max(longest_str_len, cells_count) | ||
|
||
else: | ||
c_frequency[s[l]] -= 1 | ||
if not c_frequency[s[l]]: | ||
c_frequency.pop(s[l]) | ||
l += 1 | ||
|
||
return longest_str_len | ||
|
||
## TC: O(n), SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
left = 0 | ||
seen = {} | ||
res = 0 | ||
|
||
for right, curr in enumerate(s): | ||
if curr in seen: | ||
left = max(left, seen[curr] + 1) | ||
res = max(res, right - left + 1) | ||
seen[curr] = right | ||
|
||
return res | ||
|
||
## TC:O(n), SC:O(min(m,n)) where n is len(s) and m is size(seen) | ||
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. ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(n) ์ผ๋ก ์ ๋ฆฌํ ์ ์์ ๊ฒ ๊ฐ์๋ฐ 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. ์ ๋ ์ฒ์์ ํ๋ O(n)์ด๋ผ ์๊ฐํ๋๋ฐ์, ์ด๋ฒ์ ํ๋ฉด์ ์๊ฐํด๋ณด๋๊น ์๋ฅผ๋ค์ด ์ํ๋ฒณ์ด๋ผ๊ณ ๊ฐ์ ํ์ ๋ s์ ๊ธธ์ด๋ ์ด๋ก ์ ์ผ๋ก 10000๊ฐ๋ ๋ฃ์ ์ ์๊ฒ ์ง๋ง, seen์ ๋ค์ด๊ฐ๋๊ฑด ๊ฒฐ๊ตญ ์ํ๋ฒณ ์ต๋๊ฐฏ์๋ฅผ ๋์ด์ค ์ ์๊ณ , ๋ฐ๋๋ก s๊ฐ 26์๋ณด๋ค ์งง๋ค๋ฉด ๊ทธ ๊ธธ์ด๋งํผ๋ง ๊ณ ๋ คํ๋ฉด ๋๋ค๊ณ ์๊ฐํ์ด์. ๊ทธ๋์ ๋ ๊ฒฝ์ฐ ์ค min๊ฐ์ ๊ฐ์ ธ๊ฐ๋๊ฒ ์ข๋ ๋ํ ์ผํ ๋ถ์์ด ์๋๊น ์๊ฐํด๋ดค์ต๋๋ค ๐ 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. ์๊ฐ ๋ณต์ก๋ ๊ณ์ฐํ ๋ ์ฌ์ฉํ๋ ๋น
์ค ํ๊ธฐ๋ฒ์์๋ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
left, right = 0, len(nums) - 1 | ||
|
||
while left <= right: | ||
mid = (left + right) // 2 | ||
|
||
if nums[mid] == target: | ||
return mid | ||
|
||
if nums[left] <= nums[mid]: | ||
if nums[left] <= target < nums[mid]: | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
|
||
else: | ||
if nums[mid] < target <= nums[right]: | ||
left = mid + 1 | ||
else: | ||
right = mid - 1 | ||
|
||
return -1 | ||
|
||
## TC: O(n), SC: O(1) | ||
|
||
# if target in nums: | ||
# return nums.index(target) | ||
# else: | ||
# return -1 | ||
|
||
## TC: O(n), this may fater than bintree way if gvien nums are longer | ||
## SC: 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.
๋ชจ๋ ๋ฌธ์ ์ ๋ํ ๋ต์ ์ฝ๋๋ฅผ ํ ๋ฒ์ ์ฌ๋ฆฌ๋ ค๊ณ ํ์ง ๋ง๊ณ , Draft PR์ ํตํด์ ์กฐ๊ธ์ฉ ๋๋์ด์ ์ฌ๋ฆฌ๋ ๊ฒ์ ๊ถ์ฅํ๋ ๋ด์ฉ๋ ์ถ๊ฐ๋๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
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.
@DaleSeo ์ด๋ฏธ์ง์ถ๊ฐ ๋ฐ ํด๋น๋ด์ฉ, Iteration ์ค์ ์ ๋ํ ๋ด์ฉ๊น์ง ์ถ๊ฐํ์ต๋๋ค. ํ์ธ ๋ถํ๋๋ฆด๊ฒ์!
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.
ํด๋น ์ด๋ฏธ์ง ๋ฐ ์์ผ๋ก ํน์๋ ์ฌ์ฉ๋ ์ด๋ฏธ์ง๋ฅผ ์ํด image ํด๋๋ฅผ ์ถ๊ฐํ์ต๋๋ค. ์ง๊ธ PR์์์ ์ ๋๋ก ์๋ณด์ด๋๋ฐ ํฌํฌํ ์ ๋ฅดํฌ์์๋ ์ ์์ ์ผ๋ก ๋ณด์ด๋๊ฑฐ ๊ฐ์์. ํน์ ์ ๊ฐ ๋์น๊ฒ ์๋ค๋ฉด ์ฒดํฌ ๋ถํ๋๋ฆฌ๊ณ์ต๋๋ค.
