-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem_134.js
More file actions
42 lines (38 loc) Β· 772 Bytes
/
problem_134.js
File metadata and controls
42 lines (38 loc) Β· 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class SparseArray {
/* constructor */
constructor() {
this.sparseArray = [];
}
/**
* initialize with original large array and size
* @param {number[]} arr
* @param {number} size
*/
init(arr, size) {
const newArr = new Array(size);
arr.forEach(i => {
newArr[i] = i;
});
this.sparseArray = newArr;
}
/**
* Updates index at i with val
* @param {number} i
* @param {number} val
*/
set(i, val) {
this.sparseArray[i] = val;
}
/**
* Gets the value at index i
* @param {number} i
* @return {number}
*/
get(i) {
return this.sparseArray[i];
}
}
// const sparse = new SparseArray();
// sparse.init([1, 2, 3, 4, 5], 100);
// sparse.set(68, 10);
// console.log(sparse.get(68)); // 10