diff --git a/solution/0000-0099/0045.Jump Game II/Solution.js b/solution/0000-0099/0045.Jump Game II/Solution.js new file mode 100644 index 0000000000000..5ab291e61126c --- /dev/null +++ b/solution/0000-0099/0045.Jump Game II/Solution.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var jump = function(nums) { + let jumps = 0; + let currentEnd = 0; + let farthest = 0; + + for (let i = 0; i < nums.length - 1; i++) { + farthest = Math.max(farthest, i + nums[i]); + + if (i === currentEnd) { + jumps++; + currentEnd = farthest; + } + } + + return jumps; +}; diff --git a/solution/0100-0199/0119.Pascal's Triangle II/Solution.js b/solution/0100-0199/0119.Pascal's Triangle II/Solution.js new file mode 100644 index 0000000000000..079c79ffea45a --- /dev/null +++ b/solution/0100-0199/0119.Pascal's Triangle II/Solution.js @@ -0,0 +1,14 @@ +/** + * @param {number} rowIndex + * @return {number[]} + */ +var getRow = function(rowIndex) { + const f = Array(rowIndex + 1).fill(1); + for (let i = 2; i < rowIndex + 1; ++i) { + for (let j = i - 1; j > 0; --j) { + f[j] += f[j - 1]; + } + } + return f; + +}; diff --git a/solution/0200-0299/0274.H-Index/Solution.js b/solution/0200-0299/0274.H-Index/Solution.js new file mode 100644 index 0000000000000..cae05d6579048 --- /dev/null +++ b/solution/0200-0299/0274.H-Index/Solution.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} citations + * @return {number} + */ +var hIndex = function(citations) { + citations.sort((a, b) => b - a); + for (let h = citations.length; h; --h) { + if (citations[h - 1] >= h) { + return h; + } + } + return 0; +}; diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js new file mode 100644 index 0000000000000..ac34f7a9b19a9 --- /dev/null +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js @@ -0,0 +1,48 @@ +var RandomizedSet = function() { + this.d = new Map(); + this.q = []; +}; + +/** + * @param {number} val + * @return {boolean} + */ +RandomizedSet.prototype.insert = function(val) { + if (this.d.has(val)) { + return false; + } + this.d.set(val, this.q.length); + this.q.push(val); + return true; +}; + +/** + * @param {number} val + * @return {boolean} + */ +RandomizedSet.prototype.remove = function(val) { + if (!this.d.has(val)) { + return false; + } + const i = this.d.get(val); + this.d.set(this.q[this.q.length - 1], i); + this.q[i] = this.q[this.q.length - 1]; + this.q.pop(); + this.d.delete(val); + return true; +}; + +/** + * @return {number} + */ +RandomizedSet.prototype.getRandom = function() { + return this.q[Math.floor(Math.random() * this.q.length)]; +}; + +/** + * Your RandomizedSet object will be instantiated and called as such: + * var obj = new RandomizedSet() + * var param_1 = obj.insert(val) + * var param_2 = obj.remove(val) + * var param_3 = obj.getRandom() + */ diff --git a/solution/0400-0499/0432.All O`one Data Structure/Solution.js b/solution/0400-0499/0432.All O`one Data Structure/Solution.js new file mode 100644 index 0000000000000..fb0c68c88e683 --- /dev/null +++ b/solution/0400-0499/0432.All O`one Data Structure/Solution.js @@ -0,0 +1,98 @@ +class Node { + constructor(key = '', cnt = 0) { + this.prev = null; + this.next = null; + this.cnt = cnt; + this.keys = new Set([key]); + } + + insert(node) { + node.prev = this; + node.next = this.next; + this.next.prev = node; + this.next = node; + return node; + } + + remove() { + this.prev.next = this.next; + this.next.prev = this.prev; + } +} + +var AllOne = function() { + this.root = new Node(); + this.root.next = this.root; + this.root.prev = this.root; + this.nodes = {}; +}; + +AllOne.prototype.inc = function(key) { + const root = this.root; + const nodes = this.nodes; + + if (!nodes[key]) { + if (root.next === root || root.next.cnt > 1) { + nodes[key] = root.insert(new Node(key, 1)); + } else { + root.next.keys.add(key); + nodes[key] = root.next; + } + } else { + const curr = nodes[key]; + const next = curr.next; + + if (next === root || next.cnt > curr.cnt + 1) { + nodes[key] = curr.insert(new Node(key, curr.cnt + 1)); + } else { + next.keys.add(key); + nodes[key] = next; + } + + curr.keys.delete(key); + if (curr.keys.size === 0) { + curr.remove(); + } + } +}; + +AllOne.prototype.dec = function(key) { + const root = this.root; + const nodes = this.nodes; + const curr = nodes[key]; + + if (curr.cnt === 1) { + delete nodes[key]; + } else { + const prev = curr.prev; + + if (prev === root || prev.cnt < curr.cnt - 1) { + nodes[key] = prev.insert(new Node(key, curr.cnt - 1)); + } else { + prev.keys.add(key); + nodes[key] = prev; + } + } + + curr.keys.delete(key); + if (curr.keys.size === 0) { + curr.remove(); + } +}; + +AllOne.prototype.getMaxKey = function() { + return this.root.prev === this.root ? '' : Array.from(this.root.prev.keys)[0]; +}; + +AllOne.prototype.getMinKey = function() { + return this.root.next === this.root ? '' : Array.from(this.root.next.keys)[0]; +}; + +/** + * Your AllOne object will be instantiated and called as such: + * var obj = new AllOne() + * obj.inc(key) + * obj.dec(key) + * var param_3 = obj.getMaxKey() + * var param_4 = obj.getMinKey() + */ diff --git a/solution/0600-0699/0641.Design Circular Deque/Solution.js b/solution/0600-0699/0641.Design Circular Deque/Solution.js new file mode 100644 index 0000000000000..1e8d146872040 --- /dev/null +++ b/solution/0600-0699/0641.Design Circular Deque/Solution.js @@ -0,0 +1,97 @@ +/** + * @param {number} k + */ +var MyCircularDeque = function(k) { + this.size = k; + this.deque = new Array(k); + this.front = 0; + this.rear = 0; + this.count = 0; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertFront = function(value) { + if (this.isFull()) return false; + this.front = (this.front - 1 + this.size) % this.size; + this.deque[this.front] = value; + this.count++; + return true; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertLast = function(value) { + if (this.isFull()) return false; + this.deque[this.rear] = value; + this.rear = (this.rear + 1) % this.size; + this.count++; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteFront = function() { + if (this.isEmpty()) return false; + this.front = (this.front + 1) % this.size; + this.count--; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteLast = function() { + if (this.isEmpty()) return false; + this.rear = (this.rear - 1 + this.size) % this.size; + this.count--; + return true; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getFront = function() { + if (this.isEmpty()) return -1; + return this.deque[this.front]; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getRear = function() { + if (this.isEmpty()) return -1; + return this.deque[(this.rear - 1 + this.size) % this.size]; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isEmpty = function() { + return this.count === 0; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isFull = function() { + return this.count === this.size; +}; + +/** + * Your MyCircularDeque object will be instantiated and called as such: + * var obj = new MyCircularDeque(k) + * var param_1 = obj.insertFront(value) + * var param_2 = obj.insertLast(value) + * var param_3 = obj.deleteFront() + * var param_4 = obj.deleteLast() + * var param_5 = obj.getFront() + * var param_6 = obj.getRear() + * var param_7 = obj.isEmpty() + * var param_8 = obj.isFull() + */ diff --git a/solution/0700-0799/0731.My Calendar II/README.md b/solution/0700-0799/0731.My Calendar II/README.md index 1971961290950..04d8becf7d485 100644 --- a/solution/0700-0799/0731.My Calendar II/README.md +++ b/solution/0700-0799/0731.My Calendar II/README.md @@ -208,6 +208,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function (start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ +``` + diff --git a/solution/0700-0799/0731.My Calendar II/README_EN.md b/solution/0700-0799/0731.My Calendar II/README_EN.md index 5d3abb9bcf791..b3f664980ff95 100644 --- a/solution/0700-0799/0731.My Calendar II/README_EN.md +++ b/solution/0700-0799/0731.My Calendar II/README_EN.md @@ -206,6 +206,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function (start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ +``` + diff --git a/solution/0700-0799/0731.My Calendar II/Solution.js b/solution/0700-0799/0731.My Calendar II/Solution.js new file mode 100644 index 0000000000000..53cfad370e6f7 --- /dev/null +++ b/solution/0700-0799/0731.My Calendar II/Solution.js @@ -0,0 +1,32 @@ +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function (start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ diff --git a/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js new file mode 100644 index 0000000000000..87e3963d3df46 --- /dev/null +++ b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js @@ -0,0 +1,52 @@ +/** + * @param {number} maxSize + */ +var CustomStack = function(maxSize) { + this.stk = Array(maxSize).fill(0); + this.add = Array(maxSize).fill(0); + this.i = 0; +}; + +/** + * @param {number} x + * @return {void} + */ +CustomStack.prototype.push = function(x) { + if (this.i < this.stk.length) { + this.stk[this.i++] = x; + } +}; + +/** + * @return {number} + */ +CustomStack.prototype.pop = function() { + if (this.i <= 0) { + return -1; + } + const ans = this.stk[--this.i] + this.add[this.i]; + if (this.i > 0) { + this.add[this.i - 1] += this.add[this.i]; + } + this.add[this.i] = 0; + return ans; +}; + +/** + * @param {number} k + * @param {number} val + * @return {void} + */ +CustomStack.prototype.increment = function(k, val) { + if (this.i > 0) { + this.add[Math.min(this.i, k) - 1] += val; + } +}; + +/** + * Your CustomStack object will be instantiated and called as such: + * var obj = new CustomStack(maxSize) + * obj.push(x) + * var param_2 = obj.pop() + * obj.increment(k,val) + */ diff --git a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js new file mode 100644 index 0000000000000..7ebb3a8d91a15 --- /dev/null +++ b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} arr + * @return {boolean} + */ +var canMakeArithmeticProgression = function(arr) { + arr.sort((a,b)=>a-b) + + let val = arr[1] - arr [0] + + for(let i = 1 ; i < arr.length-1 ; i++){ + if(arr[i+1]-arr[i] !== val){ + return false + } + } + + return true +}; diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js new file mode 100644 index 0000000000000..02031e84633c6 --- /dev/null +++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js @@ -0,0 +1,101 @@ +/** + * @param {number[][]} times + * @param {number} targetFriend + * @return {number} + */ +var smallestChair = function(times, targetFriend) { + const n = times.length; + + const availableChairs = new MinHeap(); + + const busy = new MinHeap((a, b) => a[0] - b[0]); + + for (let i = 0; i < n; ++i) { + times[i].push(i); + availableChairs.push(i); + } + + times.sort((a, b) => a[0] - b[0]); + + for (let t of times) { + const arrival = t[0], leaving = t[1], friendIndex = t[2]; + + while (!busy.isEmpty() && busy.peek()[0] <= arrival) { + availableChairs.push(busy.pop()[1]); + } + + const chair = availableChairs.pop(); + + if (friendIndex === targetFriend) { + return chair; + } + + busy.push([leaving, chair]); + } + + return -1; +}; + +class MinHeap { + constructor(compare = (a, b) => a - b) { + this.heap = []; + this.compare = compare; + } + + push(val) { + this.heap.push(val); + this.bubbleUp(this.heap.length - 1); + } + + pop() { + const top = this.heap[0]; + const bottom = this.heap.pop(); + if (this.heap.length > 0) { + this.heap[0] = bottom; + this.bubbleDown(0); + } + return top; + } + + peek() { + return this.heap[0]; + } + + isEmpty() { + return this.heap.length === 0; + } + + bubbleUp(index) { + while (index > 0) { + const parentIndex = Math.floor((index - 1) / 2); + if (this.compare(this.heap[index], this.heap[parentIndex]) < 0) { + [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]]; + index = parentIndex; + } else { + break; + } + } + } + + bubbleDown(index) { + const length = this.heap.length; + while (true) { + const leftIndex = 2 * index + 1; + const rightIndex = 2 * index + 2; + let smallest = index; + + if (leftIndex < length && this.compare(this.heap[leftIndex], this.heap[smallest]) < 0) { + smallest = leftIndex; + } + if (rightIndex < length && this.compare(this.heap[rightIndex], this.heap[smallest]) < 0) { + smallest = rightIndex; + } + if (smallest !== index) { + [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]]; + index = smallest; + } else { + break; + } + } + } +}