Most Asked Leetcode
Most Asked Leetcode
-
Ayush Kumar
Array
-
Colors (3
Sort .
4 M)
Two Pass
an pass
(3 m)
Frequent Elements
-of 5
.
Method -
I Mah , Min Heah
-
TC :
o Inlogk)
insertion and
logk for
deletion in Heal and
, we are
doing it
n times
Sc : 0(n + 1)
TC : O(n)
SC : 0 (n)
frequency
:
Method Greedy approach
TC : 0 (n)
SC : O(n)
hod2
Dynamic Programming
TC : 0(n2)
SC : o (n)
Struct
Pascals'
-
Triangl (2 3m)
.
Iterative
-
TC : OllurmRows : (
SC : O ((nrmRows(2)
Recursi
-
TC :
same as abou
SC :
struir
-
permitation (3
Next .
5 m)
Baut
Ford
permutations
and storing
-
Generating all
them in a list
antimised
-
TC :
O(n)
Sc : -(i)
steuris
M
(3m)
Find
the Duplicate Number
TC :
O(n)
SC :
0(1)
Majority
~
Element II
Recursion
-
+ n)
TC : 0 (2m
3 : 0(m + n)
Ap
TC : O(man)
SC : 0 (man)
Antimised
DP
TC : O(m n) .
Sc : 0(n)
of current
row
The value
Pers
Dairs
Brute
Forc
TC : 0 (n2)
Sc : O(1]
TC
O(nlogn'
:
Sc :
o(n)
Window
-
stries
-
withdifferent integers
Salarray
nice
Some
~Number - t
subarray
-larray
sum equals <(3m)
-
Strings
struir
TC : O(n)
SC :
OC11
Harp Algorithm
Ralun
Text : a a aaab
We have to find pattern in
text
b
pattern :
-
a a
hashfunchon hashcod
--
#2 = 4 -
>
-
make a hash
function and use it on pattern and then check
hashcodes text window that siz
in
for of
7 7
- M
:
Drawbacks Text : Ccacca aed ba
- -
Patterni
L
db a
-
4 + 2+ 1 =
7
-
Here hashcodes are
matching , despite having different strings
,
I
↓
without
a
&
Shurious hits
->
Here we are
using 10 as lease
,
we hav to us 26 Des
to accommodate all characters j - 18
- In
sliding window of complex hash
function
Cc a C Ca aed b a
3 x
: 18 + 3 x10 + 1x10
.
(3x18 + 3.
x10 + 1x100 -
3x102] + 10 + 3x182
Struer
-
Repeated string
-
watching
-mem
- We are
using a recursive approach
and calculating count of say
>
- We
reach end
are OR
using because even
if
w
need to
of string
one , we
ned
Minumber
nimum characters that a
to b adde
>
-
Then check rest
culstring from i to n-2 - (ans) whose
mismatch palindrome
&
Method
,
Brutt Ford
reasing
Method Monotonic
TC : 0 (n + m)
Sc :
0(n + m)
Strier
/660K)
ext
Greater Element It
7: O(a)
0(n)
Doing Sc :
iteration
2 times
Method
- Brutt Forc
TC : 0(n'
Sc
:
O()
element
usser
previous element
Ment less
PLE
-
TC : 0 In
Sc : 0 (n)
is not
This
approach
imp
F mat
Rainwater
repping Problem (150)
Gnien n non
negative
integers ,
we have to
-
367891011
TC : O(n)
S
Sc : or
(2 1m)
gest Rectangle in Histogram .
TC : 0 (n2)
Sc : o (n)
stack
Method -2
Menton's
(ith)
-
For man are
formed by a bar in which this bar is shortest
we need to
find bear smaller than ith on bath sides
"TIIIII
war smaller Than it it h
-
war smaller than
on
left sid ith on right
TCi O(N)
Sc : O(N)
J -team
Strik
r
Bars
updating length of
35 Unl
at Each
Reclangh
I
- largest
in Histogram
TC : 0 Crows Xcols)
Sc :
0 (cols)
struir
Jean Jeon)
>
- 0(1)
- 8/17
Oll
>
-
O(1)
>
- 0(17
O(i)
-struir
Queues using stacks
element
>
- 0(17
>
- Amortized O(I
almost
moved blw stacks one
Each Element is
>
- Amortized 0(1)
- O(17
struir
r
Cach
Binary Search
-
Stuier
-
even
>
-
If uniquel element is
found eufors
,
the ordering would be disrupted
>
-
upper half
-
bour half
man min
,
elements
Strnier
agressiv
Cous TC : 0 (N log(mas-min)(
SC : 0 (1)
-Binary
Search Frees
stair
-
>
-
Recursive
-
Iteratuit
-
TC :
0 (n)
Sc :
0(n)
strines
TC : O(n)
Sc :
Ollogn'
Predecessor and
-
TC : OIn Y
Sc :
(1)
Struir
-
kth largest Element in a BST
-
>
- Using Share O(n)
Without space
Strive
Two Sum It
-
Zorder
↓
-
Greedy
-
Struir
N
meetings in poom
a
We
always select
motings that end
↓
first
so wil
>
- sort meetings by end time
number
>
- so we can maximis
of
non overlapping intervals
TC :
0 Inlogn'
Sc :
0 (n)
-Strives
Minimum
~
Platforms
mu
TC :
0 Inlogn)
SC : (i)
Struer
-
Tob Problem
Sequencing
&
TC :
O(nlogn)
Sc
O(d)
:
mas Deadline
struir
-
Cookies
sign
Taskheduler(1-1M)
uniquel
No of tasks
↑
TCi O(TlogT)
Sc : O(T)
struir
r
sorted lists (3 9 m
Merge
-
linked lists
-
Recursiv
TC : O(u)
Sc :
0 (n(k)
recursion stack
Iteratu
-
Add two Numbers (11 M)
-
Iteratuit
-
Tc : 0 (m + n)
Sc : Oil
2 Sorted List (6 8 M)
r
.
Iteratuit
-
TC : 0(m + n)
Sc :
0 (m + n)
Struer
linked list 11 8m
let
Mode in a .
TC : OGI
Sc : ocil
TC : 0 (n)
Sc : (1)
struir
↑
Palindrome Linked List (3 8 m) .
or
s >
- o
TC :
Olm)
Sc :
(i)
stru
&
Intersection Two Linked Lists (2 SM)
-
of .
-
-
n
rut
Ford a a
using other
& switchedto aching
mous over (m + n)
-> Each pointer
nodes intersection point
If
- ·
is there ,
both will intersect
n) nodes
after (m
+
struir
↑
list Cycle It (2 6 M)
Linked
.
timed
runMan
-Struer
LFU Cacht
-
DLL
=
minfreg stares
minimum Freg
tell this point
Maths
-
+C : 0 (nm)
Sc : O(1)
Method-2 Sie
of Eratosthenes
TC : 0 In loglogn)
Sc : 0(n)
-
Backtracking
Subsets (2 6 M)
.
T generating
for subset
one
no of subset
↑
s
TCI 0 (n . 2n)
SC : 0 In 2)
Binary
-
Frees
Calculate Height of
ximum Depth of Binary Cre or -
~
Binary
-
Cre
Tree ·
Calculate its max
and add
I to it
TC :
OINI
SC :
ON
des to recursive
stack
Transform
um I
Q-1 in a
linary tree we have to replac each node valed with
,
sum
of its left sub trel and right sub tree
We are
given void function but we ,
int .
, so we can
apply recursion
while returningcum
of itself and sub tree
Leu Arder Transversal
-
Return 2 -
D Vector with nod
each
I- for storing
TC : ON
greached
real
SC :
O(N)
I' end of
~
View Free
of Binary Trel
win
left
Transversal
level order
Perform
>
- a
TC : JIN]
SC : 0 (N
t
view
~
of Zel
T
change
Diameter
Binary zrel
xiter- length
- of longest path
between two nodes in trel
any a
>
-
Function updates diameter and
returns height
TC : OIN)
SC :
o (N)
&
ans-I
mirror
tree
from given Binary Tre
Enorder versal
of
Struct
Binary TC : O(N)
Sc : 0 (H)
Recursive Iterative
↓ ↓
↑
Stri
ansversal of Binary Ire
Border
TC :
O(n)
SC : O(n)
stews
T
cransversal of Binary
Poster
Irel
e
stru
can w
of Binary Tres
TC :
O (N)
SC :
OIN)
strust
&
of Binary in
Bottom Irel
perf
No
condition
=
Serve
r
Transversal
Zig Zag of Binary Fre
mem
or Balanced Cre
Check
Diagional
mansversalof Binary Tre
>
-
This code is
modified version of level
order transversal where instead
of
visiting all children nodes ,
we
only
insit right child and enqueing the
child
.
left
-answersal
Boundary of Binary Free
TC :
O(N)
SC : OIN 3
-
under
thiswill come
leaf nodl
-
Graphs
Flood Fill ( .
SM)
-
matrix
image (sr](sC]
>
-
We can
only mow on color same as
[clar path]
-
rows
↓ A
rows
If pin color is not sam as color path
on this color
struir
T
struir
T
BFS
-
TC :
O(V+ E)
Election
in Undirected Graph
O ③
② ⑪ ⑥
-
while performing DFs
visited nod
if we come across a
it means ,
a
cycle is present
.
① [No Cell]
We I to
go from 2
- So , is visited ,
when we
neck
for adjacent visited nodes
visited
find
.
we I is In order to
parent node
along with starting
verten in
function
boops to avoid
for
>
-
we are so
using ,
components
edg case
of unconnected
directed acyelis graph
only possible for
-
Topological Sort I see
coding Ninjas notes
for Theory
-
Topological sart is linear
>
-
in the
u comes
before
ordering
TC :
0 (V+ E)
DFS Approach
-
>
-
We use stack and push starting node at last after completing
dfs ,
because me want
starting node at
first in nector
Dictionary(imp)
fiel adjacency
Tatrin anda
indegr
and
I
zeking rings
ensecutin
-
nathwal a
als
M shortest
Floyd
remem
Warshall
algorithm
I
② I
>
-
If we want to
find shortest distand I'm
27 3
-
O and I
,
we go through every
verten and
6 distanc
⑧ calculate min
>
D
17
Y
blu noc
denotes shortest distand
Ip (i) (j] -
i and noc;
from
k tes
-
If
pathexists
>
- MirHeap will le used
to start distan
from
and node
su
itself
e
en
&Min
Irelaning
struir
T
(1 MS
Matiss 2
.
TC : 0 (m n)
.
Sc : 0 (m n) .
>
- coloring a
graph with 2 nodes such
TC : 0(u + E)
Scio(v)