Skip to content

Commit

Permalink
revert several python solution changes
Browse files Browse the repository at this point in the history
  • Loading branch information
neetcode-gh committed Sep 20, 2023
1 parent 69b5fb7 commit 3fb65a3
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 163 deletions.
23 changes: 10 additions & 13 deletions python/0003-longest-substring-without-repeating-characters.py
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
15 changes: 9 additions & 6 deletions python/0023-merge-k-sorted-lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ def mergeKLists(self, lists: List[ListNode]) -> ListNode:
return lists[0]

def mergeList(self, l1, l2):
dummy = node = ListNode()
dummy = ListNode()
tail = dummy

while l1 and l2:
if l1.val < l2.val:
node.next = l1
tail.next = l1
l1 = l1.next
else:
node.next = l2
tail.next = l2
l2 = l2.next
node = node.next

node.next = l1 or l2
tail = tail.next
if l1:
tail.next = l1
if l2:
tail.next = l2
return dummy.next
17 changes: 9 additions & 8 deletions python/0025-reverse-nodes-in-k-group.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
dummy = groupPrev = ListNode(0, head)

dummy = ListNode(0, head)
groupPrev = dummy

while True:
kth = self.getKth(groupPrev, k)
if not kth:
break
groupPrev.next = kth
groupNext = kth.next

# reverse group
prev, curr = groupNext, head
prev, curr = kth.next, groupPrev.next
while curr != groupNext:
nxt = curr.next
tmp = curr.next
curr.next = prev
prev = curr
curr = nxt
curr = tmp

groupPrev = head
head = groupNext
tmp = groupPrev.next
groupPrev.next = kth
groupPrev = tmp
return dummy.next

def getKth(self, curr, k):
Expand Down
28 changes: 15 additions & 13 deletions python/0042-trapping-rain-water.py
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
14 changes: 7 additions & 7 deletions python/0084-largest-rectangle-in-histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ def largestRectangleArea(self, heights: List[int]) -> int:
maxArea = 0
stack = [] # pair: (index, height)

for right, short in enumerate(heights):
start = right
while stack and short < stack[-1][1]:
left, tall = stack.pop()
maxArea = max(maxArea, tall * (right - left))
start = left
stack.append((start, short))
for i, h in enumerate(heights):
start = i
while stack and stack[-1][1] > h:
index, height = stack.pop()
maxArea = max(maxArea, height * (i - index))
start = index
stack.append((start, h))

for i, h in stack:
maxArea = max(maxArea, h * (len(heights) - i))
Expand Down
11 changes: 4 additions & 7 deletions python/0121-best-time-to-buy-and-sell-stock.py
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
26 changes: 11 additions & 15 deletions python/0134-gas-station.py
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
38 changes: 13 additions & 25 deletions python/0150-evaluate-reverse-polish-notation.py
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
22 changes: 15 additions & 7 deletions python/0153-find-minimum-in-rotated-sorted-array.py
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])
20 changes: 11 additions & 9 deletions python/0424-longest-repeating-character-replacement.py
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)
29 changes: 16 additions & 13 deletions python/0567-permutation-in-string.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Solution:
def checkInclusion(self, s1, s2):
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False

Expand All @@ -8,24 +8,27 @@ def checkInclusion(self, s1, s2):
s1Count[ord(s1[i]) - ord("a")] += 1
s2Count[ord(s2[i]) - ord("a")] += 1

matches = sum(map(lambda i: s1Count[i] == s2Count[i], range(26)))
matches = 0
for i in range(26):
matches += 1 if s1Count[i] == s2Count[i] else 0

for l in range(len(s2) - len(s1)):
l = 0
for r in range(len(s1), len(s2)):
if matches == 26:
return True

i = ord(s2[l]) - ord("a")
s2Count[i] -= 1
if s2Count[i] == s1Count[i]:
index = ord(s2[r]) - ord("a")
s2Count[index] += 1
if s1Count[index] == s2Count[index]:
matches += 1
elif s2Count[i] == s1Count[i] - 1:
elif s1Count[index] + 1 == s2Count[index]:
matches -= 1

i = ord(s2[l + len(s1)]) - ord("a")
s2Count[i] += 1
if s2Count[i] == s1Count[i]:
index = ord(s2[l]) - ord("a")
s2Count[index] -= 1
if s1Count[index] == s2Count[index]:
matches += 1
elif s2Count[i] == s1Count[i] + 1:
elif s1Count[index] - 1 == s2Count[index]:
matches -= 1

return matches == 26
l += 1
return matches == 26
40 changes: 0 additions & 40 deletions python/0705-design-hashset

This file was deleted.

0 comments on commit 3fb65a3

Please sign in to comment.