Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions leetcode-409.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
https://leetcode.com/problems/longest-palindrome/

Time Complexity: O(n)
Space comlexity: O(n)
TIP: Count the total number of even occurences and add it to the totalSum. For odd occurences add `occurence - 1`, and set the oddPresent to true - in the end add 1 to res if oddPresent == true
Refer to optimized way in the video solution s30 to use hashset for adding count
'''
from collections import Counter
class Solution:
def longestPalindrome(self, s: str) -> int:
count = Counter(s)
res = 0
oddPresent = False
for el in count.values():
if el%2 == 0:
res += el
else:
res += (el - 1)
oddPresent = True
if oddPresent:
res += 1
return res
31 changes: 31 additions & 0 deletions leetcode-525.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
https://leetcode.com/problems/contiguous-array/description/

Time Complexity: O(n)
Space complexity: O(n)

TIP: brute force: n*n loop to generate subarrays -> and finding the longest canditate. starting from each index in the array.
How do you eliminate the n*n loop? use running sum to remove the iteration..
since its 0-indexed - you dont need to add an extra 1 to find the diff in the length. this is wrong - i - minIndex[currSum] + 1
'''
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
#when you encounter 0 -> add -1 to running sum - handles edge case, which says when we start the iteration, sum is initially 0 at index -1
runningSum = []
minIndex = {}
minIndex[0] = -1 #handle edge case for first occurence of 0 sum.
currSum = 0
longestLength = 0
for i, el in enumerate(nums):
if el == 0:
currSum -= 1
else:
currSum += 1

# runningSum.append(currSum)
if currSum not in minIndex:
minIndex[currSum] = i
else:
longestLength = max(longestLength, i - minIndex[currSum] )

return longestLength
31 changes: 31 additions & 0 deletions leetcode-560.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
https://leetcode.com/problems/subarray-sum-equals-k/


Time Complexity: O(n)
Space Complexity: O(n)
TIP:
You need to store dictionary as {runningSum: count} cuz you are tyring to count the occurence of the sum. which will give the total count
'''
class Solution:
def subarraySum(self, nums: List[int], target: int) -> int:
runningSumDict = {}
runningSumDict[0] = 1 #edge case - refer to the video to understand the edge case
runningSum = 0
totalCount = 0

for el in nums:
runningSum += el
complement = runningSum - target

if complement in runningSumDict:
totalCount += runningSumDict[complement]

if runningSum not in runningSumDict:
runningSumDict[runningSum] = 1
else:
runningSumDict[runningSum] += 1

return totalCount