Leetcode DSA Sheet by Fraz
Leetcode DSA Sheet by Fraz
EASY
https://leetcode.com/problems/two-sum/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
https://leetcode.com/problems/plus-one/
https://leetcode.com/problems/move-zeroes/
https://leetcode.com/problems/running-sum-of-1d-array/
https://leetcode.com/problems/find-pivot-index/
https://leetcode.com/problems/majority-element/
https://leetcode.com/problems/fibonacci-number/
https://leetcode.com/problems/squares-of-a-sorted-array/
https://leetcode.com/problems/pascals-triangle/
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
MEDIUM
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
https://leetcode.com/problems/merge-intervals/
https://leetcode.com/problems/3sum/
https://leetcode.com/problems/product-of-array-except-self/
https://leetcode.com/problems/insert-delete-getrandom-o1/
https://leetcode.com/problems/subarray-sum-equals-k/
https://leetcode.com/problems/next-permutation/
https://leetcode.com/problems/spiral-matrix/
https://leetcode.com/problems/container-with-most-water/
https://leetcode.com/problems/rotate-image/
https://leetcode.com/problems/word-search/
https://leetcode.com/problems/3sum-closest/
https://leetcode.com/problems/game-of-life/
https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/
https://leetcode.com/problems/4sum/
https://leetcode.com/problems/find-the-duplicate-number/
https://leetcode.com/problems/combination-sum/
https://leetcode.com/problems/jump-game/
https://leetcode.com/problems/jump-game-ii/
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
https://leetcode.com/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
https://leetcode.com/problems/max-area-of-island/
https://leetcode.com/problems/find-all-duplicates-in-an-array/
https://leetcode.com/problems/k-diff-pairs-in-an-array/
https://leetcode.com/problems/subsets/
https://leetcode.com/problems/invalid-transactions/
https://leetcode.com/problems/subarray-sums-divisible-by-k/
HARD
https://leetcode.com/problems/first-missing-positive/
https://leetcode.com/problems/largest-rectangle-in-histogram/
https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
https://leetcode.com/problems/max-value-of-equation/
RECURSION
1- What is recursion
2- How Recursion Works
3- What is a recursion tree
4- https://leetcode.com/problems/powx-n/
5- https://leetcode.com/problems/valid-palindrome/
6- General Steps to recursion problems
7- https://leetcode.com/problems/subsets/
10- https://leetcode.com/problems/subsets-ii/
8- https://leetcode.com/problems/permutations/
9- https://leetcode.com/problems/permutations-ii/
11- https://leetcode.com/problems/combinations/
12- https://leetcode.com/problems/combination-sum/
13- https://leetcode.com/problems/combination-sum-ii/
14- https://leetcode.com/problems/combination-sum-iii/
15- https://leetcode.com/problems/letter-combinations-of-a-phone-number/
16- https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
17- https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/
18- https://leetcode.com/problems/flood-fill/
19- https://leetcode.com/problems/word-search/
20- https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1
21- https://leetcode.com/problems/n-queens/
DYNAMIC PROGRAMING
EASY
https://leetcode.com/problems/maximum-subarray/
https://leetcode.com/problems/climbing-stairs
https://leetcode.com/problems/divisor-game/
https://leetcode.com/problems/counting-bits/
MEDIUM
https://leetcode.com/problems/decode-ways/
https://leetcode.com/problems/word-break/
https://leetcode.com/problems/maximal-square/
https://leetcode.com/problems/coin-change/
https://leetcode.com/problems/maximum-product-subarray/
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
https://leetcode.com/problems/palindromic-substrings/
https://leetcode.com/problems/house-robber/
https://leetcode.com/problems/delete-and-earn/
https://leetcode.com/problems/continuous-subarray-sum/
https://leetcode.com/problems/knight-dialer/
https://leetcode.com/problems/longest-increasing-subsequence/
https://leetcode.com/problems/unique-paths/
https://leetcode.com/problems/count-square-submatrices-with-all-ones/
https://leetcode.com/problems/range-sum-query-2d-immutable/
https://leetcode.com/problems/longest-arithmetic-subsequence/
HARD
https://leetcode.com/problems/trapping-rain-water/
https://leetcode.com/problems/word-break-ii/
https://leetcode.com/problems/regular-expression-matching/
https://leetcode.com/problems/maximal-rectangle/
https://leetcode.com/problems/longest-valid-parentheses/
https://leetcode.com/problems/edit-distance/
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/
https://leetcode.com/problems/frog-jump/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
https://leetcode.com/problems/burst-balloons/
https://leetcode.com/problems/minimum-cost-to-merge-stones/
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/
https://leetcode.com/problems/super-egg-drop/
https://leetcode.com/problems/count-different-palindromic-subsequences/
https://leetcode.com/problems/minimum-cost-to-cut-a-stick/
STRINGS
EASY
https://leetcode.com/problems/add-strings/
https://leetcode.com/problems/longest-common-prefix/
https://leetcode.com/problems/valid-palindrome-ii/
https://leetcode.com/problems/roman-to-integer/
https://leetcode.com/problems/implement-strstr/
MEDIUM
https://leetcode.com/problems/longest-substring-without-repeating-characters/
https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
https://leetcode.com/problems/longest-palindromic-substring/
https://leetcode.com/problems/group-anagrams/
https://leetcode.com/problems/generate-parentheses/
https://leetcode.com/problems/basic-calculator-ii/
https://leetcode.com/problems/integer-to-roman/
https://leetcode.com/problems/reverse-words-in-a-string/
https://leetcode.com/problems/simplify-path/
https://leetcode.com/problems/zigzag-conversion/
HARD
https://leetcode.com/problems/text-justification/
https://leetcode.com/problems/integer-to-english-words/
https://leetcode.com/problems/minimum-window-substring/
https://leetcode.com/problems/valid-number/
https://leetcode.com/problems/distinct-subsequences/
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
MATHS
EASY
https://leetcode.com/problems/reverse-integer/
https://leetcode.com/problems/add-binary/
https://leetcode.com/problems/palindrome-number/
https://leetcode.com/problems/happy-number/
https://leetcode.com/problems/excel-sheet-column-title/
https://leetcode.com/problems/missing-number/
https://leetcode.com/problems/maximum-product-of-three-numbers/
https://leetcode.com/problems/power-of-two/
MEDIUM
https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
https://leetcode.com/problems/encode-and-decode-tinyurl/
https://leetcode.com/problems/string-to-integer-atoi/
https://leetcode.com/problems/multiply-strings/
https://leetcode.com/problems/angle-between-hands-of-a-clock/
https://leetcode.com/problems/integer-break/
https://leetcode.com/problems/valid-square/
https://leetcode.com/problems/the-kth-factor-of-n/
HARD
https://leetcode.com/problems/basic-calculator/
https://leetcode.com/problems/max-points-on-a-line/
https://leetcode.com/problems/permutation-sequence/
https://leetcode.com/problems/number-of-digit-one/
GREEDY
MEDIUM
https://leetcode.com/problems/task-scheduler/
https://leetcode.com/problems/gas-station/
https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/
https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
https://leetcode.com/problems/remove-k-digits/
https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/
https://leetcode.com/problems/non-overlapping-intervals/
HARD
https://leetcode.com/problems/candy/
https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/
https://leetcode.com/problems/create-maximum-number/
DFS
MEDIUM
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
https://leetcode.com/problems/course-schedule-ii/
https://leetcode.com/problems/decode-string/
https://leetcode.com/problems/number-of-provinces/
https://leetcode.com/problems/clone-graph/
https://leetcode.com/problems/shortest-bridge/
https://leetcode.com/problems/all-paths-from-source-to-target/
https://leetcode.com/problems/surrounded-regions//
https://leetcode.com/problems/house-robber-iii/
HARD
https://leetcode.com/problems/critical-connections-in-a-network/
https://leetcode.com/problems/remove-invalid-parentheses/
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
https://leetcode.com/problems/concatenated-words/
https://leetcode.com/problems/making-a-large-island/
https://leetcode.com/problems/contain-virus/
https://leetcode.com/problems/24-game/
https://leetcode.com/problems/remove-boxes/
TREE
EASY
https://leetcode.com/problems/diameter-of-binary-tree/
https://leetcode.com/problems/invert-binary-tree/
https://leetcode.com/problems/subtree-of-another-tree/
https://leetcode.com/problems/range-sum-of-bst/
https://leetcode.com/problems/symmetric-tree/
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
https://leetcode.com/problems/merge-two-binary-trees/
https://leetcode.com/problems/maximum-depth-of-binary-tree/
https://leetcode.com/problems/binary-tree-paths/
https://leetcode.com/problems/same-tree/
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
https://leetcode.com/problems/path-sum/
https://leetcode.com/problems/minimum-absolute-difference-in-bst/
https://leetcode.com/problems/sum-of-left-leaves/
https://leetcode.com/problems/balanced-binary-tree/
https://leetcode.com/problems/binary-tree-inorder-traversal/ (USING STACK TOO)
MEDIUM
https://leetcode.com/problems/count-good-nodes-in-binary-tree/
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
https://leetcode.com/problems/binary-tree-right-side-view/
https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
https://leetcode.com/problems/validate-binary-search-tree/
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
https://leetcode.com/problems/binary-search-tree-iterator/
https://leetcode.com/problems/binary-tree-level-order-traversal/
https://leetcode.com/problems/path-sum-iii/
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
https://leetcode.com/problems/unique-binary-search-trees/
https://leetcode.com/problems/recover-binary-search-tree/
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
https://leetcode.com/problems/maximum-width-of-binary-tree/
https://leetcode.com/problems/unique-binary-search-trees-ii/
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
https://leetcode.com/problems/redundant-connection/
HARD
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
https://leetcode.com/problems/binary-tree-maximum-path-sum/
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/
https://leetcode.com/problems/binary-tree-cameras/
https://leetcode.com/problems/sum-of-distances-in-tree/
https://leetcode.com/problems/number-of-ways-to-reconstruct-a-tree/
https://leetcode.com/problems/redundant-connection-ii/
HASH TABLE
EASY
https://leetcode.com/problems/verifying-an-alien-dictionary/
https://leetcode.com/problems/design-hashmap/
MEDIUM
https://leetcode.com/problems/top-k-frequent-elements/
https://leetcode.com/problems/design-twitter/
BINARY SEARCH
EASY
https://leetcode.com/problems/sqrtx/
https://leetcode.com/problems/binary-search/
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
https://leetcode.com/problems/peak-index-in-a-mountain-array/
MEDIUM
https://leetcode.com/problems/time-based-key-value-store/
https://leetcode.com/problems/search-in-rotated-sorted-array/
https://leetcode.com/problems/powx-n/
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
https://leetcode.com/problems/find-peak-element/
https://leetcode.com/problems/search-a-2d-matrix/
https://leetcode.com/problems/divide-two-integers/
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/
HARD
https://leetcode.com/problems/median-of-two-sorted-arrays/
https://leetcode.com/problems/count-of-smaller-numbers-after-self/
https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k/
https://leetcode.com/problems/split-array-largest-sum/
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/
BFS
MEDIUM
https://leetcode.com/problems/number-of-islands/
https://leetcode.com/problems/rotting-oranges/
https://leetcode.com/problems/snakes-and-ladders/
https://leetcode.com/problems/is-graph-bipartite/
https://leetcode.com/problems/minimum-jumps-to-reach-home/
HARD
https://leetcode.com/problems/word-ladder/
https://leetcode.com/problems/word-ladder-ii/
https://leetcode.com/problems/cut-off-trees-for-golf-event/
https://leetcode.com/problems/reachable-nodes-in-subdivided-graph/
TWO POINTER
MEDIUM / HARD
https://leetcode.com/problems/partition-labels/
https://leetcode.com/problems/sort-colors/
https://leetcode.com/problems/longest-repeating-character-replacement/
https://leetcode.com/problems/maximum-number-of-visible-points/
https://leetcode.com/problems/subarrays-with-k-different-integers/
STACK
EASY
https://leetcode.com/problems/min-stack/
https://leetcode.com/problems/next-greater-element-i/
https://leetcode.com/problems/backspace-string-compare/
https://leetcode.com/problems/implement-queue-using-stacks/
PRE , POST , INORDER TRAVERSALS OF BINARY TREE
https://leetcode.com/problems/implement-stack-using-queues/
MEDIUM
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/
https://leetcode.com/problems/daily-temperatures/
https://leetcode.com/problems/flatten-nested-list-iterator/
https://leetcode.com/problems/online-stock-span/
https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
https://leetcode.com/problems/sum-of-subarray-minimums/
https://leetcode.com/problems/evaluate-reverse-polish-notation/
DESIGN
MEDIUM / HARD
https://leetcode.com/problems/lru-cache/
https://leetcode.com/problems/find-median-from-data-stream/
https://leetcode.com/problems/design-underground-system/
https://leetcode.com/problems/lfu-cache/
https://leetcode.com/problems/tweet-counts-per-frequency/
https://leetcode.com/problems/all-oone-data-structure/
https://leetcode.com/problems/design-browser-history/
GRAPH
EASY
https://leetcode.com/problems/employee-importance/
https://leetcode.com/problems/find-the-town-judge/
MEDIUM
https://leetcode.com/problems/evaluate-division/
https://leetcode.com/problems/accounts-merge/
https://leetcode.com/problems/network-delay-time/
https://leetcode.com/problems/find-eventual-safe-states/
https://leetcode.com/problems/keys-and-rooms/
https://leetcode.com/problems/possible-bipartition/
https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/
https://leetcode.com/problems/regions-cut-by-slashes/
https://leetcode.com/problems/satisfiability-of-equality-equations/
https://leetcode.com/problems/as-far-from-land-as-possible/
https://leetcode.com/problems/number-of-closed-islands/
https://leetcode.com/problems/number-of-operations-to-make-network-connected/
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
https://leetcode.com/problems/time-needed-to-inform-all-employees/
BIT MANIPULATION
https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-
LINKED LIST
HEAP
MEDIUM
https://leetcode.com/problems/k-closest-points-to-origin/
https://leetcode.com/problems/kth-largest-element-in-an-array/
https://leetcode.com/problems/reorganize-string/
https://leetcode.com/problems/furthest-building-you-can-reach/
https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
https://leetcode.com/problems/cheapest-flights-within-k-stops/
https://leetcode.com/problems/find-the-most-competitive-subsequence/
https://leetcode.com/problems/ugly-number-ii/
HARD
https://leetcode.com/problems/merge-k-sorted-lists/
https://leetcode.com/problems/sliding-window-maximum/
https://leetcode.com/problems/the-skyline-problem/
https://leetcode.com/problems/trapping-rain-water-ii/
https://leetcode.com/problems/minimum-number-of-refueling-stops/
https://leetcode.com/problems/swim-in-rising-water/
https://leetcode.com/problems/shortest-path-to-get-all-keys/
https://leetcode.com/problems/minimum-cost-to-hire-k-workers/
https://leetcode.com/problems/k-th-smallest-prime-fraction/
SLIDING WINDOW
MEDIUM/HARD
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
https://leetcode.com/problems/max-consecutive-ones-iii/
https://leetcode.com/problems/grumpy-bookstore-owner/
https://leetcode.com/problems/sliding-window-median/
TRIE
https://leetcode.com/explore/learn/card/trie/
SEGMENT TREE
https://leetcode.com/articles/a-recursive-approach-to-segment-trees-range-sum-queries-lazy-propagation/
Concept
O(n): use hash table(store current value as key with its index as value). whenever target-current is in has
O(n): use 2 pointer approach. take max r-l. when r-l<0 l becomes r. or use DP+greedy approach. store, u
O(m+n): fill from backside. 3 pointers: m-1, n-1, m+n-1
O(n): 2 pointer. l=0. r=0 to n-1. if r's not 0 swap l's, r's, l++
O(n): for i=1 to n-1, add i-1's to i's
O(n): find sum of array. for i=0 to n-1, if sum-leftsum-i's = leftsum return i, add i's to leftsum
O(n): maintain a count, ans. for i=0 to n-1, if count=0 update ans as i's, if ans=i's ++count else --count
O(n): if n<=1 return 1. for i=2 to n, sum=n1+n2, n1=n2, n2=sum. return sum
O(n): fill from back. 2 pointer approach. l=0, r=n-1. for i=n-1 to 0, if abs(l's)>abs(r's) take l's^2 else r's^2
O(n): for i=0 to n-1, append 1 at beginning, for j=1 to its size-2, set j's as j's+j+1's, add a copy of it to ans
O(n): maintain a pointer. for i=1 to n-1,if i-1's != i's, pointer's=i's, increment pointer
O(n): [Greeedy] peak valley approach. for i=1 to n-1, if i's and i-1's difference is +ve, add to ans
O(nlogn): sort intervals based on 1st value. 2 pointer approach. if x1<=x2<=y1, make y1 as y2 if y2 is big.
O(n2): sort array. for i=0 to n-3 use j,k pointers to find sum. if its 0 add to ans, elseif its >0 move k to left,
O(n): maintain a left array which is product of elements to its left. make int right=1. for i=n-1 to 0, left[i
rt-delete-getrandom-o1/
O(n): if (sum upto jth element - sum upto ith element)(from 0th index)=target that means sum of elements
O(n): find an index i from backwards such that it is < all of its right ones. swap i's with the rightmost number > i. reve
O(mn):
O(n): 2 take rowbegin,end
pointer andr=n-1.
approach. l=0, colbegin,end.
while l<r,move
ans isaccordingly.
max of (min of l's, r's x gap b/w l,r) whichever is less move tha
Concept: between any two selected l,r no matter how big heights are there in b/w, they store the same amount of w
O(mn): find therows
O(n): reverse states as perand
of matrix rulesthen
andtranspose.
update the status in a way to represent both old and new states.
// 0 = dead -> dead
O(m*n*(4^s)): at each index call search method. maintain visited matrix. in that method, return true if entire word is
// 1 = live -> live
O(n2): same
// 2 as 3->sum.
= dead live but we find min diffference sum.
// 3 = live -> dead
O(n): use hashmap to store remainder counts or array of length of possible remainder. for i=0 to n-1 add 60-remain
O(n^(k-1)): see editorial for generalized ksum
O(n): like loop in linked list. after intersection, restart 1st pointer and continue. 2(F+a)=F+nC+a => F+a=nC => let n=
O(nx(2^n)): helper backtrack function: add temp to ans if remain=0. return if its <0. else for i=0 to n-1,
O(n): for i=n-2 to 0, if you can reach goal/beyond goal from i, move goal to i. if goal is 0 return true else false. purpo
O(n): greedy approach. use near, far pointers. start from near to far, find the farthest point you can rea
O(n): we are concerned only about 1st k sum and last k sum. so find one of them(here right). for i=0 to k-1, add i's t
O(nlogn): sort both. find max diff in both. return their product
O(mn): for all indices call helper function, which uses dfs to find its surrounding 1s
O(n): as range of numbers is 1 to n in array of length n, for i=0 to n-1, if array[array[i]-1] is -ve it has occured alread
O(n): 2 pointer approach after sorting. if k is 0 increase count and move until next unique element. else move left, ri
O(nx(2^n)): helper backtrack function: if we reach end of array, add temp to ans and stop. add i's to temp
O(n2): create objects for ease. use hashmap to link them by name. for every name check if its transactions are vali
O(n): if sum[0, i] % K == sum[0, j] % K, sum[i + 1, j] is divisible by by K. So for current index j, we need to find out h
O(n): add a 0 at end of array or store it seperately. answer could be 1 to length+1. so make n=length+1. remove -ve
O(n): maintain lessfromleft, lessfromright arrays. for i=0 to n-1, maxarea including ith bar will be i's x its lessfromrigh
rt-delete-getrandom-o1-duplicates-allowed/
O(n): minimize cost1. maximize profit1. minimize cost2. maximize profit2.
O(n^2): for i=0 to n-1, if flag<=i, make flag as its next one i+1. for j=flag to n-1, if x's diff violated break,
***imp: https://leetcode.com/problems/combination-sum/solutions/16502/a-general-approach-to-backtracking-ques
O(n): if n==0 return 1. if n<0, x=1/x. return pow( x*x, n/2) if n is even else x*pow( x*x, n/2). for n<0 also
O(n): Recursion: clean the string with only alphanum. if leftmost and rightmost letters are same, recursively ca
O(n*(2^n)): in recursive call add temp to ans. for i=start to n-1, add i's to temp, call i+1, remove lastone
same as 7 but after add, call, remove, for next index we need not repeat adding, calling, removing as we
same as 7 but add temp to ans only if its of length n.else for i=0 to n-1, if i's not in temp do add, call, re
same as 8 but we need to sort array and maintain vis array. before adding to temp check if its used or if its
same as 7 but add only if k is 0. otherwise for j=start to n-k+1 add, call by reducing k and increasing sta
similar to 11 but we add only if remain is 0, return if remain<0, else for i=start to n-1, add, call by reduc
same as 12 but we skip if nums[i]==nums[i-1] and also call for i+1
same as 13 but we add if remain is 0 and k is 0, return if remain<0 or k=0, else we add, call for k-1, rem
similar to 11 but used an array of keypad. add to answer if given string length is equal to i. else for j=0 to
if sum%k is not 0 return false. sort array. we consider from right to left so that we can stop when we reach -
in recursive method, if we reach end of array return 0. create a set of all elements of sofar formed string,
O(n): if given color is not present at that place call: if i,j are valid and initial color is present at b[i][j], mak
use vis matrix. search at every index. if index of letter being searched equals length of search word return tr
org/problems/rat-in-a-maze-problem/1
maintain used col, diagleft, diagright. recursion: if row==n we completed all queens so add to ans, return. fo
O(n): kadanes alg: for i=0 to n-1, cursum+=i's, update maxsum, if cursum<0, restart it. DP: use dp array to store ma
O(n): if n<4 return n. dp[0]=1, dp[1]=2. for i=2 to n-1, dp[i]=dp[i-1]+dp[i-2]. return dp[n-1]
O(1): if n is even alice can choose 1 and win atlast. if n is odd he will definitely lose. alice: odd=odd*o
O(n): for i=1 to n, dp[i]=dp[i/2]+i%2. (or) maintain current power of 2 as p. any number is of form 1xxx where (num o
O(n): dp[n]=1. for i=n-1 to 0, if i's not '0' dp[i]=dp[i+1] and check i+1's is valid then dp[i]+=dp[i+2]. return d
O(n): dp[0]=true. for i=1 to n, for j=0 to i-1, if dp[j] && substring from j to i is in dict, dp[i]=true, break. retu
O(mn): at any index, max length of square is based on its left, top, diagonal max length. for i=1 to r, for j=1 to c, if m
O(n):
O(n): do product paralelly from left and right. if 0 encounter make product 1. find max of left,right,ans each time
O(n2): dp[i][j] denotes longest common prefix of A[i:] and B[j:]. fill from backwards. if A[i]==B[j] dp[i][j]=dp[i+1][j+1]+1
O(n2): dp[i][j] denotes whether s[i:j+1] is a palindrome or not. move i backwards and j forward from i. if i's==j's and (
O(n): we have only concern about max upto i-2 so that we could rob them together with i's and max upto i
O(n): take an array of size of max element allowed. for i=0 to its size add num[i] to index num[i]. for i=2 to last, i's=m
O(n): dp[r]=i denotes remainder of sum of array of length i when divided by k is r. so each time we return true if that
dp[i][j] denotes number of length i starting with j. for k=0 to 9 we call helper function with length n-1, starting with k a
est-increasing-subsequence/
O(mn): DP. dp[i][j] stores how many ways to get there from start point. for i=0 to m-1, for j=0 to n-1 dp[i][j]=dp[i-1][j]+
O(mn): without extra storage we can modify actual matrix. for i=0 to m-1, for j=0 to n-1 if a[i][j] is 1 and i>0, j>0 then
O(mn) for cumulative sum //(Sum(OD)=Sum(OC)+Sum(OB)−Sum(OA)+D since OA is included twice in OB and OC
O(n2): maintain dp array of hashmaps. dp[i] contains hashmap of diff:right index. for r=0 to n-1, create new hashma
O(n): DP use left highest, right highest arrays. for i=1 to n-2, if current height is lesser than its left, right water stored
ular-expression-matching/
imal-rectangle/
est-valid-parentheses/
mum-difficulty-of-a-job-schedule/
-time-to-buy-and-sell-stock-iv/
mum-cost-to-merge-stones/
mum-insertion-steps-to-make-a-string-palindrome/
er-egg-drop/
nt-different-palindromic-subsequences/
mum-cost-to-cut-a-stick/
O(n): take a stringbuffer and append individual digits sum from ones place to left. atlast reverse it and convert to str
O(mn): horizontal scanning: take 1st word as ans. for i=1 to n-1 compare i's with ans and shorten it. (or) vertical sc
O(n): 2 pointer approach. whenever l's != r's check if l+1 to r or l to r-1 is palindrome
O(n): take hashmap of values. scan from right to left and compare itself with its next. if cur<next subtract it else add
O(mn): for i=0 to n-m-1, for j=0 to m-1, if haystack[i]!=needle[j] break. if j==m-1 return i
O(n): sliding window. use integer array[128] to store recent index of chars. maintain l, r pointers. while r<end, if arra
do in python easily. for java use stack and string builder
O(n^2): dp[i][j] denotes index i to j is a palindrome. bottom up approach. if i's==j's and substring from i to j is of leng
O(NKlogK): use hashmap with key as sorted string and value as list of actual string. for each str, sort it a
O(4^n/root n) DFS: method takes ans, left, right, n, str. if str len is n*2 add it to ans and return. if left<n i.e '(' can stil
O(n): using stack to store only numbers. [concept: (expression=a prevop curnum curop b). curop doesnt matter, if p
runs atmost 27 times. O(1): maintain 2 arrays of unique numbers(nums) and roman values(rom) from 100 to 1. for
O(n): take char array of string. reverse entire string. then reverse each word(i=0, j=0. while(i<n) i=j, skip spaces with
O(n): use stack. split string to string array at '/'. for all strings in array, if =.. and stack isn't empty, pop, else if !='','.','.
O(mn): while i<numrows, j=i-1, if i is 1 or numrows, just add j+numrows-1+numrows-1's until its<n. else a=numrows
O(num of words*avg len of words): while all words aren't included, take an empty string builder and add ith str to it.
O(1): take <20 in array, tens in array. in helper function if n>billion, helper(n/billion)+" billion "+helper(n%billion). do
O(s+t): take a hashmap with frequency of each char in t. while(i<=j && j<n), loop until all char in t are covered b/w i
O(n): use numseen, dotseen, eseen flags. for i=0 to n-1, if its digit, numseen=1. else if its dot, if dotseen or eseen re
O(st): DP. dp[i][j] denotes num of times t[:i] is present in s[:j]. make 1st row as 1s. for i=0 to len(t), for j=0 to len(s), i
learn priority q
string-with-concatenation-of-all-words/
mum-moves-to-equal-array-elements/
ode-and-decode-tinyurl/
g-to-integer-atoi/
iply-strings/
e-between-hands-of-a-clock/
kth-factor-of-n/
c-calculator/
-points-on-a-line/
mutation-sequence/
mber-of-digit-one/
-scheduler/
mum-deletion-cost-to-avoid-repeating-letters/
imum-number-of-events-that-can-be-attended/
mum-deletions-to-make-character-frequencies-unique/
ove-k-digits/
ore-the-array-from-adjacent-pairs/
-overlapping-intervals/
mum-number-of-taps-to-open-to-water-a-garden/
ate-maximum-number/
r-combinations-of-a-phone-number/
rse-schedule-ii/
mber-of-provinces/
rtest-bridge/
aths-from-source-to-target/
ounded-regions//
se-robber-iii/
cal-connections-in-a-network/
ove-invalid-parentheses/
est-increasing-path-in-a-matrix/
catenated-words/
ing-a-large-island/
meter-of-binary-tree/
rt-binary-tree/
ree-of-another-tree/
ge-sum-of-bst/
metric-tree/
vert-sorted-array-to-binary-search-tree/
ge-two-binary-trees/
imum-depth-of-binary-tree/
ry-tree-paths/
est-common-ancestor-of-a-binary-search-tree/
mum-absolute-difference-in-bst/
-of-left-leaves/
nced-binary-tree/
ry-tree-inorder-traversal/ (USING STACK TOO)
nt-good-nodes-in-binary-tree/
est-common-ancestor-of-a-binary-tree/
ry-tree-right-side-view/
odes-distance-k-in-binary-tree/
date-binary-search-tree/
ry-tree-zigzag-level-order-traversal/
ry-search-tree-iterator/
ry-tree-level-order-traversal/
struct-binary-tree-from-preorder-and-postorder-traversal/
ue-binary-search-trees/
over-binary-search-tree/
ulating-next-right-pointers-in-each-node/
en-binary-tree-to-linked-list/
imum-width-of-binary-tree/
ue-binary-search-trees-ii/
smallest-element-in-a-bst/
undant-connection/
alize-and-deserialize-binary-tree/
ry-tree-maximum-path-sum/
cal-order-traversal-of-a-binary-tree/
ry-tree-cameras/
-of-distances-in-tree/
mber-of-ways-to-reconstruct-a-tree/
undant-connection-ii/
ying-an-alien-dictionary/
gn-hashmap/
k-frequent-elements/
nt-negative-numbers-in-a-sorted-matrix/
k-index-in-a-mountain-array/
-based-key-value-store/
rch-in-rotated-sorted-array/
-first-and-last-position-of-element-in-sorted-array/
-peak-element/
rch-a-2d-matrix/
de-two-integers/
acity-to-ship-packages-within-d-days/
mum-limit-of-balls-in-a-bag/
dian-of-two-sorted-arrays/
nt-of-smaller-numbers-after-self/
-sum-of-rectangle-no-larger-than-k/
-array-largest-sum/
rtest-subarray-with-sum-at-least-k/
mber-of-islands/
ng-oranges/
kes-and-ladders/
raph-bipartite/
mum-jumps-to-reach-home/
off-trees-for-golf-event/
chable-nodes-in-subdivided-graph/
ition-labels/
est-repeating-character-replacement/
imum-number-of-visible-points/
arrays-with-k-different-integers/
-greater-element-i/
kspace-string-compare/
ement-queue-using-stacks/
SALS OF BINARY TREE
ement-stack-using-queues/
ove-all-adjacent-duplicates-in-string-ii/
y-temperatures/
en-nested-list-iterator/
ne-stock-span/
mum-cost-tree-from-leaf-values/
-of-subarray-minimums/
uate-reverse-polish-notation/
-median-from-data-stream/
gn-underground-system/
et-counts-per-frequency/
one-data-structure/
gn-browser-history/
ployee-importance/
-the-town-judge/
uate-division/
ounts-merge/
work-delay-time/
-eventual-safe-states/
s-and-rooms/
sible-bipartition/
t-stones-removed-with-same-row-or-column/
ons-cut-by-slashes/
sfiability-of-equality-equations/
ar-from-land-as-possible/
mber-of-closed-islands/
mber-of-operations-to-make-network-connected/
-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
-needed-to-inform-all-employees/
-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently
elete-node-in-a-linked-list/
middle-of-the-linked-list/
onvert-binary-number-in-a-linked-list-to-integer/
esign-hashset/
esign-hashmap/
reverse-linked-list/
tps://leetcode.com/problems/reverse-nodes-in-k-group/ ]
O EXTRA SPACE)
merge-two-sorted-lists/
ms/merge-k-sorted-lists/
remove-duplicates-from-sorted-list/
linked-list-cycle/
linked-list-cycle-ii/
intersection-of-two-linked-lists/
palindrome-linked-list/
remove-linked-list-elements/
design-browser-history/
osest-points-to-origin/
argest-element-in-an-array/
ganize-string/
hest-building-you-can-reach/
smallest-element-in-a-sorted-matrix/
apest-flights-within-k-stops/
-the-most-competitive-subsequence/
ge-k-sorted-lists/
ng-window-maximum/
skyline-problem/
ping-rain-water-ii/
mum-number-of-refueling-stops/
m-in-rising-water/
rtest-path-to-get-all-keys/
mum-cost-to-hire-k-workers/
-smallest-prime-fraction/
est-substring-with-at-least-k-repeating-characters/
-consecutive-ones-iii/
mpy-bookstore-owner/
ng-window-median/
rsive-approach-to-segment-trees-range-sum-queries-lazy-propagation/
Solution link Editorials
length of possible remainder. for i=0 to n-1 add 60-remainder's frequency to answer, update remainder count.
https://www.youtube.com/watch?v=UG1C-otvF2U
ointer and continue. 2(F+a)=F+nC+a => F+a=nC => let n=1. after intersection, we want C-a i.e F+a-a=F
remain=0. return if its <0. else for i=0 to n-1, https://www.youtube.com/watch?v=yEAHwSS8HN0
m i, move goal to i. if goal is 0 return true else false. purpose is to find an index which can't be reached by any of its left indices
near to far, find the farthest point you can rea https://www.youtube.com/watch?v=1DkVU2i3sOA
um. so find one of them(here right). for i=0 to k-1, add i's to left sum, subtract n-k+i's from right sum, upadate max left+right. Ba
/solutions/16502/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-part
https://youtu.be/LgaFY8WyTX8
https://youtu.be/dWsQroffE-U
https://youtu.be/2l3cSfE885g
f n is even else x*pow( x*x, n/2). for n<0 also https://youtu.be/an9WRz8QhkA
ftmost and rightmost letters are same, recursively https://youtu.be/m5Cx_AQnpfE
ca
https://youtu.be/quqHd8UHbzs
n-1, add i's to temp, call i+1, remove lastone https://youtu.be/0N3RCWa8jFM
eed not repeat adding, calling, removing as we https://youtu.be/u40eYbmT9zg
for i=0 to n-1, if i's not in temp do add, call, re https://youtu.be/jRyQVVvYBAM
ay. before adding to temp check if its used or if its https://youtu.be/lQaNrfA86B0
k+1 add, call by reducing k and increasing sta https://youtu.be/DTFy9spEQGo
n<0, else for i=start to n-1, add, call by reduc https://youtu.be/Hca7284gCpI
https://youtu.be/6fARitpO0p8
emain<0 or k=0, else we add, call for k-1, rem https://youtu.be/xNeLP5RDY-c
f given string length is equal to i. else for j=0 to https://youtu.be/qyHlIdxk820
m right to left so that we can stop when we reach https://youtu.be/h_6MldQ8vB8
-
ate a set of all elements of sofar formed string, https://youtu.be/DIJQ8zLeVEk
e valid and initial color is present at b[i][j], mak https://youtu.be/RjO-HDjRMpM
ng searched equals length of search word returnhttps://youtu.be/hPyR1un7_hw
tr
https://youtu.be/XiJ0Qq1LD5g
we completed all queens so add to ans, return.https://youtu.be/VrySrTVAI3Y
fo
xsum, if cursum<0, restart it. DP: use dp array to store maxsofar till i index.for i=1 to n-1 if dp[i-1]>0 add it to dp[i], dp[i]+=i's, up
=dp[i-1]+dp[i-2]. return dp[n-1]
odd he will definitely lose. alice: odd=odd*o https://www.youtube.com/watch?v=UbE4-ONpJcc
power of 2 as p. any number is of form 1xxx where (num of 1s in xxx)+1 is answer. for i=1 to n, if i==p*2 p=p*2, dp[i]=dp[i-p]+1
m start point. for i=0 to m-1, for j=0 to n-1 dp[i][j]=dp[i-1][j]+dp[i][[j-1] as we can only travel right, down. (or) O(1): Math solutio
. for i=0 to m-1, for j=0 to n-1 if a[i][j] is 1 and i>0, j>0 then a[i][j]=min(a[i][j-1],a[i-1][j-1],a[i-1][j])+1. add a[i][j] to ans.
B)−Sum(OA)+D since OA is included twice in OB and OC) and O(1) for each query // (Sum(ABCD)=Sum(OD)−Sum(OB)−Sum
hmap of diff:right index. for r=0 to n-1, create new hashmap, for l=0 to r-1 if that diff has occured already upto left, use its value
-2, if current height is lesser than its left, right water stored is min of those - its height. (or) O(n): without extra storage: just hav
https://www.youtube.com/watch?v=mFwf1YbH-Jk
nt index of chars. maintain l, r pointers. while r<end, if array[charat(r)] is not null and is between l,r make l as array[charat(r)]+1.
m up approach. if i's==j's and substring from i to j is of length<=2 or substring from i+1 to j-1 is a palindrom mark dp[i][j] as 1. if it
alue as list of actual string. for each str, sort it a https://www.youtube.com/watch?v=NVIm5_TIqUs
str len is n*2 add it to ans and return. if left<n i.e '(' can still be added, call for left+1, str+'('. if right<left i.e ')' can be added to ma
ssion=a prevop curnum curop b). curop doesnt matter, if prevop is +/- no need to evaluate a (prevop curnum), just push curnum
umbers(nums) and roman values(rom) from 100 to 1. for i=0; n>=0; i++ while n>=nums[i], add rom[i] to answer and subtract it
reverse each word(i=0, j=0. while(i<n) i=j, skip spaces with i, j=i, skip non spaces with j, reverse i to j-1). then remove extra spa
ngs in array, if =.. and stack isn't empty, pop, else if !='','.','..' push it. if stack is empty return /. while stack not empty, insert /,pop
dd j+numrows-1+numrows-1's until its<n. else a=numrows-1+numrows-1, b=i-1+i-1, j+a's and j+b's until <n
ncluded, take an empty string builder and add ith str to it. find num of words that can be added with single space . if no words c
n>billion, helper(n/billion)+" billion "+helper(n%billion). do same for >million, >thousand, >hundred, >20(" "), >10
while(i<=j && j<n), loop until all char in t are covered b/w i and j of s(if char found reduce its freq and count). if count becomes 0
f its digit, numseen=1. else if its dot, if dotseen or eseen return 0, doseen=1. else if its e/E eseen or not numseen return 0, num
[:j]. make 1st row as 1s. for i=0 to len(t), for j=0 to len(s), if t[i]==s[j], dp[i+1][j+1]=dp[i][j]+dp[i+1][j] i.e num of times t[:i+1] occure
s*10+num%10
=carry, if i>=0 sum+=i's, if j>=0 sum+=j's, if sum<2 concat it with ans and carry=0. else concat sum-2 with ans and carry=1. i--, j
https://leetcode.com/problems/palindrome-number/submissions/967409763/
ant go infinitely because next num of any num https://www.youtube.com/watch?v=ocDwEjRVDAk
https://www.youtube.com/watch?v=TunTV2-griM
https://www.youtube.com/watch?v=B9bSSOnc0CQ,https://www.youtube.com/w
https://www.youtube.com/watch?v=9-TXIVEXX2w
pulation-to-solve-problems-easily-and-efficiently
https://youtu.be/fxuVD1e66QA
https://youtu.be/Xtvo7Y2q4NA
https://youtu.be/m6rUsGNRYPg
https://youtu.be/KMc-B051ne8
https://youtu.be/5blSG0JZNbg
https://youtu.be/rPbzUW7usJE
https://youtu.be/cTm0AR5_O54
https://youtu.be/IjxkD8L2cOM
https://youtu.be/xVEGczCJCHs
https://youtu.be/MsIRa740mQY
https://youtu.be/TeDcLjOOiK4
https://youtu.be/dbRJFnQoKTI
https://youtu.be/0QPpgAsd4IY
https://youtu.be/RYJ3vs8qx10
https://youtu.be/_Dc1IqY2WpQ
https://youtu.be/nbQUEN3IRFk
https://youtu.be/tsQT0MT73Og
https://youtu.be/DGEqY5rLyVc
https://youtu.be/XYY2tc_8KDM
https://youtu.be/JrRoR7ycM8U
https://youtu.be/SPDhPuMsM9A
https://youtu.be/nN36SSIMn1k
ndom-pointer/ ] https://youtu.be/ctFmFbeyTVU
https://youtu.be/uBjpRedaNKQ
ray/discuss/686261/JavaC%2B%2BPython-Array-Time-O(N)-Space-O(1)
ss/411318/Java%3A-beats100-1ms-easy-understanding-solution
&list=PLKZaSt2df1gxtem7J8QqY8m2bHliz8mPt
array/discuss/495394/C%2B%2B%3A-Simplest-one-pass-two-pointers
uss/38125/Solution-in-Java
om-sorted-array/discuss/11782/Share-my-clean-C%2B%2B-code
uss/21222/A-simple-Java-solution
code.com/problems/insert-delete-getrandom-o1/discuss/85422/AC-C%2B%2B-Solution.-Unordered_map-%2B-Vector
-k/discuss/301242/General-summary-of-what-kind-of-problem-can-cannot-solved-by-Two-Pointers
d r and ignore l or r whichever is less. say r < l, there is no point of remembering r anymore as min(r,l,anything bigger that r) is a
or index is visited or corresponding letter is not present at that index. else dfs 4 directions, return true. at last remove it as visit
, upadate max left+right. Basically if you assume the array as a circle, just need to find the window with max sum between n-k
se hashmap approach to count frequencies and for every element in hashmap, if its complement exists, increment count. if k is
me mod of K.
cy of each occurence without disturbing number present there. for i=0 to l, if i's<n return i. if x<n return n-1. return n
nation-sum-palindrome-partitioning/?orderBy=most_votes
else add to dp
0 if n==0. if dp[n][k] is not null return its value. for key in keys[k], count+= call helper function with length n-1, starting with key.
n. (or) O(1): Math solution. as we can go only right or down, to reach (m,n) from (1,1), we must move m-1 times downward,
dd a[i][j] to ans.
=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA) as OA is removed twice in OB and OC.)
eady upto left, use its value else 0 and add 1. update ans
hout extra storage: just have left max, right max values. l=0, r=n-1. while(l<r) if l's<r's{ if leftmax is less update it(as r's is alread
make l as array[charat(r)]+1. after if, update res with r-l+1 and array[charat(r)]=r, r++. (or) maintain hashmap of recent index of r
ndrom mark dp[i][j] as 1. if its 1 and it is longer than current answer, make it as answer
eft i.e ')' can be added to match a previous unmatched '(', call for right+1, str+')'O
p curnum), just push curnum as curop can be /*. if prevop is */ must evaluate it.] maintain curnum,prevop. for i=0 to len-1, if i's i
[i] to answer and subtract it from n.
j-1). then remove extra spaces(i=0,j=0. while(j<n) skip spaces with j, move non space j's to i's, skip spaces, if j<n add single sp
stack not empty, insert /,popped item at 0
single space . if no words can be added simply make remaining as spaces, else if is the last line, add words seperated by sing
>20(" "), >10
d count). if count becomes 0, reduce the size of window by trimming unncessary left char, if it cant be removed break. update m
not numseen return 0, numseen=0 as num must be there after e/E, eseen=1. else if its +/-, if i!=0 or i-1's not e/E return 0. else
e num of times t[:i+1] occured in s[:j+1]=num of times t[:i] occured in s[:j]+num of times t[:i+1] has already occured in s[:j] as curr
2 with ans and carry=1. i--, j--. atlast if carry is still 1 concat it
https://www.youtube.com/watch?v=oUbZurIKnsY ,
map-%2B-Vector
anything bigger that r) is always r whose area is already have taken into account and min(r,l,anything lesser than r) will always h
e. at last remove it as visited and return false if true is not returned previously.
ve m-1 times downward, n-1 times rightward in any possible way. if m=3, n=7 solution could be R R R D R D R R. no. of permu
s update it(as r's is already high but there is no max on l's left, water cant be store so leave), else add leftmax-l's(its guaranteed
shmap of recent index of r's. if charat(r) is already present, l=max(l,r's recent index). update answer, recent index of r's.
vop. for i=0 to len-1, if i's is digit, curnum=curnum*10+i's-'0'. if its not digit and not space or i==len-1, if prevop is + just push cur
d words seperated by single space and make remaining as spaces, else find num of equally distributed extra spaces and leftove
d leftmax-l's(its guaranteed that leftmax< some height on right since its impossible to make leftmax=l's by entering the condition
d extra spaces and leftover spaces and add words accordingly. convert to string and append.
)!/m!*n! instead of finding 3 factorials, (m+n)!/m!=(m+n)*(m+n-1)*...*(m+2)*(m+1). this is numerator and denominator is n!. para
by entering the condition l's<r's ) to answer, l++} else {if rightmax is less update it, else add rightmax-r's to answer, r--}
m=0, prevop=i's. after all stack is filled add its all elements. (or) simply eliminate stack with O(1) space. u
nd denominator is n!. paralelly we can multiply and divide to get answer.