0% found this document useful (0 votes)
2 views14 pages

HashMap3

The document outlines a series of algorithms and pseudo-code for managing a randomized set, including operations for insertion, deletion, and random selection of elements, while handling duplicates and blacklists. It describes the implementation of a class called RandomizedSet, detailing methods for inserting, removing, and retrieving random elements with equal probability. Additionally, it includes a section on generating random numbers within a specified range while excluding blacklisted values.

Uploaded by

anzulburner
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)
2 views14 pages

HashMap3

The document outlines a series of algorithms and pseudo-code for managing a randomized set, including operations for insertion, deletion, and random selection of elements, while handling duplicates and blacklists. It describes the implementation of a class called RandomizedSet, detailing methods for inserting, removing, and retrieving random elements with equal probability. Additionally, it includes a section on generating random numbers within a specified range while excluding blacklisted values.

Uploaded by

anzulburner
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

Today's agenda

Insert delete get Random Old


Insert delete get Random OCD duplicates
Random Pick with Blacklist
I How to get random number in a range

Random 5 t new Randomly

onentine É dig
CE10

a LI Koo 3221 1000 11003 get random

Hideo
8 nentent Ca 1101 I 370

Il idea
0 A 2 3 4 5 150 1200
II too tooo too 1100

6 nest Int 0,204 150


a Insert delete get Random OG
implement the class
a bool insert val insert val in set not Present Return true
s
if
ifitem was not Present
b bool remove val Remove an item val
from set if Present
return tone item was Present
if

C int getRandomC returns a random element from the


current set elements each element must have the same
of
Probability

en add to add add 14 add 16 remote 12 removes


iz
grc
nestIntlolist size

Intf Integer ionin ng 14

51 list

14
16
20
3 1
Io Iz tf
Remid D
I Psuedo code

Class RandomizedSeth
Hashraape Integer Integer him
list
Arraylist Integer
Random 69
Public Randomizedset C 1
hm new Hashmap o c

boolean insert intra h


hm contains val I
if key true
returnfalse
3
on else
hm but val list sizes
list add Val
return tone
s
3
int getRandomC f
int id n o nestent list Sized
Oct return list get lion
boolean remove int val d
if
Chm Contains
keyWal false returnfalse

int idn hm get val


hm remove val
lion I list
if list size c 1 [Link] e's
Old
int idn I list sized 1
int temp list get id 2
Swap ida ida 2
list remove list Size 1

h m Pat temp idn


3 return true
3
boolean remove intra I list y 2
if keyWal false return
Camcontains false
318 3h86
20
int idn hmget ral
hmremove rae

intions listsized e 1 0 ida o


shirt atjsetcionas 20 I idn 22 2
list remove listsizes t
30 120
hmPat temp idn
returntrue temp 30
a Insert delete get Random OG duplicates allowed

add io add 12 add 14 add lo add 12 add 14 addled


onestatfolistsized a
add iz
grey gem g
remove in
Integer had
indices
Haghsets
10 1033612
list I 2 3
I 12

idn 2
I PSuedo code
Hathras C Integer Hashsetcentegers him
list
Arraylist Integer
Random 69

boolean insert int val h


Hashsetcorteges set
boolean flag

if hm contains
key Val tone 2
set hm get frat
I flag false
06 else I
set a new hash sets
true
3 flag
Set add list sized
map Put ral set
list add val

return
flag
3

int getRandomC f
int id n 6 nestent list Sized
OF setum list get lion
boolean remove int ra h
Hashset Integer set

if hm contains
key Val tone 2
set hm get frat
3
else4
return
J
false
int semi d 1

int n set I
for
semidn ng
break
3
OCD
Set remove semi dn
map put ral set

remick list size c 174


if
list remove list Sized 1

3
else I
int id22 I list size l 1
int temps list getCida2
Swap semidn id 2
list remove list sized 13
Hashset CInteges S mapget temp
so semere id 22
s add semi da
map Pat temp S
y
map sermovecrae
if mapget var size 2 0

return tone
3
add 10 add 12 add 14 add Io
boolean insert int vast
Hashsetcanteges set list
0 1 2 3
0
[Link] me
set hmgeteral 12 I 10 12 MY 10
blagfalse
Else
set newnasnsetesci
14 I
flag true
setaddleistsizes
mapPutCralset
list adderall
returnflag

boolean remove int rash Add 10 add 12 add 14 add lo


Hasnsetconteges set

[Link] [Link]
set nmget
crass

to a
752
me a
returnfalse 92
II 12 10

isemidning
c 14 I
break

set semorecremian remove Lo


mapputCraesets

ifremian listsized 121


listremovelistsizeso

[Link] ni 10 temp M
swapCremion
g
ian2
an semian
list remove listsizesa idn2 3
mar
[Link].gg1
getCteemo

I m c
[Link] sizen
[Link]

3
a Random Pick with Blacklist
Return a random Integer in the range a 17
And not in blacklist
Eon n 20 BL L 3 7 13 17 193

O Ilidead
I random in Co n and
get d
2 return only it is not Present
3
if
in Blacklist
4

5 11 idea 2
6 Add number
from Co ne 1
6
7 eneept the blacklisted numbers
8 and getRandom on inden
g
lo TC OG S C OCM
11

12
13
14

15

18
19
N 20 BL L 3 7 13 17 193

I valid number count 20 5 15


2

3
U

5 Do random in E A ra

t's
g
16 I

12
13
valid

If
716

17
19
I PSuedo code
HashraaPc Integer Integer marg
Random
og
int valid

Public solution int n into blacklist f


map 9

Cint ios is blacklistlength it t I


for map
Put blacklist i 1

valid N blacklist length


T C OCon

Cint ios is blacklistlength it t I


for
blacklist i a valid h
if
while mapContainskey a D toned a
map Put blacklist Cid NH
n 9

3
3

int Pick C f
int ral o hentant valid
map ContainskeyCrab
if tone
setigget ra
else I return rat 3
4 41 L 3 7 13 17 193

blacklistlength itt l
2,12 valid 120 5 15
forCint ios is
mapPut blacklistEi 1
3
3 u 3 3 418
int valid n blacklistlength 5
7 2 116
blacklistlength it 71 13 7 1 95
forCint ios is i valid 177 1
blacklist
if while 4 g
stone In
is 3 1
map key d
Contains a 53
mapPut blacklistCid n D
9
12
n 9 13
14

s
3
Y

You might also like