forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert several python solution changes
- Loading branch information
1 parent
69b5fb7
commit 3fb65a3
Showing
12 changed files
with
120 additions
and
163 deletions.
There are no files selected for viewing
23 changes: 10 additions & 13 deletions
23
python/0003-longest-substring-without-repeating-characters.py
This file contains 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 |
---|---|---|
@@ -1,16 +1,13 @@ | ||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
|
||
repeats = dict() | ||
start = longest = 0 | ||
charSet = set() | ||
l = 0 | ||
res = 0 | ||
|
||
for i, char in enumerate(s): | ||
if char in repeats and repeats[char] >= start: | ||
start = repeats[char] + 1 | ||
|
||
repeats[char] = i | ||
|
||
if longest < i - start + 1: | ||
longest = i - start + 1 | ||
|
||
return longest | ||
for r in range(len(s)): | ||
while s[r] in charSet: | ||
charSet.remove(s[l]) | ||
l += 1 | ||
charSet.add(s[r]) | ||
res = max(res, r - l + 1) | ||
return res |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
class Solution: | ||
def trap(self, height: List[int]) -> int: | ||
if not height: | ||
return 0 | ||
|
||
c = height.index(max(height)) | ||
|
||
vol = 0 | ||
for arr in [height[:c], height[:c:-1]]: | ||
first = 0 | ||
for i in arr: | ||
if i < first: | ||
vol += first - i | ||
else: | ||
first = i | ||
|
||
return vol | ||
|
||
l, r = 0, len(height) - 1 | ||
leftMax, rightMax = height[l], height[r] | ||
res = 0 | ||
while l < r: | ||
if leftMax < rightMax: | ||
l += 1 | ||
leftMax = max(leftMax, height[l]) | ||
res += leftMax - height[l] | ||
else: | ||
r -= 1 | ||
rightMax = max(rightMax, height[r]) | ||
res += rightMax - height[r] | ||
return res |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
res = 0 | ||
|
||
profit = 0 | ||
lowest = prices[0] | ||
|
||
for price in prices[1:]: | ||
for price in prices: | ||
if price < lowest: | ||
lowest = price | ||
elif price - lowest > profit: | ||
profit = price - lowest | ||
|
||
return profit | ||
res = max(res, price - lowest) | ||
return res |
This file contains 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 |
---|---|---|
@@ -1,18 +1,14 @@ | ||
class Solution: | ||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: | ||
total_gas = 0 | ||
remaining_gas = 0 | ||
start_index = 0 | ||
start, end = len(gas) - 1, 0 | ||
total = gas[start] - cost[start] | ||
|
||
for i in range(len(gas)): | ||
total_gas += gas[i] - cost[i] | ||
remaining_gas += gas[i] - cost[i] | ||
|
||
if remaining_gas < 0: | ||
remaining_gas = 0 | ||
start_index = i + 1 | ||
|
||
if total_gas >= 0: | ||
return start_index | ||
else: | ||
return -1 | ||
while start >= end: | ||
while total < 0 and start >= end: | ||
start -= 1 | ||
total += gas[start] - cost[start] | ||
if start == end: | ||
return start | ||
total += gas[end] - cost[end] | ||
end += 1 | ||
return -1 |
This file contains 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 |
---|---|---|
@@ -1,29 +1,17 @@ | ||
class Solution: | ||
def evalRPN(self, tokens: List[str]) -> int: | ||
h = { | ||
'+': self.add, | ||
'-': self.subtract, | ||
'*': self.multiply, | ||
'/': self.divide | ||
} | ||
|
||
stack = [] | ||
for t in tokens: | ||
if t in h: | ||
b, a = stack.pop(), stack.pop() | ||
t = h[t](a, b) | ||
stack.append(int(t)) | ||
|
||
for c in tokens: | ||
if c == "+": | ||
stack.append(stack.pop() + stack.pop()) | ||
elif c == "-": | ||
a, b = stack.pop(), stack.pop() | ||
stack.append(b - a) | ||
elif c == "*": | ||
stack.append(stack.pop() * stack.pop()) | ||
elif c == "/": | ||
a, b = stack.pop(), stack.pop() | ||
stack.append(int(float(b) / a)) | ||
else: | ||
stack.append(int(c)) | ||
return stack[0] | ||
|
||
def add(self, a, b): | ||
return a + b | ||
|
||
def subtract(self, a, b): | ||
return a - b | ||
|
||
def multiply(self, a, b): | ||
return a * b | ||
|
||
def divide(self, a, b): | ||
return a / b |
This file contains 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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
l, r = 0, len(nums) - 1 | ||
while l < r: | ||
m = l + (r - l) // 2 | ||
if nums[m] > nums[r]: | ||
l = m + 1 | ||
start , end = 0, len(nums) - 1 | ||
curr_min = float("inf") | ||
|
||
while start < end : | ||
mid = (start + end ) // 2 | ||
curr_min = min(curr_min,nums[mid]) | ||
|
||
# right has the min | ||
if nums[mid] > nums[end]: | ||
start = mid + 1 | ||
|
||
# left has the min | ||
else: | ||
r = m | ||
return nums[l] | ||
end = mid - 1 | ||
|
||
return min(curr_min,nums[start]) |
This file contains 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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
class Solution: | ||
def characterReplacement(self, s, k): | ||
counts = {} | ||
maxf = 0 | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
count = {} | ||
|
||
l = 0 | ||
for r, ch in enumerate(s): | ||
counts[ch] = 1 + counts.get(ch, 0) | ||
maxf = max(maxf, counts[ch]) | ||
if maxf + k < r - l + 1: | ||
counts[s[l]] -= 1 | ||
maxf = 0 | ||
for r in range(len(s)): | ||
count[s[r]] = 1 + count.get(s[r], 0) | ||
maxf = max(maxf, count[s[r]]) | ||
|
||
if (r - l + 1) - maxf > k: | ||
count[s[l]] -= 1 | ||
l += 1 | ||
|
||
return len(s) - l | ||
return (r - l + 1) |
This file contains 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
This file was deleted.
Oops, something went wrong.