Skip to content

Commit bd58946

Browse files
author
Partho Biswas
committed
no message
1 parent c593acc commit bd58946

File tree

3 files changed

+119
-21
lines changed

3 files changed

+119
-21
lines changed

leetcode.com/python/283_Move_Zeroes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,30 @@ def moveZeroes(self, nums):
99
if nums[fastPtr] != 0:
1010
nums[fastPtr], nums[slowPtr] = nums[slowPtr], nums[fastPtr]
1111
slowPtr += 1
12+
13+
14+
15+
# My solution duriing mock
16+
class Solution(object):
17+
def moveZeroes(self, nums):
18+
"""
19+
:type nums: List[int]
20+
:rtype: None Do not return anything, modify nums in-place instead.
21+
"""
22+
if len(nums) <= 1:
23+
return nums
24+
read, write = 0, 0
25+
while read < len(nums):
26+
if nums[read] != 0:
27+
nums[read], nums[write] = nums[write], nums[read]
28+
read += 1
29+
write += 1
30+
else:
31+
read += 1
32+
33+
34+
"""
35+
[0,1,0,3,12]
36+
r
37+
w
38+
"""

leetcode.com/python/314_Binary_Tree_Vertical_Order_Traversal.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,29 @@ def verticalOrder(self, root):
3232
result.append(verticalNodeMap[key])
3333
return result
3434

35+
36+
# My solution during mock, getting TLE, don't know why
37+
from collections import defaultdict
38+
from collections import deque
39+
class Solution(object):
40+
def verticalOrder(self, root):
41+
"""
42+
:type root: TreeNode
43+
:rtype: List[List[int]]
44+
"""
45+
if not root:
46+
return []
47+
orderMap = defaultdict(list)
48+
queue = deque([(root, 0)])
49+
50+
while queue:
51+
currentNode, vLine = queue.popleft()
52+
if currentNode:
53+
orderMap[vLine].append(root.val)
54+
queue.append((root.left, vLine - 1))
55+
queue.append((root.right, vLine + 1))
56+
57+
result = []
58+
for i in range(min(orderMap.keys()), max(orderMap.keys()) + 1):
59+
result.append(orderMap[i])
60+
return result
Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,81 @@
1+
# from collections import Counter
2+
3+
# class Solution(object):
4+
# def subarraySum(self, nums, k):
5+
# """
6+
# :type nums: List[int]
7+
# :type k: int
8+
# :rtype: int
9+
# """
10+
11+
# # Edge case
12+
# if not nums:
13+
# return 0
14+
15+
# # Sliding Window -- No, because the array contains negative number
16+
# # Dictionary + prefixSum technique
17+
# subArrayCount, currentSum = 0, 0
18+
# prefixSumCounter = Counter()
19+
# prefixSumCounter[0] = 1
20+
# for num in nums:
21+
# currentSum += num
22+
# prefixSum = currentSum - k
23+
# if prefixSum in prefixSumCounter:
24+
# subArrayCount += prefixSumCounter[prefixSum]
25+
# prefixSumCounter[currentSum] += 1
26+
# return subArrayCount
27+
28+
29+
# # My solution duriing mock
30+
# # The following solution is basically a slidinng window type solutio which won't work for egative numbers
31+
# class Solution(object):
32+
# def subarraySum(self, nums, k):
33+
# """
34+
# :type nums: List[int]
35+
# :type k: int
36+
# :rtype: int
37+
# """
38+
# if len(nums) <= 0:
39+
# return 0
40+
# prefixSum = [0] + nums
41+
# for i in range(1, len(prefixSum)):
42+
# prefixSum[i] = prefixSum[i - 1] + prefixSum[i]
43+
44+
# left, right, subArrayCount = 0, 1, 0
45+
# while right < len(prefixSum) and left < right:
46+
# currentSum = prefixSum[right] - prefixSum[left]
47+
# if currentSum == k:
48+
# subArrayCount += 1
49+
# right += 1
50+
# elif currentSum < k:
51+
# right += 1
52+
# else:
53+
# left += 1
54+
# return subArrayCount
55+
56+
157
from collections import Counter
258

59+
360
class Solution(object):
461
def subarraySum(self, nums, k):
562
"""
663
:type nums: List[int]
764
:type k: int
865
:rtype: int
966
"""
10-
11-
# Edge case
12-
if not nums:
67+
if len(nums) <= 0:
1368
return 0
1469

15-
# Sliding Window -- No, because the array contains negative number
16-
# Dictionary + prefixSum technique
17-
subArrayCount, currentSum = 0, 0
70+
prefixSum, currentSum, subArrayCount = 0, 0, 0
1871
prefixSumCounter = Counter()
1972
prefixSumCounter[0] = 1
20-
for num in nums:
21-
currentSum += num # increment current sum
22-
prefixSum = currentSum - k
23-
if prefixSum in prefixSumCounter: # check if there is a prefix subarray we can take out to reach k
24-
subArrayCount += prefixSumCounter[prefixSum]
25-
prefixSumCounter[currentSum] += 1 # add current sum to sum count
26-
return subArrayCount
27-
28-
29-
3073

74+
for num in nums:
75+
currentSum += num
76+
requiredPrefixSum = currentSum - k
77+
if requiredPrefixSum in prefixSumCounter:
78+
subArrayCount += prefixSumCounter[requiredPrefixSum]
79+
prefixSumCounter[currentSum] += 1
3180

32-
sol = Solution()
33-
nums = [1,1,1]
34-
k = 2
35-
out = sol.subarraySum(nums, k)
36-
print("Res: ", out)
81+
return subArrayCount

0 commit comments

Comments
 (0)