0% found this document useful (0 votes)
12 views

Most Asked Leetcode

The document outlines various algorithms and methods for solving different computational problems, including sorting, searching, and data structure manipulation. It provides time and space complexity analysis for each method, such as Bucket Sort, Quickselect, and Greedy approaches. Additionally, it covers specific problems like finding duplicates, generating permutations, and managing linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Most Asked Leetcode

The document outlines various algorithms and methods for solving different computational problems, including sorting, searching, and data structure manipulation. It provides time and space complexity analysis for each method, such as Bucket Sort, Quickselect, and Greedy approaches. Additionally, it covers specific problems like finding duplicates, generating permutations, and managing linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Letrade Most Asked

-
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)

Milhod-2 Bucket Sort


-

TC : O(n)

SC : 0 (n)

- Each bucket has elements with same

frequency

Method 3 Quickselect Approach


(5 2 m)
Zumb
Game .

:
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

There are n ! permutations


and each
TC :
0 (R !. n) generating
takes oIn' time
SC :
0 (n n)
·

antimised
-

TC :
O(n)
Sc : -(i)
steuris
M

(3m)
Find
the Duplicate Number

TC :
O(n)
SC :
0(1)

Majority
~
Element II

Boyer-Moore Voting Algorithm (extended)


-struir
Unique Deiths (3r)
-

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

depends on previous row


combinatorial
Approach
Striver

Pers
Dairs

Brute
Forc
TC : 0 (n2)
Sc : O(1]

Merge Sort and Count

TC
O(nlogn'
:

Sc :
o(n)

If any element right subarray


-
in

which is less than element


of left
Cularray ,
then rem part of cubarray
also
form reverse pairs
Sliding
-

Window
-
stries
-

withdifferent integers
Salarray
nice
Some

~Number - t
subarray

-larray
sum equals <(3m)
-
Strings
struir

String to Integer (atoi) (9 2M)


.

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
,

This is known as spurious hits

With spurious hits


1)
>
-
TC :
0 (n -m +
TC :
0 (mm)

I

without
a
&

Shurious hits

- We need to form more complex hash functions ,


db a 10
fat
4x102 + 2x(6' + 1x100 =
421

->
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

abcd + abcd + abcd


# II
We need to check one more time if clabadab clabedab
Strvir
-
Count and Problem (1 7 M
Say
.

-mem

- We are
using a recursive approach
and calculating count of say

of n using n .. and adding


characters to our new
stering
according to n-
string
striv
-
Version Numbers (1 M)
-mpare .
2

>
- We

reach end
are OR
using because even
if
w

need to
of string
one , we

check other string


strie
&

ned
Minumber
nimum characters that a
to b adde

We make pointers start and end and check


for if
>
- 2
,

palindrom rists not mismatel


or
if any
We increase count characters to be added
of
-

>
-
Then check rest
culstring from i to n-2 - (ans) whose
mismatch palindrome
&

value and is checked


-
decreases
after
again
Stack
-
-
t Creater Element I (1 .
,m)
~

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

Strur Next Smaller Element


-
Sheet
struc
S

m of Subarray Minimums (7181)


-

Method
- Brutt Forc

TC : 0(n'
Sc
:
O()

element
usser
previous element
Ment less
PLE
-

thed-2 Using concept of and NLE

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

how much water


find
can it tran

-
367891011

TC : O(n)
S
Sc : or
(2 1m)
gest Rectangle in Histogram .

Method -I Brute Forc


-

TC : 0 (n2)
Sc : o (n)

Area possible between


>
- Calculating
every 2 bars

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

Maximal Rectangle (960k)


-

Bars
updating length of
35 Unl
at Each

Reclangh

I
- largest
in Histogram

TC : 0 Crows Xcols)
Sc :
0 (cols)
struir

Implement Stack Using Queues (1 ,M)


-

-> Rotate all elements after


Using Two Queeres Using
I Guess
pushing

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
-

Single Element in Sorted Array (1 2 m)


.

elements always start at


- The pair of
indices
.

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
-

>
-

Binary Search Free from Dreorder Traversal


Construct
-

Recursive
-
Iteratuit
-

TC :
0 (n)
Sc :
0(n)
strines

Convert Sorted Array to BST


-

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

Cuo Pointes) O(n)


TCi
O(n)
-Sc :


-
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

Mithod- , Brutt Forc


-

For each train check it overlaps with how


-
,
many and select
the mar TC :
0 (n 2)

Method-2 Greedy (Two pointer

TC :
0 Inlogn)
SC : (i)
Struer
-
Tob Problem
Sequencing
&

TC :
O(nlogn)
Sc
O(d)
:

mas Deadline
struir
-
Cookies
sign

Two Pointer Approach


Priority Queu
-

Taskheduler(1-1M)

uniquel
No of tasks

TCi O(TlogT)
Sc : O(T)
struir
r
sorted lists (3 9 m
Merge
-
linked lists
-

use Nodes in Groups (1 .


6 M)
~

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

Cycle in Linked list (6 2m)


elect .

TC : 0 (n)
Sc : (1)
struir

Palindrome Linked List (3 8 m) .

Using Vector (n) spac

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
-

Count Primes (2 7m)


.

Method- , Brute Fore

+C : 0 (nm)
Sc : O(1)

Method-2 Sie
of Eratosthenes

TC : 0 In loglogn)
Sc : 0(n)
-
Backtracking
Subsets (2 6 M)
.

generate all possible subsets


Using Backtracking ,

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

① calculate More Depth of Binary Tre


-

We will let recursion


handle apith for left
sub trel and
Right sub

Tree ·
Calculate its max

and add
I to it

Height : Maximum depth of


neds th
any laf from
root node .

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 ,

need helper function thatan return


a

int .
, so we can
apply recursion

So Each nod returns its value and


sum
of
both sub trees but it changes its own

value to just sum


of sub trees
I Each
itself
(
noc is
making change in

while returningcum
of itself and sub tree
Leu Arder Transversal
-

Return 2 -
D Vector with nod

data at each luel

each
I- for storing
TC : ON

greached
real
SC :
O(N)

I' end of

~
View Free
of Binary Trel

when we see tree


from left sides ,
then

all Nodes seen are part of left wein

win
left

Transversal
level order
Perform
>
- a

and tabl each level


starting nod
of

TC : JIN]
SC : 0 (N
t
view
~
of Zel

same view but here take


as
left ,
we

last node at each level

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)
-

> similar to island problem do afs


of
no we on
,

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

we need to send color path ,


as we can
only mou

on this color
struir
T

S TC : O/V + E) where v is number


of witices and
E is number
of edges

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
.

we need to take are


of
>
-
on

case : That we parent


don't take

nock as it is already visited

① [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

prevent this , we will also take

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
>
-

BFS (Kahn's Algorithm) ordering of vertices such that


unitie
for every directed edge usv

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)

- We will use Topological Sorting


will compare characters dictionary
W
of consecutive words in
>
-

until they are


different then character in
first word comes

first as compared to character in second word in


alien
dictionary order

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

TC 0(V3] where v is number vertices


of
:
distand from on
shortest
finding nodes
jkstra Algorithm nodes to all
~

>
- MirHeap will le used
to start distan
from
and node
su
itself

e
en
&Min

Irelaning
struir
T

(1 MS
Matiss 2
.

Multi sour BFS


>
-

TC : 0 (m n)
.

Sc : 0 (m n) .

>
- coloring a
graph with 2 nodes such

rath Bipartite (1 2M) .

that no two adjacent nodes have the


same color

TC : 0(u + E)

Scio(v)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy