0% found this document useful (0 votes)
925 views4 pages

LeetCode Problem Solutions in Python

The document contains summaries of 11 LeetCode problems. The problems cover topics such as arrays, strings, hash tables, two pointers, sorting, and heaps. For each problem, the document provides the LeetCode link, a short description of the problem, and a Python code snippet solving the problem in 3 sentences or less.

Uploaded by

sukkhin hegde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
925 views4 pages

LeetCode Problem Solutions in Python

The document contains summaries of 11 LeetCode problems. The problems cover topics such as arrays, strings, hash tables, two pointers, sorting, and heaps. For each problem, the document provides the LeetCode link, a short description of the problem, and a Python code snippet solving the problem in 3 sentences or less.

Uploaded by

sukkhin hegde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Contains Duplicate: Discusses the Contains Duplicate problem, providing a Python code solution and its workings.
  • Valid Anagram: Explores the Valid Anagram problem, presenting Python code for an effective solution.
  • Best Time to Buy and Sell Stock: Gives insight into solving the Best Time to Buy and Sell Stock problem with a profit maximization strategy in Python.
  • Group Anagrams: Details the Group Anagrams problem with a code approach using collections and mapping.
  • Sum: Covers the Sum problem with Python solution, including steps for sorting and adding results.
  • Two Sum II: Discusses the Two Sum II problem, outlining a code-driven solution in Python.
  • Container with Most Water (2 pointer): Presents a problem-solving approach to determine the container that can hold the most water, utilizing a two-pointer technique in Python.
  • Top K Frequent Elements: Outlines a strategy for identifying the top K frequent elements in a list using Python.
  • Longest Substring Without Repeating Characters: Explores a Python solution for finding the length of the longest substring without repeating characters.

LeetCode Problems

1. Contains Duplicate
Link: [Link]
Code: Python3
def containsDuplicate(self, nums: List[int]) -> bool:
repeat = set()
for num in nums:
if num in repeat:
return True
[Link](num)
return False
2. Valid Anagram
Link: [Link]
Code: Python3
from collections import Counter
def isAnagram(s,t) ->bool:
return Counter(s) == Counter(t)
3. Two Sum
Link: [Link]
Code: Python3
def twoSum(self, nums: List[int], target: int) -> List[int]:
prevMap = {} # val -> index
for i, n in enumerate(nums):
diff = target - n
if diff in prevMap:
return [prevMap[diff], i]
prevMap[n] = i
4. Valid palindrome
Link: [Link]
Code: Python3
def isPalindrome(self, s:str) -> bool:
s= "".join(ch for ch in s if [Link]()).lower()
l=0
r = len(s)-1
while(l<r):
if s[l] != s[r]:
return False
l+=1
r-=1
return True
5. Group Anagrams
Link: [Link]
Code: Python3
ans = [Link](list)

for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord("a")] += 1
ans[tuple(count)].append(s)
return [Link]()
6. Two Sum II
Link:
[Link]

Code: Python3
l,r = 0, len(numbers)-1
while(l<r):
sumOfTwo=numbers[l] + numbers[r]
if sumOfTwo > target:
r=r-1
elif sumOfTwo < target:
l=l+1
else:
return [l+1,r+1]
7. 3sum
Link: [Link]
Code: Python3
res=[]
[Link]()

for index,val in enumerate(nums):


if index>0 and val == nums[index-1]:
continue
l, r = index+1, len(nums)-1
while(l<r):
sumOfThree = val + nums[l] + nums[r]
if sumOfThree > 0:
r -=1
elif sumOfThree < 0:
l +=1
else:
[Link]([val, nums[l], nums[r]])
l +=1
while nums[l] == nums[l-1] and l<r:
l +=1
return res
8. Best Time to Buy and Sell Stock
Link: [Link]
Code: Python3
min_price = float('inf')
max_profit = 0
for i in range(len(prices)):
if prices[i] < min_price:
min_price = prices[i]
elif prices[i] - min_price > max_profit:
max_profit = prices[i] - min_price

return max_profit
9. Longest Substring Without Repeating Characters
Link: [Link]
characters/
Code: Python3
charSet = set()
l=0
res = 0

for r in range(len(s)):
while s[r] in charSet:
[Link](s[l])
l += 1
[Link](s[r])
res = max(res, r - l + 1)
return res
10. Container with most water (2 pointer)
Link: [Link]
Code: Python3
l, r = 0, len(height) - 1
res = 0

while l < r:
res = max(res, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
elif height[r] <= height[l]:
r -= 1
return res
11. Top K frequent elements
Link: [Link]
Code: Python3
count = {}
freq = [[] for i in range(len(nums) + 1)]

for n in nums:
count[n] = 1 + [Link](n, 0)
for n, c in [Link]():
freq[c].append(n)
res = []
for i in range(len(freq) - 1, 0, -1):
for n in freq[i]:
[Link](n)
if len(res) == k:
return res

You might also like