You are on page 1of 12

Today's content

Currency enchange

fractional knapsack
U Greedy Properties
4 Activity Selection
4
job scheduling
Greedy Algorithm

Buy a Phone iPhone 14

Amazon Flipkart Gma


170k age 68 K

Accept the
offer

I
GOLDA 45 CPA I co

U Creedy algo is an approach to solve problems


by
making locally optimal choices.at each step

5 10

100 2 I 3
Indian currency 1 2,5 10 20,50 100 200 500 2000
I IT It Is É
Cash 5548 min number Coins notes to
s
of get required
Cash hmultiples coins are allowed
of fame

notes coins count lefout amount


2000 2 1548
500 3 48 it always works
20 2 8
5 A 3
2 I I

I 1 1
10

I Curenty 1 amount É
212,18 thou to
Sylve
l m discussed
18 10
32
1
n so I
greedy won't
work

1 10 34
a Fractional knapsack
K
you can consume kg of vegetables you can
eat any integral amount of item Man Proteinyou canget

vegetables
Eating complete item
protein gained bit f Raton
thebasis
Protein
Tomato
20kg 200P COP
kg 20 Loop of man

onion sokg72500
Apples 180 P 1201kg 15 1808
15kg Tomato 120kg72000
Onion 50kg 250 P JP kg us P

chicken to 150P 1581kg co Nop


kg
Potato 2000 4 8Pig 8 64 P
25kg
1320 41181kg 12 1320
Mango 12kg
100 P 5 coop
Seafood 5kg 2081kg
826P

Class Pair C
int w

int P
double PPV
I Psuedo Code

int wt Ca int ProteinCa int k


fractionalknapsack

Pair 0 items new pair m

int i ice itt C


for
O

items i new Pair wtf poohing TIE

T.si Olalogal on the basis


of PPU
S C Old Arrays Soot items

int i m t i o i I
for Pair seem items i

if Coen k
w c
ans ans t rem Pj
K k se sem w
g
else Sem Ppu K
ans any A
K 20
break
I
g return ans
binarysearch
Greedy Properties greedy
40
for
min montalbobums
Based on what Parameter we want to applygreedy

either Bove it logically or discard it with one


counter example

I counter enamph
U most likely idea is incorrect
your

Break fill 8 30AM


a Activity Selection

man Count
of task you can do
Note Start a task we need to complete
At Point time single task
any of

office is
missatiissi cricket
a
ya
a creaking a shopping

Iga mining instasees


Ia
b y se g to 1 12pmInna
z s a s d n d g item

min duration task


first
U
yoga running States instasees
Hazy

ending early
yoga running Meaning Shopping Slater
Hns
S

Correctness TaekA
n tasks
ji
Task B l
I
Tff
n tasks
D
D pentosks
class Pair
IPSweds code
init
int activityselection int started int end Ca

Pair C are new Pair A

int i 0 ical itt


for
as i new pair start lid end fi
g
sort on thebasis
of end time
Arrays soot arr

T.cc OCrelogne

S C OCN
II Pick all the non overlapping tasks
Usetum ans
Job Scheduling
I Given a tasks to complete
Deadline assigned
for each task day on or before we

can do task

Payment assigned to each task


on
any given day we can Perform
only a task and
Each task take a day to finish
find man Payment we can get

FENI Parameter

Job deadlineday Payment payment


deline Figment

a 3 100 Iodine
b I 19 a 3 100
C 2 27 b t 19
C 21 27
d I 25 d 1 2 25
e 3 30 e 31 1 30

Enz I 5
Job deadlineday Payment soot the basis
on
of deadline
a 3 5
I t 2 3 3 3
deadline I
C 3 6 Payment I 3 5 6 9
d 2 3
e s g
deadline I f g
deadline 3 t

Enz Tasks I 22 3 4 5 6 77 8 9 10
dead 2 I t I 4 5 4 5 5 2

Money 200 250 200 350300 100 250 600 400 150

of deadline
Steps Sort on the basis

Tasks 2 3 4 1 10 5 7 6 8 9

dead I 1 I 2 2 4 4 5 5 5

money 250 200 350 200 150 300 250 100 600 400

min near

It 5 I 300 400
250350
600
24
250

addthem
am 1900
I PSued code

soot the data ace to deadline

minheap e int mh

Lint i ical it t
for
O

deadline fi mh sizes
if
i
T c Olalogal mh insert money

S C Ocn 3
else I
if I moneyCig smh Peek C
mh semovell
mh insert money i
g

3
U sum all the elements mh
of
a 3 100

b I 19
C 21 27

d 1 2 25

e BI 30

di
b c e d a
1 I 1 2 3

19 27 30 25 100

You might also like