diff --git a/2601-2700/2684. Maximum Number of Moves in a Grid.md b/2601-2700/2684. Maximum Number of Moves in a Grid.md new file mode 100644 index 0000000..a6cc478 --- /dev/null +++ b/2601-2700/2684. Maximum Number of Moves in a Grid.md @@ -0,0 +1,103 @@ +# 2684. Maximum Number of Moves in a Grid + +- Difficulty: Medium. +- Related Topics: Array, Dynamic Programming, Matrix. +- Similar Questions: . + +## Problem + +You are given a **0-indexed** `m x n` matrix `grid` consisting of **positive** integers. + +You can start at **any** cell in the first column of the matrix, and traverse the grid in the following way: + + + +- From a cell `(row, col)`, you can move to any of the cells: `(row - 1, col + 1)`, `(row, col + 1)` and `(row + 1, col + 1)` such that the value of the cell you move to, should be **strictly** bigger than the value of the current cell. + + +Return **the **maximum** number of **moves** that you can perform.** + +  +Example 1: + +![](https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png) + +``` +Input: grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]] +Output: 3 +Explanation: We can start at the cell (0, 0) and make the following moves: +- (0, 0) -> (0, 1). +- (0, 1) -> (1, 2). +- (1, 2) -> (2, 3). +It can be shown that it is the maximum number of moves that can be made. +``` + +Example 2: + +``` + +Input: grid = [[3,2,4],[2,1,9],[1,1,7]] +Output: 0 +Explanation: Starting from any cell in the first column we cannot perform any moves. +``` + +  +**Constraints:** + + + +- `m == grid.length` + +- `n == grid[i].length` + +- `2 <= m, n <= 1000` + +- `4 <= m * n <= 105` + +- `1 <= grid[i][j] <= 106` + + + +## Solution + +```javascript +/** + * @param {number[][]} grid + * @return {number} + */ +var maxMoves = function(grid) { + var dp = Array(grid.length).fill(0).map(() => Array(grid[0].length)); + var max = 0; + for (var i = 0; i < grid.length; i++) { + max = Math.max(max, helper(grid, i, 0, dp)); + } + return max; +}; + +var helper = function(grid, i, j, dp) { + if (dp[i][j] !== undefined) return dp[i][j]; + var max = 0; + if (j < grid[0].length - 1) { + if (i > 0 && grid[i - 1][j + 1] > grid[i][j]) { + max = Math.max(max, 1 + helper(grid, i - 1, j + 1, dp)); + } + if (grid[i][j + 1] > grid[i][j]) { + max = Math.max(max, 1 + helper(grid, i, j + 1, dp)); + } + if (i < grid.length - 1 && grid[i + 1][j + 1] > grid[i][j]) { + max = Math.max(max, 1 + helper(grid, i + 1, j + 1, dp)); + } + } + dp[i][j] = max; + return max; +}; +``` + +**Explain:** + +nope. + +**Complexity:** + +* Time complexity : O(m * n). +* Space complexity : O(m * n). diff --git a/3001-3100/3043. Find the Length of the Longest Common Prefix.md b/3001-3100/3043. Find the Length of the Longest Common Prefix.md new file mode 100644 index 0000000..35530a0 --- /dev/null +++ b/3001-3100/3043. Find the Length of the Longest Common Prefix.md @@ -0,0 +1,97 @@ +# 3043. Find the Length of the Longest Common Prefix + +- Difficulty: Medium. +- Related Topics: Array, Hash Table, String, Trie. +- Similar Questions: Longest Common Prefix, Longest Common Suffix Queries. + +## Problem + +You are given two arrays with **positive** integers `arr1` and `arr2`. + +A **prefix** of a positive integer is an integer formed by one or more of its digits, starting from its **leftmost** digit. For example, `123` is a prefix of the integer `12345`, while `234` is **not**. + +A **common prefix** of two integers `a` and `b` is an integer `c`, such that `c` is a prefix of both `a` and `b`. For example, `5655359` and `56554` have a common prefix `565` while `1223` and `43456` **do not** have a common prefix. + +You need to find the length of the **longest common prefix** between all pairs of integers `(x, y)` such that `x` belongs to `arr1` and `y` belongs to `arr2`. + +Return **the length of the **longest** common prefix among all pairs**.** If no common prefix exists among them**, **return** `0`. + +  +Example 1: + +``` +Input: arr1 = [1,10,100], arr2 = [1000] +Output: 3 +Explanation: There are 3 pairs (arr1[i], arr2[j]): +- The longest common prefix of (1, 1000) is 1. +- The longest common prefix of (10, 1000) is 10. +- The longest common prefix of (100, 1000) is 100. +The longest common prefix is 100 with a length of 3. +``` + +Example 2: + +``` +Input: arr1 = [1,2,3], arr2 = [4,4,4] +Output: 0 +Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. +Note that common prefixes between elements of the same array do not count. +``` + +  +**Constraints:** + + + +- `1 <= arr1.length, arr2.length <= 5 * 104` + +- `1 <= arr1[i], arr2[i] <= 108` + + + +## Solution + +```javascript +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function(arr1, arr2) { + var trie = {}; + for (var i = 0; i < arr1.length; i++) { + var str = `${arr1[i]}`; + var map = trie; + for (var j = 0; j < str.length; j++) { + if (!map[str[j]]) { + map[str[j]] = {}; + } + map = map[str[j]]; + } + } + var max = 0; + for (var i = 0; i < arr2.length; i++) { + var str = `${arr2[i]}`; + var map = trie; + var len = 0; + for (var j = 0; j < str.length; j++) { + if (!map[str[j]]) { + break; + } + len += 1; + map = map[str[j]]; + } + max = Math.max(max, len); + } + return max; +}; +``` + +**Explain:** + +Trie. + +**Complexity:** + +* Time complexity : O(n * m). +* Space complexity : O(n * m). diff --git a/601-700/641. Design Circular Deque.md b/601-700/641. Design Circular Deque.md new file mode 100644 index 0000000..f6839ce --- /dev/null +++ b/601-700/641. Design Circular Deque.md @@ -0,0 +1,351 @@ +# 641. Design Circular Deque + +- Difficulty: Medium. +- Related Topics: Array, Linked List, Design, Queue. +- Similar Questions: Design Circular Queue, Design Front Middle Back Queue. + +## Problem + +Design your implementation of the circular double-ended queue (deque). + +Implement the `MyCircularDeque` class: + + + +- `MyCircularDeque(int k)` Initializes the deque with a maximum size of `k`. + +- `boolean insertFront()` Adds an item at the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. + +- `boolean insertLast()` Adds an item at the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. + +- `boolean deleteFront()` Deletes an item from the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. + +- `boolean deleteLast()` Deletes an item from the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. + +- `int getFront()` Returns the front item from the Deque. Returns `-1` if the deque is empty. + +- `int getRear()` Returns the last item from Deque. Returns `-1` if the deque is empty. + +- `boolean isEmpty()` Returns `true` if the deque is empty, or `false` otherwise. + +- `boolean isFull()` Returns `true` if the deque is full, or `false` otherwise. + + +  +Example 1: + +``` +Input +["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] +[[3], [1], [2], [3], [4], [], [], [], [4], []] +Output +[null, true, true, true, false, 2, true, true, true, 4] + +Explanation +MyCircularDeque myCircularDeque = new MyCircularDeque(3); +myCircularDeque.insertLast(1); // return True +myCircularDeque.insertLast(2); // return True +myCircularDeque.insertFront(3); // return True +myCircularDeque.insertFront(4); // return False, the queue is full. +myCircularDeque.getRear(); // return 2 +myCircularDeque.isFull(); // return True +myCircularDeque.deleteLast(); // return True +myCircularDeque.insertFront(4); // return True +myCircularDeque.getFront(); // return 4 +``` + +  +**Constraints:** + + + +- `1 <= k <= 1000` + +- `0 <= value <= 1000` + +- At most `2000` calls will be made to `insertFront`, `insertLast`, `deleteFront`, `deleteLast`, `getFront`, `getRear`, `isEmpty`, `isFull`. + + + +## Solution 1 + +```javascript +/** + * @param {number} k + */ +var MyCircularDeque = function(k) { + this.limit = k; + this.count = 0; + this.front = null; + this.last = null; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertFront = function(value) { + if (this.count === this.limit) return false; + var node = { + value, + prev: null, + next: null, + }; + if (!this.front) { + this.front = node; + this.last = node; + } else { + node.next = this.front; + this.front.prev = node; + this.front = node; + } + this.count += 1; + return true; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertLast = function(value) { + if (this.count === this.limit) return false; + var node = { + value, + prev: null, + next: null, + }; + if (!this.last) { + this.front = node; + this.last = node; + } else { + node.prev = this.last; + this.last.next = node; + this.last = node; + } + this.count += 1; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteFront = function() { + if (!this.front) return false; + if (this.front === this.last) { + this.front = null; + this.last = null; + } else { + this.front.next.prev = null; + this.front = this.front.next; + } + this.count -= 1; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteLast = function() { + if (!this.last) return false; + if (this.front === this.last) { + this.front = null; + this.last = null; + } else { + this.last.prev.next = null; + this.last = this.last.prev; + } + this.count -= 1; + return true; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getFront = function() { + return this.front ? this.front.value : -1; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getRear = function() { + return this.last ? this.last.value : -1; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isEmpty = function() { + return this.count === 0; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isFull = function() { + return this.count === this.limit; +}; + +/** + * 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() + */ +``` + +**Explain:** + +nope. + +**Complexity:** + +* Time complexity : O(1). +* Space complexity : O(n). + +## Solution 2 + +```javascript +/** + * @param {number} k + */ +var MyCircularDeque = function(k) { + this.limit = k; + this.count = 0; + this.arr = Array(k); + this.front = -1; + this.last = -1; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertFront = function(value) { + if (this.count === this.limit) return false; + if (this.count === 0) { + this.front = 0; + this.last = 0; + } else { + this.front -= 1; + if (this.front < 0) { + this.front += this.arr.length; + } + } + this.arr[this.front] = value; + this.count += 1; + return true; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertLast = function(value) { + if (this.count === this.limit) return false; + if (this.count === 0) { + this.front = 0; + this.last = 0; + } else { + this.last += 1; + if (this.last >= this.arr.length) { + this.last -= this.arr.length; + } + } + this.arr[this.last] = value; + this.count += 1; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteFront = function() { + if (this.count === 0) return false; + if (this.count === 1) { + this.front = -1; + this.last = -1; + } else { + this.front += 1; + if (this.front >= this.arr.length) { + this.front -= this.arr.length; + } + } + this.count -= 1; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteLast = function() { + if (this.count === 0) return false; + if (this.count === 1) { + this.front = -1; + this.last = -1; + } else { + this.last -= 1; + if (this.last < 0) { + this.last += this.arr.length; + } + } + this.count -= 1; + return true; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getFront = function() { + return this.front === -1 ? -1 : this.arr[this.front]; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getRear = function() { + return this.last === -1 ? -1 : this.arr[this.last]; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isEmpty = function() { + return this.count === 0; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isFull = function() { + return this.count === this.limit; +}; + +/** + * 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() + */ +``` + +**Explain:** + +nope. + +**Complexity:** + +* Time complexity : O(1). +* Space complexity : O(n).