Skip to content

Commit fd37001

Browse files
committed
added javascript TreeMap, possibleHotelBooking problem
1 parent 9b56d80 commit fd37001

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed

TreeMap.js

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// https://github.com/somdipdey/JavaScript-implementation-of-java.util.TreeMap-Class/blob/master/treeMap.js
2+
module.exports = class TreeMap {
3+
constructor(){
4+
this.dict = {}
5+
}
6+
7+
/*
8+
* <summary>
9+
* Value get(Object key)
10+
* This method returns the value to which the specified key is mapped,
11+
* or null if this map contains no mapping for the key.
12+
* </summary>
13+
*/
14+
get(key) {
15+
return this.dict[key];
16+
};
17+
18+
/*
19+
* <summary>
20+
* boolean containsKey(Object key)
21+
* This method returns true
22+
* if this map contains a mapping for the specified key.
23+
* </summary>
24+
*/
25+
containsKey(key) {
26+
if( this.get(key) !== undefined) {
27+
return true;
28+
} else {
29+
return false;
30+
}
31+
};
32+
33+
/*
34+
* <summary>
35+
* Value put(K key, V value)
36+
* This method associates the specified value with the specified key in this map.
37+
* </summary>
38+
*/
39+
put(key, value) {
40+
this.dict[key] = value;
41+
if(isNumber(key))
42+
{
43+
if(allKeysAreNumeral(this.dict)) {
44+
this.dict = sortOnKeys(this.dict);
45+
}
46+
}
47+
};
48+
49+
/*
50+
* <summary>
51+
* Value remove(Object key)
52+
* This method removes the mapping for this key from this TreeMap if present.
53+
* </summary>
54+
*/
55+
remove(key) {
56+
'use strict';
57+
delete this.dict[key];
58+
};
59+
60+
/*
61+
* <summary>
62+
* void clear()
63+
* This method removes all of the mappings from this map.
64+
* </summary>
65+
*/
66+
clear() {
67+
this.dict = {};
68+
};
69+
70+
71+
/*
72+
* <summary>
73+
* treeMap.foreach(V value)
74+
* This method returns each value for each keys in the TreeMap.
75+
* </summary>
76+
*/
77+
forEach(callback){
78+
var len = this.size();
79+
for (i = 0; i < len; i++) {
80+
var item = this.get( Object.keys(this.dict)[i] );
81+
callback(item);
82+
}
83+
}
84+
85+
86+
/*
87+
* <summary>
88+
* int size()
89+
* This method returns the number of key-value mappings in this map.
90+
* </summary>
91+
*/
92+
size() {
93+
return Object.keys(this.dict).length;
94+
};
95+
96+
/*
97+
* <summary>
98+
* boolean isEmpty()
99+
* This method returns a boolean
100+
* determining whether the TreeMap is empty or not.
101+
* </summary>
102+
*/
103+
isEmpty() {
104+
return Object.keys(this.dict).length == 0;
105+
};
106+
107+
108+
/*
109+
* <summary>
110+
* Key floorKey(K key)
111+
* This method returns the greatest key less than or equal
112+
* to the given key, or null if there is no such key.
113+
* </summary>
114+
*/
115+
floorKey(key) {
116+
if(!isNumber(key))
117+
throw "Invalid Operation: key has to be an integer value";
118+
119+
if(this.containsKey(key))
120+
return this.get(key);
121+
122+
return this.floorKey(key - 1);
123+
}
124+
125+
126+
/*
127+
* <summary>
128+
* Key ceilingKey(K key)
129+
* This method returns the least key greater than or equal
130+
* to the given key, or null if there is no such key.
131+
* </summary>
132+
*/
133+
ceilingKey(key) {
134+
if(!isNumber(key))
135+
throw "Invalid Operation: key has to be an integer value";
136+
137+
if(this.containsKey(key))
138+
return this.get(key);
139+
140+
return this.floorKey(key + 1);
141+
}
142+
143+
144+
/*
145+
* <summary>
146+
* Object clone()
147+
* This method returns a shallow copy of this TreeMap instance.
148+
* </summary>
149+
*/
150+
clone(){
151+
return this.dict;
152+
}
153+
154+
155+
/*
156+
* <summary>
157+
* boolean containsValue(Object value)
158+
* This method returns true if this map maps one or more keys to the specified value.
159+
* </summary>
160+
*/
161+
containsValue(value){
162+
var len = this.size();
163+
for (i = 0; i < len; i++) {
164+
var item = this.get( Object.keys(this.dict)[i] );
165+
if(value === item)
166+
return true;
167+
}
168+
169+
return false;
170+
}
171+
172+
173+
/*
174+
* <summary>
175+
* Set<K> keySet()
176+
* This method returns a Set view of the keys contained in this map.
177+
* </summary>
178+
*/
179+
keySet(){
180+
var set = [];
181+
var len = this.size();
182+
for (i = 0; i < len; i++) {
183+
set.push(Object.keys(this.dict)[i]);
184+
}
185+
186+
return set;
187+
}
188+
189+
190+
/*
191+
* <summary>
192+
* Key firstKey()
193+
* This method returns the first (lowest) key currently in this map.
194+
* </summary>
195+
*/
196+
firstKey(){
197+
return Object.keys(this.dict)[0];
198+
}
199+
200+
201+
/*
202+
* <summary>
203+
* Key lastKey()
204+
* This method returns the last (highest) key currently in this map.
205+
* </summary>
206+
*/
207+
lastKey(){
208+
var len = this.size();
209+
return Object.keys(this.dict)[len - 1];
210+
}
211+
212+
}
213+
214+
// some more functions which might be required to complete the operations of TreeMap -->
215+
216+
// Checks if the input is a number or not
217+
function isNumber( input ) {
218+
return !isNaN( input );
219+
}
220+
221+
// Sorts a JavaScript dictionary by key
222+
function sortOnKeys(dict) {
223+
224+
var sorted = [];
225+
for(var key in dict) {
226+
sorted[sorted.length] = key;
227+
}
228+
sorted.sort();
229+
230+
var tempDict = {};
231+
for(var i = 0; i < sorted.length; i++) {
232+
tempDict[sorted[i]] = dict[sorted[i]];
233+
}
234+
235+
return tempDict;
236+
}
237+
238+
// Checks if all the keys in the JavaScript dictionary are numeral.
239+
// If Yes, then it returns true or else it returns false
240+
function allKeysAreNumeral(dict) {
241+
for(var key in dict) {
242+
if(!isNumber(key))
243+
return false;
244+
}
245+
246+
return true;
247+
}

possibleHotelBooking.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const TreeMap = require('./TreeMap');
2+
3+
// A hotel manager has to process N bookings of rooms for the next season. His hotel has K rooms.
4+
// Bookings contain an arrival date and a departure date.
5+
// He wants to find out whether there are enough rooms in the hotel to satisfy the demand.
6+
// Inputs:
7+
// - First list for arrival time of booking
8+
// - Second list for departure time of booking
9+
// - Third is K which denotes the count of rooms
10+
// Output:
11+
// - A boolean which tells whether its possible to make a booking
12+
// false means there are not enough rooms for N booking
13+
// true means there are enough rooms for N booking
14+
// Example:
15+
// Inputs:
16+
// - arrivals = [1, 3, 5]
17+
// - departures = [2, 6, 10]
18+
// - K = 1
19+
20+
// Output: false. At day = 5, there are 2 guests in the hotel. But we have only one room.
21+
22+
// sorted map solution
23+
function possibleHotelBookings(arrive, depart, k) {
24+
// collection of events
25+
const events = new Map();
26+
27+
// number of rooms
28+
let num = arrive.length;
29+
30+
for (let i = 0; i< num; i++) {
31+
let arrival = arrive[i];
32+
let departure = depart[i];
33+
// add one during an arrival;
34+
let current = events.get(arrival);
35+
// events.set(arrival, current === null ? 1 : current + 1);
36+
current ? events.set(arrival, current + 1) : events.set(arrival, 1);
37+
// remove one during a departure
38+
current = events.get(departure);
39+
current ? events.set(departure, current - 1) : events.set(departure, -1)
40+
}
41+
console.log('events => ', events)
42+
43+
// sort the map
44+
const sortedEvents = new TreeMap();
45+
for (const el of events.entries()) {
46+
sortedEvents.put(el[0], el[1])
47+
}
48+
console.log('sorted events => ', sortedEvents.dict)
49+
50+
let count = 0;
51+
for (const el in sortedEvents.dict) {
52+
count += sortedEvents.dict[el]
53+
console.log('new count', count)
54+
if (count > k) return false;
55+
}
56+
return true;
57+
}
58+
// Time O(nlogn) average, O(n^2) worst | Space O(n)
59+
60+
61+
console.log(possibleHotelBookings([1,3,5], [2,6,10], 1)); // false

0 commit comments

Comments
 (0)