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/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/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 +};