Skip to content

Instantly share code, notes, and snippets.

View amarjitdhillon's full-sized avatar
🎯
Focusing

Amarjit Singh Dhillon amarjitdhillon

🎯
Focusing
View GitHub Profile
@amarjitdhillon
amarjitdhillon / container-with-most-water.py
Last active February 7, 2022 05:55
Container With Most Water
class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea, currArea, l, r = 0,0, 0, len(height)-1
while(l < r and l >= 0 and r <= len(height)-1):
currArea = min(height[l],height[r]) * (r-l)
if currArea > maxArea:
maxArea = currArea # udpate the max area
if height[l] < height[r]: # left pointer needs to be incremented
@amarjitdhillon
amarjitdhillon / maximum-subarray.py
Created February 7, 2022 04:03
Maximum Subarray
def maxSubArray(self, nums: List[int]) -> int:
current_sum, max_sum = 0, nums[0] # add first element to max array as len(nums) > 1 (given constaint)
for x in nums:
current_sum += x
if current_sum > max_sum: # update the max sum
max_sum = current_sum
# reset if sum is negative
if current_sum < 0:
@amarjitdhillon
amarjitdhillon / find-peak-element.py
Created February 7, 2022 03:42
Find Peak Element
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
l, r = 0, len(nums)-1
while(l < r):
m = (l+r)//2
if nums[m] > nums[m+1]: # peak is at left for sure, so update the r pointer
r = m
else : # peak is at right for sure, so update l pointer
l = m+1
return l
@amarjitdhillon
amarjitdhillon / find-all-anagrams-in-a-string.py
Created February 7, 2022 02:17
Find All Anagrams in a String
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
'''
1. len(P) >= len(S). if not return [] as there is no anagram
2. Take sliding window of len(P) and slide over the S array. Update the frequecy list for each window
if both lists are same then add the index to result list.append
3. At then end return res list
'''
res, charP, charS = [], [0]*26, [0]*26
@amarjitdhillon
amarjitdhillon / permutation-in-string.py
Created February 7, 2022 02:12
Permutation in String
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s2) < len(s1): # edge case
return False
freq_s1, freq_s2 = [0]*26, [0]*26
# populate freq_s1 list
for x in s1:
freq_s1[ord(x)-ord('a')] += 1
@amarjitdhillon
amarjitdhillon / 3sum.py
Created February 7, 2022 02:09
3 sum problem
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# special case
if len(nums) <= 2:
return []
result = set() # result will contain the unique tuples
nums = sorted(nums) # nlog(n) complexity
@amarjitdhillon
amarjitdhillon / longest-common-prefix.py
Created February 7, 2022 02:06
Longest Common Prefix Leetcode
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
res = '' # final output
if len(strs) == 0: # special case
return res
min_element = min(strs, key = len) # find the min length word in strs
for i in range(len(min_element)):
@amarjitdhillon
amarjitdhillon / group-anagrams.py
Created February 7, 2022 01:58
Group Anagrams
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if len(strs) == 0:
return []
c_dict = {}
for word in strs:
freq_list = [0]*26
@amarjitdhillon
amarjitdhillon / highlight.html
Created August 5, 2019 06:48
Bootstrap class to highlight text
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
\documentclass[letterpaper,12pt,titlepage,final]{report}
\setlength{\marginparwidth}{0pt} % Please customize the margins as per your need
\setlength{\marginparsep}{0pt} % width of space between body text and margin notes
\setlength{\evensidemargin}{0.125in}
\setlength{\oddsidemargin}{0.125in}
\setlength{\textwidth}{6.375in}
\raggedbottom
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{subfig}