0% found this document useful (0 votes)
3 views29 pages

Problem Solving 1 - Greedy Problem Solving - Level 3 - TLE

very good

Uploaded by

Anshu Yadav
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)
3 views29 pages

Problem Solving 1 - Greedy Problem Solving - Level 3 - TLE

very good

Uploaded by

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

Greedy Problem Solving 2

- Gaurish Baliga
Problem 1: Duff and Weight Lifting
Problem Link: [Link]

-
Problem I
2: Duff and Weight Lifting
= 109

-
-

-
Obs 1 :
Weights are powers
of two

E2 should be a
&

of two
uES power
S o S

10 + a

↑ 1 To
22-
E
E is

power
a

of

two
smallest power
occurs atteast twice
moves
>
-
Min
- possible

D ↑
2

O(nlogn)
·
23
Some observations:
1. We want to club up as many numbers together as possible such that their
sum is a power of two.
2. Consider the smallest number (2^a) in a sequence 2^a, 2^b, 2^c.
&

3. We can claim that 2^a should occur atleast one more time if it is part of a
-
larger sequence.
4. Why? Because 2^a can be only clubbed with 2^a to make a power of 2.
Solution
1. Note down the frequency of each power of two.
-

2. Start from the smallest power of 2 and iterate towards the largest.
3. If you have x elements = 2^i, then club most of them to form floor(x / 2)
-
=>

elements = 2^(i + 1) and you might and might not have a remainder left.
-

4. If remainder is left (x % 2 == 1) that means that this element needs to be


handled separately.
41 - 222
s
,
5 -
22s

11

Pq
-
11233
behind

-
left

21222324
2
answer +t ; -
-
112 33 ↓ ↓
D
-
Po

7
I
D810
Dolo

%0 3
.
10 &
Implementation

-
carry
3
Problem 2: Polycarp at the Radio son -
!

Problem Link: [Link]


Problem 2: Polycarp at the Radio
3

D
2
.
Using a
pg
of frequencies
imple complicated
mak min freq.

x- 1
D
-
x+ 1

X
v ~ x

-asse
z

BinarySearch
- ⑬
eleme z

2
10 20
- ~ -
↑ X

3
O
m
x2
=
=
v23s
-
-

m taken
>
# Elegant Obs .

trecht

I
1-m
!
X #
-
Some observations:
1. Trivial Observation: Binary Search on Answer is possible
2. Non-trivial observation: Each element from 1 to m can occur atleast
floor(n / m) times.
Solution
1. The min max frequency is just n / m.
2. Now the main task is to implement making changes in the final array.
3. Make a note of the elements that can be changed.
4. These include elements > m and elements whose frequency is > n / m.
5. Now we just need to change these elements into the elements we want.
Implementation

-
Problem 3: -od
K-Complete Word
!
Problem Link: [Link]
#
Pen &
string K-complete
B
paper
k =

alindrome
P F =

- #j
z

m -
g & Y W
-
-
2
-----
~
-

S-
- -

Wi
A
-
-
A
~
I 7

I
5-3
2
Frea
,
For each chain
-
(size of chain) -
Car free

m
I
-
211 -

2
3 =

S2-freq s
=

,
Problem 3: K-Complete Word
Some observations:
1. S[0] = s[n - 1]
2. But s[0] = s[k - 1]
3. This means that every period should also be a palindrome.
4. Also all periods should be the same, i.e. s[0..k-1] = s[k..2k - 1]
Solution
1. For every pair of elements that must be same in a period, note down the
frequency of elements over all periods.
2. Now since you want to make minimum changes, you make all elements
in this pairing = element with max frequency.
3. Cost = number of elements in this pairing - max frequency
Implementation
-

task [ ↓
mak free
--

som

You might also like