Skip to content

Commit 3fb65a3

Browse files
committed
revert several python solution changes
1 parent 69b5fb7 commit 3fb65a3

12 files changed

+120
-163
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
class Solution:
22
def lengthOfLongestSubstring(self, s: str) -> int:
3-
4-
repeats = dict()
5-
start = longest = 0
3+
charSet = set()
4+
l = 0
5+
res = 0
66

7-
for i, char in enumerate(s):
8-
if char in repeats and repeats[char] >= start:
9-
start = repeats[char] + 1
10-
11-
repeats[char] = i
12-
13-
if longest < i - start + 1:
14-
longest = i - start + 1
15-
16-
return longest
7+
for r in range(len(s)):
8+
while s[r] in charSet:
9+
charSet.remove(s[l])
10+
l += 1
11+
charSet.add(s[r])
12+
res = max(res, r - l + 1)
13+
return res

python/0023-merge-k-sorted-lists.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ def mergeKLists(self, lists: List[ListNode]) -> ListNode:
1818
return lists[0]
1919

2020
def mergeList(self, l1, l2):
21-
dummy = node = ListNode()
21+
dummy = ListNode()
22+
tail = dummy
2223

2324
while l1 and l2:
2425
if l1.val < l2.val:
25-
node.next = l1
26+
tail.next = l1
2627
l1 = l1.next
2728
else:
28-
node.next = l2
29+
tail.next = l2
2930
l2 = l2.next
30-
node = node.next
31-
32-
node.next = l1 or l2
31+
tail = tail.next
32+
if l1:
33+
tail.next = l1
34+
if l2:
35+
tail.next = l2
3336
return dummy.next

python/0025-reverse-nodes-in-k-group.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
class Solution:
22
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
3-
dummy = groupPrev = ListNode(0, head)
4-
3+
dummy = ListNode(0, head)
4+
groupPrev = dummy
5+
56
while True:
67
kth = self.getKth(groupPrev, k)
78
if not kth:
89
break
9-
groupPrev.next = kth
1010
groupNext = kth.next
1111

1212
# reverse group
13-
prev, curr = groupNext, head
13+
prev, curr = kth.next, groupPrev.next
1414
while curr != groupNext:
15-
nxt = curr.next
15+
tmp = curr.next
1616
curr.next = prev
1717
prev = curr
18-
curr = nxt
18+
curr = tmp
1919

20-
groupPrev = head
21-
head = groupNext
20+
tmp = groupPrev.next
21+
groupPrev.next = kth
22+
groupPrev = tmp
2223
return dummy.next
2324

2425
def getKth(self, curr, k):

python/0042-trapping-rain-water.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution:
22
def trap(self, height: List[int]) -> int:
3+
if not height:
4+
return 0
35

4-
c = height.index(max(height))
5-
6-
vol = 0
7-
for arr in [height[:c], height[:c:-1]]:
8-
first = 0
9-
for i in arr:
10-
if i < first:
11-
vol += first - i
12-
else:
13-
first = i
14-
15-
return vol
16-
6+
l, r = 0, len(height) - 1
7+
leftMax, rightMax = height[l], height[r]
8+
res = 0
9+
while l < r:
10+
if leftMax < rightMax:
11+
l += 1
12+
leftMax = max(leftMax, height[l])
13+
res += leftMax - height[l]
14+
else:
15+
r -= 1
16+
rightMax = max(rightMax, height[r])
17+
res += rightMax - height[r]
18+
return res

python/0084-largest-rectangle-in-histogram.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ def largestRectangleArea(self, heights: List[int]) -> int:
33
maxArea = 0
44
stack = [] # pair: (index, height)
55

6-
for right, short in enumerate(heights):
7-
start = right
8-
while stack and short < stack[-1][1]:
9-
left, tall = stack.pop()
10-
maxArea = max(maxArea, tall * (right - left))
11-
start = left
12-
stack.append((start, short))
6+
for i, h in enumerate(heights):
7+
start = i
8+
while stack and stack[-1][1] > h:
9+
index, height = stack.pop()
10+
maxArea = max(maxArea, height * (i - index))
11+
start = index
12+
stack.append((start, h))
1313

1414
for i, h in stack:
1515
maxArea = max(maxArea, h * (len(heights) - i))
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution:
22
def maxProfit(self, prices: List[int]) -> int:
3+
res = 0
34

4-
profit = 0
55
lowest = prices[0]
6-
7-
for price in prices[1:]:
6+
for price in prices:
87
if price < lowest:
98
lowest = price
10-
elif price - lowest > profit:
11-
profit = price - lowest
12-
13-
return profit
9+
res = max(res, price - lowest)
10+
return res

python/0134-gas-station.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
class Solution:
22
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
3-
total_gas = 0
4-
remaining_gas = 0
5-
start_index = 0
3+
start, end = len(gas) - 1, 0
4+
total = gas[start] - cost[start]
65

7-
for i in range(len(gas)):
8-
total_gas += gas[i] - cost[i]
9-
remaining_gas += gas[i] - cost[i]
10-
11-
if remaining_gas < 0:
12-
remaining_gas = 0
13-
start_index = i + 1
14-
15-
if total_gas >= 0:
16-
return start_index
17-
else:
18-
return -1
6+
while start >= end:
7+
while total < 0 and start >= end:
8+
start -= 1
9+
total += gas[start] - cost[start]
10+
if start == end:
11+
return start
12+
total += gas[end] - cost[end]
13+
end += 1
14+
return -1
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
class Solution:
22
def evalRPN(self, tokens: List[str]) -> int:
3-
h = {
4-
'+': self.add,
5-
'-': self.subtract,
6-
'*': self.multiply,
7-
'/': self.divide
8-
}
9-
103
stack = []
11-
for t in tokens:
12-
if t in h:
13-
b, a = stack.pop(), stack.pop()
14-
t = h[t](a, b)
15-
stack.append(int(t))
16-
4+
for c in tokens:
5+
if c == "+":
6+
stack.append(stack.pop() + stack.pop())
7+
elif c == "-":
8+
a, b = stack.pop(), stack.pop()
9+
stack.append(b - a)
10+
elif c == "*":
11+
stack.append(stack.pop() * stack.pop())
12+
elif c == "/":
13+
a, b = stack.pop(), stack.pop()
14+
stack.append(int(float(b) / a))
15+
else:
16+
stack.append(int(c))
1717
return stack[0]
18-
19-
def add(self, a, b):
20-
return a + b
21-
22-
def subtract(self, a, b):
23-
return a - b
24-
25-
def multiply(self, a, b):
26-
return a * b
27-
28-
def divide(self, a, b):
29-
return a / b
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
class Solution:
22
def findMin(self, nums: List[int]) -> int:
3-
l, r = 0, len(nums) - 1
4-
while l < r:
5-
m = l + (r - l) // 2
6-
if nums[m] > nums[r]:
7-
l = m + 1
3+
start , end = 0, len(nums) - 1
4+
curr_min = float("inf")
5+
6+
while start < end :
7+
mid = (start + end ) // 2
8+
curr_min = min(curr_min,nums[mid])
9+
10+
# right has the min
11+
if nums[mid] > nums[end]:
12+
start = mid + 1
13+
14+
# left has the min
815
else:
9-
r = m
10-
return nums[l]
16+
end = mid - 1
17+
18+
return min(curr_min,nums[start])
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution:
2-
def characterReplacement(self, s, k):
3-
counts = {}
4-
maxf = 0
2+
def characterReplacement(self, s: str, k: int) -> int:
3+
count = {}
4+
55
l = 0
6-
for r, ch in enumerate(s):
7-
counts[ch] = 1 + counts.get(ch, 0)
8-
maxf = max(maxf, counts[ch])
9-
if maxf + k < r - l + 1:
10-
counts[s[l]] -= 1
6+
maxf = 0
7+
for r in range(len(s)):
8+
count[s[r]] = 1 + count.get(s[r], 0)
9+
maxf = max(maxf, count[s[r]])
10+
11+
if (r - l + 1) - maxf > k:
12+
count[s[l]] -= 1
1113
l += 1
1214

13-
return len(s) - l
15+
return (r - l + 1)

python/0567-permutation-in-string.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution:
2-
def checkInclusion(self, s1, s2):
2+
def checkInclusion(self, s1: str, s2: str) -> bool:
33
if len(s1) > len(s2):
44
return False
55

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

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

13-
for l in range(len(s2) - len(s1)):
15+
l = 0
16+
for r in range(len(s1), len(s2)):
1417
if matches == 26:
1518
return True
1619

17-
i = ord(s2[l]) - ord("a")
18-
s2Count[i] -= 1
19-
if s2Count[i] == s1Count[i]:
20+
index = ord(s2[r]) - ord("a")
21+
s2Count[index] += 1
22+
if s1Count[index] == s2Count[index]:
2023
matches += 1
21-
elif s2Count[i] == s1Count[i] - 1:
24+
elif s1Count[index] + 1 == s2Count[index]:
2225
matches -= 1
2326

24-
i = ord(s2[l + len(s1)]) - ord("a")
25-
s2Count[i] += 1
26-
if s2Count[i] == s1Count[i]:
27+
index = ord(s2[l]) - ord("a")
28+
s2Count[index] -= 1
29+
if s1Count[index] == s2Count[index]:
2730
matches += 1
28-
elif s2Count[i] == s1Count[i] + 1:
31+
elif s1Count[index] - 1 == s2Count[index]:
2932
matches -= 1
30-
31-
return matches == 26
33+
l += 1
34+
return matches == 26

python/0705-design-hashset

-40
This file was deleted.

0 commit comments

Comments
 (0)