diff --git a/articles/constrained-subsequence-sum.md b/articles/constrained-subsequence-sum.md new file mode 100644 index 000000000..1c04624df --- /dev/null +++ b/articles/constrained-subsequence-sum.md @@ -0,0 +1,701 @@ +## 1. Dynamic Programming (Top-Down) + +::tabs-start + +```python +class Solution: + def constrainedSubsetSum(self, nums: List[int], k: int) -> int: + memo = [None] * len(nums) + + def dfs(i): + if memo[i] != None: + return memo[i] + + res = nums[i] + for j in range(i + 1, len(nums)): + if j - i > k: + break + res = max(res, nums[i] + dfs(j)) + + memo[i] = res + return res + + ans = float('-inf') + for i in range(len(nums)): + ans = max(ans, dfs(i)) + return ans +``` + +```java +public class Solution { + private int[] nums; + private Integer[] memo; + private int k; + + public int constrainedSubsetSum(int[] nums, int k) { + this.nums = nums; + this.memo = new Integer[nums.length]; + this.k = k; + + int ans = Integer.MIN_VALUE; + for (int i = 0; i < nums.length; i++) { + ans = Math.max(ans, dfs(i)); + } + return ans; + } + + private int dfs(int i) { + if (memo[i] != null) { + return memo[i]; + } + + int res = nums[i]; + for (int j = i + 1; j < nums.length && j - i <= k; j++) { + res = Math.max(res, nums[i] + dfs(j)); + } + + memo[i] = res; + return res; + } +} +``` + +```cpp +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + vector memo(nums.size(), INT_MIN); + int ans = INT_MIN; + for (int i = 0; i < nums.size(); i++) { + ans = max(ans, dfs(nums, memo, k, i)); + } + return ans; + } + +private: + int dfs(vector& nums, vector& memo, int k, int i) { + if (memo[i] != INT_MIN) { + return memo[i]; + } + + int res = nums[i]; + for (int j = i + 1; j < nums.size() && j - i <= k; j++) { + res = max(res, nums[i] + dfs(nums, memo, k, j)); + } + + memo[i] = res; + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + constrainedSubsetSum(nums, k) { + const memo = new Array(nums.length).fill(null); + + const dfs = (i) => { + if (memo[i] !== null) { + return memo[i]; + } + + let res = nums[i]; + for (let j = i + 1; j < nums.length && j - i <= k; j++) { + res = Math.max(res, nums[i] + dfs(j)); + } + + memo[i] = res; + return res; + }; + + let ans = -Infinity; + for (let i = 0; i < nums.length; i++) { + ans = Math.max(ans, dfs(i)); + } + return ans; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * k)$ +* Space complexity: $O(n)$ + +--- + +## 2. Dynamic Programming (Bottom-Up) + +::tabs-start + +```python +class Solution: + def constrainedSubsetSum(self, nums: List[int], k: int) -> int: + dp = [num for num in nums] + + for i in range(1, len(nums)): + for j in range(max(0, i - k), i): + dp[i] = max(dp[i], nums[i] + dp[j]) + + return max(dp) +``` + +```java +public class Solution { + public int constrainedSubsetSum(int[] nums, int k) { + int n = nums.length; + int[] dp = new int[n]; + System.arraycopy(nums, 0, dp, 0, n); + + for (int i = 1; i < n; i++) { + for (int j = Math.max(0, i - k); j < i; j++) { + dp[i] = Math.max(dp[i], nums[i] + dp[j]); + } + } + + int res = Integer.MIN_VALUE; + for (int val : dp) { + res = Math.max(res, val); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + int n = nums.size(); + vector dp(nums.begin(), nums.end()); + + for (int i = 1; i < n; i++) { + for (int j = max(0, i - k); j < i; j++) { + dp[i] = max(dp[i], nums[i] + dp[j]); + } + } + + return *max_element(dp.begin(), dp.end()); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + constrainedSubsetSum(nums, k) { + const n = nums.length; + const dp = [...nums]; + + for (let i = 1; i < n; i++) { + for (let j = Math.max(0, i - k); j < i; j++) { + dp[i] = Math.max(dp[i], nums[i] + dp[j]); + } + } + + return Math.max(...dp); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * k)$ +* Space complexity: $O(n)$ + +--- + +## 3. Dynamic Programming + Segment Tree + +::tabs-start + +```python +class SegmentTree: + def __init__(self, N): + self.n = N + while (self.n & (self.n - 1)) != 0: + self.n += 1 + self.tree = [float('-inf')] * (2 * self.n) + + def update(self, i, val): + i += self.n + self.tree[i] = val + while i > 1: + i >>= 1 + self.tree[i] = max(self.tree[i << 1], self.tree[i << 1 | 1]) + + def query(self, l, r): + res = float('-inf') + l += self.n + r += self.n + 1 + while l < r: + if l & 1: + res = max(res, self.tree[l]) + l += 1 + if r & 1: + r -= 1 + res = max(res, self.tree[r]) + l >>= 1 + r >>= 1 + return max(0, res) + +class Solution: + def constrainedSubsetSum(self, nums: List[int], k: int) -> int: + n = len(nums) + maxSegTree = SegmentTree(n) + maxSegTree.update(0, nums[0]) + res = nums[0] + + for i in range(1, n): + cur = nums[i] + maxSegTree.query(max(0, i - k), i - 1) + maxSegTree.update(i, cur) + res = max(res, cur) + + return res +``` + +```java +class SegmentTree { + int n; + int[] tree; + + public SegmentTree(int N) { + this.n = N; + while ((this.n & (this.n - 1)) != 0) { + this.n++; + } + tree = new int[2 * this.n]; + for (int i = 0; i < 2 * this.n; i++) { + tree[i] = Integer.MIN_VALUE; + } + } + + public void update(int i, int val) { + i += n; + tree[i] = val; + while (i > 1) { + i >>= 1; + tree[i] = Math.max(tree[i << 1], tree[(i << 1) | 1]); + } + } + + public int query(int l, int r) { + int res = Integer.MIN_VALUE; + l += n; + r += n + 1; + while (l < r) { + if ((l & 1) == 1) { + res = Math.max(res, tree[l]); + l++; + } + if ((r & 1) == 1) { + r--; + res = Math.max(res, tree[r]); + } + l >>= 1; + r >>= 1; + } + return Math.max(0, res); + } +} + +public class Solution { + public int constrainedSubsetSum(int[] nums, int k) { + int n = nums.length; + SegmentTree maxSegTree = new SegmentTree(n); + maxSegTree.update(0, nums[0]); + int res = nums[0]; + + for (int i = 1; i < n; i++) { + int cur = nums[i] + maxSegTree.query(Math.max(0, i - k), i - 1); + maxSegTree.update(i, cur); + res = Math.max(res, cur); + } + + return res; + } +} +``` + +```cpp +class SegmentTree { +public: + int n; + vector tree; + + SegmentTree(int N) { + n = N; + while ((n & (n - 1)) != 0) { + n++; + } + tree.assign(2 * n, INT_MIN); + } + + void update(int i, int val) { + i += n; + tree[i] = val; + while (i > 1) { + i >>= 1; + tree[i] = max(tree[i << 1], tree[i << 1 | 1]); + } + } + + int query(int l, int r) { + int res = INT_MIN; + l += n; + r += n + 1; + while (l < r) { + if (l & 1) { + res = max(res, tree[l]); + l++; + } + if (r & 1) { + r--; + res = max(res, tree[r]); + } + l >>= 1; + r >>= 1; + } + return max(0, res); + } +}; + +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + int n = nums.size(); + SegmentTree maxSegTree(n); + maxSegTree.update(0, nums[0]); + int res = nums[0]; + + for (int i = 1; i < n; i++) { + int cur = nums[i] + maxSegTree.query(max(0, i - k), i - 1); + maxSegTree.update(i, cur); + res = max(res, cur); + } + + return res; + } +}; +``` + +```javascript +class SegmentTree { + /** + * @constructor + * @param {number} N + */ + constructor(N) { + this.n = N; + while ((this.n & (this.n - 1)) !== 0) { + this.n++; + } + this.tree = new Array(2 * this.n).fill(-Infinity); + } + + /** + * @param {number} i + * @param {number} val + * @return {void} + */ + update(i, val) { + i += this.n; + this.tree[i] = val; + while (i > 1) { + i >>= 1; + this.tree[i] = Math.max(this.tree[i << 1], this.tree[i << 1 | 1]); + } + } + + /** + * @param {number} l + * @param {number} r + * @return {number} + */ + query(l, r) { + let res = -Infinity; + l += this.n; + r += this.n + 1; + while (l < r) { + if (l & 1) { + res = Math.max(res, this.tree[l]); + l++; + } + if (r & 1) { + r--; + res = Math.max(res, this.tree[r]); + } + l >>= 1; + r >>= 1; + } + return Math.max(0, res); + } +} + +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + constrainedSubsetSum(nums, k) { + const n = nums.length; + const maxSegTree = new SegmentTree(n); + maxSegTree.update(0, nums[0]); + let res = nums[0]; + + for (let i = 1; i < n; i++) { + let cur = nums[i] + maxSegTree.query(Math.max(0, i - k), i - 1); + maxSegTree.update(i, cur); + res = Math.max(res, cur); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Max-Heap + +::tabs-start + +```python +class Solution: + def constrainedSubsetSum(self, nums: List[int], k: int) -> int: + res = nums[0] + max_heap = [(-nums[0], 0)] # max_sum, index + + for i in range(1, len(nums)): + while i - max_heap[0][1] > k: + heapq.heappop(max_heap) + + cur_max = max(nums[i], nums[i] - max_heap[0][0]) + res = max(res, cur_max) + heapq.heappush(max_heap, (-cur_max, i)) + + return res +``` + +```java +public class Solution { + public int constrainedSubsetSum(int[] nums, int k) { + int res = nums[0]; + PriorityQueue maxHeap = new PriorityQueue<>( + (a, b) -> b[0] - a[0] // max_sum, index + ); + maxHeap.offer(new int[]{nums[0], 0}); + + for (int i = 1; i < nums.length; i++) { + while (i - maxHeap.peek()[1] > k) { + maxHeap.poll(); + } + + int curMax = Math.max(nums[i], nums[i] + maxHeap.peek()[0]); + res = Math.max(res, curMax); + maxHeap.offer(new int[]{curMax, i}); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + int res = nums[0]; + priority_queue> maxHeap; // max_sum, index + maxHeap.emplace(nums[0], 0); + + for (int i = 1; i < nums.size(); i++) { + while (i - maxHeap.top().second > k) { + maxHeap.pop(); + } + + int curMax = max(nums[i], nums[i] + maxHeap.top().first); + res = max(res, curMax); + maxHeap.emplace(curMax, i); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + constrainedSubsetSum(nums, k) { + let res = nums[0]; + const maxHeap = new PriorityQueue( + (a, b) => b[0] - a[0] // max_sum, index + ); + maxHeap.enqueue([nums[0], 0]); + + for (let i = 1; i < nums.length; i++) { + while (i - maxHeap.front()[1] > k) { + maxHeap.dequeue(); + } + + let curMax = Math.max(nums[i], nums[i] + maxHeap.front()[0]); + res = Math.max(res, curMax); + maxHeap.enqueue([curMax, i]); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 5. Monotonic Deque + +::tabs-start + +```python +class Solution: + def constrainedSubsetSum(self, nums: List[int], k: int) -> int: + n = len(nums) + dq = deque([(0, nums[0])]) + res = nums[0] + + for i in range(1, n): + if dq and dq[0][0] < i - k: + dq.popleft() + + cur = max(0, dq[0][1]) + nums[i] + while dq and cur > dq[-1][1]: + dq.pop() + + dq.append((i, cur)) + res = max(res, cur) + + return res +``` + +```java +public class Solution { + public int constrainedSubsetSum(int[] nums, int k) { + int n = nums.length; + Deque dq = new ArrayDeque<>(); + dq.offer(new int[]{0, nums[0]}); + int res = nums[0]; + + for (int i = 1; i < n; i++) { + if (!dq.isEmpty() && dq.peekFirst()[0] < i - k) { + dq.pollFirst(); + } + + int cur = Math.max(0, dq.peekFirst()[1]) + nums[i]; + while (!dq.isEmpty() && cur > dq.peekLast()[1]) { + dq.pollLast(); + } + + dq.offer(new int[]{i, cur}); + res = Math.max(res, cur); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int constrainedSubsetSum(vector& nums, int k) { + int n = nums.size(); + deque> dq{{0, nums[0]}}; + int res = nums[0]; + + for (int i = 1; i < n; i++) { + if (!dq.empty() && dq.front().first < i - k) { + dq.pop_front(); + } + + int cur = max(0, dq.front().second) + nums[i]; + while (!dq.empty() && cur > dq.back().second) { + dq.pop_back(); + } + + dq.emplace_back(i, cur); + res = max(res, cur); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ + constrainedSubsetSum(nums, k) { + const n = nums.length; + const dq = new Deque([[0, nums[0]]]); + let res = nums[0]; + + for (let i = 1; i < n; i++) { + if (!dq.isEmpty() && dq.front()[0] < i - k) { + dq.popFront(); + } + + let cur = Math.max(0, dq.front()[1]) + nums[i]; + while (!dq.isEmpty() && cur > dq.back()[1]) { + dq.popBack(); + } + + dq.pushBack([i, cur]); + res = Math.max(res, cur); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(k)$ \ No newline at end of file diff --git a/articles/find-the-kth-largest-integer-in-the-array.md b/articles/find-the-kth-largest-integer-in-the-array.md new file mode 100644 index 000000000..ba70f2ed8 --- /dev/null +++ b/articles/find-the-kth-largest-integer-in-the-array.md @@ -0,0 +1,543 @@ +## 1. Sorting + +::tabs-start + +```python +class Solution: + def kthLargestNumber(self, nums: List[str], k: int) -> str: + return sorted(nums, key=lambda x: (len(x), x), reverse=True)[k - 1] +``` + +```java +public class Solution { + public String kthLargestNumber(String[] nums, int k) { + Arrays.sort(nums, + (a, b) -> a.length() == b.length() ? b.compareTo(a) : b.length() - a.length() + ); + return nums[k - 1]; + } +} +``` + +```cpp +class Solution { +public: + string kthLargestNumber(vector& nums, int k) { + sort(nums.begin(), nums.end(), [](const string& a, const string& b) { + return a.size() == b.size() ? a > b : a.size() > b.size(); + }); + return nums[k - 1]; + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} nums + * @param {number} k + * @return {string} + */ + kthLargestNumber(nums, k) { + nums.sort( + (a, b) => a.length === b.length ? b.localeCompare(a) : b.length - a.length + ); + return nums[k - 1]; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * n \log n)$ +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. + +> Where $n$ is the number of strings and $m$ is the average length of a string. + +--- + +## 2. Max-Heap + +::tabs-start + +```python +class Num: + def __init__(self, s: str): + self.s = s + + def __lt__(self, other: "Num") -> bool: + if len(self.s) != len(other.s): + return len(self.s) > len(other.s) + return self.s > other.s + +class Solution: + def kthLargestNumber(self, nums: List[str], k: int) -> str: + maxHeap = [Num(s) for s in nums] + heapq.heapify(maxHeap) + + for _ in range(k - 1): + heapq.heappop(maxHeap) + + return heapq.heappop(maxHeap).s +``` + +```java +public class Solution { + public String kthLargestNumber(String[] nums, int k) { + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> + a.length() == b.length() ? b.compareTo(a) : Integer.compare(b.length(), a.length()) + ); + + for (String num : nums) { + maxHeap.offer(num); + } + + while (--k > 0) { + maxHeap.poll(); + } + + return maxHeap.poll(); + } +} +``` + +```cpp +class Solution { +public: + string kthLargestNumber(vector& nums, int k) { + auto cmp = [](const string& a, const string& b) { + return a.size() == b.size() ? a < b : a.size() < b.size(); + }; + + priority_queue, decltype(cmp)> maxHeap(cmp); + + for (const string& num : nums) { + maxHeap.push(num); + } + + while (--k > 0) { + maxHeap.pop(); + } + + return maxHeap.top(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} nums + * @param {number} k + * @return {string} + */ + kthLargestNumber(nums, k) { + const maxHeap = new PriorityQueue( + (a, b) => a.length === b.length ? b.localeCompare(a) : b.length - a.length + ); + + for (const num of nums) { + maxHeap.enqueue(num); + } + + while (--k > 0) { + maxHeap.dequeue(); + } + + return maxHeap.dequeue(); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * (n + k) * \log n)$ +* Space complexity: $O(n)$ + +> Where $n$ is the number of strings and $m$ is the average length of a string. + +--- + +## 3. Min-Heap + +::tabs-start + +```python +class Num: + def __init__(self, s: str): + self.s = s + + def __lt__(self, other: "Num") -> bool: + if len(self.s) != len(other.s): + return len(self.s) < len(other.s) + return self.s < other.s + +class Solution: + def kthLargestNumber(self, nums: List[str], k: int) -> str: + minHeap = [] + for num in nums: + heapq.heappush(minHeap, Num(num)) + if len(minHeap) > k: + heapq.heappop(minHeap) + return minHeap[0].s +``` + +```java +public class Solution { + public String kthLargestNumber(String[] nums, int k) { + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> + a.length() == b.length() ? a.compareTo(b) : Integer.compare(a.length(), b.length()) + ); + + for (String num : nums) { + minHeap.offer(num); + if (minHeap.size() > k) { + minHeap.poll(); + } + } + + return minHeap.peek(); + } +} +``` + +```cpp +class Solution { +public: + string kthLargestNumber(vector& nums, int k) { + auto cmp = [](const string& a, const string& b) { + return a.size() == b.size() ? a > b : a.size() > b.size(); + }; + + priority_queue, decltype(cmp)> minHeap(cmp); + + for (const string& num : nums) { + minHeap.push(num); + if (minHeap.size() > k) { + minHeap.pop(); + } + } + + return minHeap.top(); + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} nums + * @param {number} k + * @return {string} + */ + kthLargestNumber(nums, k) { + const minHeap = new PriorityQueue( + (a, b) => a.length === b.length ? a.localeCompare(b) : a.length - b.length + ); + + for (const num of nums) { + minHeap.enqueue(num); + if (minHeap.size() > k) { + minHeap.dequeue(); + } + } + + return minHeap.front(); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * n * \log k)$ +* Space complexity: $O(k)$ + +> Where $n$ is the number of strings and $m$ is the average length of a string. + +--- + +## 4. Quick Select + +::tabs-start + +```python +class Solution: + def greater(self, x: str, y: str) -> bool: + if len(x) != len(y): + return len(x) > len(y) + return x > y + + def less(self, x: str, y: str) -> bool: + if len(x) != len(y): + return len(x) < len(y) + return x < y + + def partition(self, nums: List[str], left: int, right: int) -> int: + mid = (left + right) >> 1 + nums[mid], nums[left + 1] = nums[left + 1], nums[mid] + + if self.less(nums[left], nums[right]): + nums[left], nums[right] = nums[right], nums[left] + if self.less(nums[left + 1], nums[right]): + nums[left + 1], nums[right] = nums[right], nums[left + 1] + if self.less(nums[left], nums[left + 1]): + nums[left], nums[left + 1] = nums[left + 1], nums[left] + + pivot = nums[left + 1] + i = left + 1 + j = right + + while True: + while True: + i += 1 + if not self.greater(nums[i], pivot): + break + while True: + j -= 1 + if not self.less(nums[j], pivot): + break + if i > j: + break + nums[i], nums[j] = nums[j], nums[i] + + nums[left + 1], nums[j] = nums[j], nums[left + 1] + return j + + def quickSelect(self, nums: List[str], k: int) -> str: + left = 0 + right = len(nums) - 1 + + while True: + if right <= left + 1: + if right == left + 1 and self.greater(nums[right], nums[left]): + nums[left], nums[right] = nums[right], nums[left] + return nums[k] + + j = self.partition(nums, left, right) + if j >= k: + right = j - 1 + if j <= k: + left = j + 1 + + def kthLargestNumber(self, nums: List[str], k: int) -> str: + return self.quickSelect(nums, k - 1) +``` + +```java +public class Solution { + public String kthLargestNumber(String[] nums, int k) { + return quickSelect(nums, k - 1); + } + + private boolean greater(String x, String y) { + if (x.length() != y.length()) { + return x.length() > y.length(); + } + return x.compareTo(y) > 0; + } + + private boolean less(String x, String y) { + if (x.length() != y.length()) { + return x.length() < y.length(); + } + return x.compareTo(y) < 0; + } + + private int partition(String[] nums, int left, int right) { + int mid = (left + right) >> 1; + swap(nums, mid, left + 1); + + if (less(nums[left], nums[right])) { + swap(nums, left, right); + } + if (less(nums[left + 1], nums[right])) { + swap(nums, left + 1, right); + } + if (less(nums[left], nums[left + 1])) { + swap(nums, left, left + 1); + } + + String pivot = nums[left + 1]; + int i = left + 1, j = right; + + while (true) { + while (greater(nums[++i], pivot)); + while (less(nums[--j], pivot)); + if (i > j) break; + swap(nums, i, j); + } + + swap(nums, left + 1, j); + return j; + } + + private String quickSelect(String[] nums, int k) { + int left = 0, right = nums.length - 1; + + while (true) { + if (right <= left + 1) { + if (right == left + 1 && greater(nums[right], nums[left])) { + swap(nums, left, right); + } + return nums[k]; + } + + int j = partition(nums, left, right); + if (j >= k) { + right = j - 1; + } + if (j <= k) { + left = j + 1; + } + } + } + + private void swap(String[] nums, int i, int j) { + String temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} +``` + +```cpp +class Solution { +public: + string kthLargestNumber(vector& nums, int k) { + return quickSelect(nums, k - 1); + } + +private: + bool greater(const string& x, const string& y) { + if (x.size() != y.size()) { + return x.size() > y.size(); + } + return x > y; + } + + bool less(const string& x, const string& y) { + if (x.size() != y.size()) { + return x.size() < y.size(); + } + return x < y; + } + + int partition(vector& nums, int left, int right) { + int mid = (left + right) >> 1; + swap(nums[mid], nums[left + 1]); + + if (less(nums[left], nums[right])) { + swap(nums[left], nums[right]); + } + if (less(nums[left + 1], nums[right])) { + swap(nums[left + 1], nums[right]); + } + if (less(nums[left], nums[left + 1])) { + swap(nums[left], nums[left + 1]); + } + + string pivot = nums[left + 1]; + int i = left + 1, j = right; + + while (true) { + while (greater(nums[++i], pivot)); + while (less(nums[--j], pivot)); + if (i > j) break; + swap(nums[i], nums[j]); + } + + swap(nums[left + 1], nums[j]); + return j; + } + + string quickSelect(vector& nums, int k) { + int left = 0, right = nums.size() - 1; + + while (true) { + if (right <= left + 1) { + if (right == left + 1 && greater(nums[right], nums[left])) { + swap(nums[left], nums[right]); + } + return nums[k]; + } + + int j = partition(nums, left, right); + if (j >= k) { + right = j - 1; + } + if (j <= k) { + left = j + 1; + } + } + } +}; +``` + +```javascript +class Solution { + /** + * @param {string[]} nums + * @param {number} k + * @return {string} + */ + kthLargestNumber(nums, k) { + const greater = (x, y) => x.length !== y.length ? x.length > y.length : x > y; + const less = (x, y) => x.length !== y.length ? x.length < y.length : x < y; + const swap = (arr, i, j) => [arr[i], arr[j]] = [arr[j], arr[i]]; + + const partition = (nums, left, right) => { + const mid = Math.floor((left + right) / 2); + swap(nums, mid, left + 1); + + if (less(nums[left], nums[right])) swap(nums, left, right); + if (less(nums[left + 1], nums[right])) swap(nums, left + 1, right); + if (less(nums[left], nums[left + 1])) swap(nums, left, left + 1); + + const pivot = nums[left + 1]; + let i = left + 1, j = right; + + while (true) { + while (greater(nums[++i], pivot)); + while (less(nums[--j], pivot)); + if (i > j) break; + swap(nums, i, j); + } + + swap(nums, left + 1, j); + return j; + }; + + const quickSelect = (nums, k) => { + let left = 0, right = nums.length - 1; + + while (true) { + if (right <= left + 1) { + if (right === left + 1 && greater(nums[right], nums[left])) { + swap(nums, left, right); + } + return nums[k]; + } + + const j = partition(nums, left, right); + if (j >= k) right = j - 1; + if (j <= k) left = j + 1; + } + }; + + return quickSelect(nums, k - 1); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * n)$ in average case, $O(m * n ^ 2)$ in worst case. +* Space complexity: $O(1)$ \ No newline at end of file diff --git a/articles/find-the-longest-valid-obstacle-course-at-each-position.md b/articles/find-the-longest-valid-obstacle-course-at-each-position.md new file mode 100644 index 000000000..ff2346555 --- /dev/null +++ b/articles/find-the-longest-valid-obstacle-course-at-each-position.md @@ -0,0 +1,387 @@ +## 1. Dynamic Programming (Top-Down) + +::tabs-start + +```python +class Solution: + def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]: + n = len(obstacles) + dp = [[-1] * (n + 1) for _ in range(n)] + + def dfs(i, prev): + if i < 0: + return 0 + if dp[i][prev] != -1: + return dp[i][prev] + + res = dfs(i - 1, prev) + if prev == n or obstacles[prev] >= obstacles[i]: + res = max(res, 1 + dfs(i - 1, i)) + dp[i][prev] = res + return res + + dfs(n - 1, n) + return [1] + [1 + dp[i - 1][i] for i in range(1, n)] +``` + +```java +public class Solution { + private int[][] dp; + + public int[] longestObstacleCourseAtEachPosition(int[] obstacles) { + int n = obstacles.length; + this.dp = new int[n][n + 1]; + for (int[] row : dp) { + Arrays.fill(row, -1); + } + + dfs(n - 1, n, obstacles); + + int[] res = new int[n]; + res[0] = 1; + for (int i = 1; i < n; i++) { + res[i] = 1 + dp[i - 1][i]; + } + return res; + } + + private int dfs(int i, int prev, int[] obstacles) { + if (i < 0) { + return 0; + } + if (dp[i][prev] != -1) { + return dp[i][prev]; + } + + int res = dfs(i - 1, prev, obstacles); + if (prev == obstacles.length || obstacles[prev] >= obstacles[i]) { + res = Math.max(res, 1 + dfs(i - 1, i, obstacles)); + } + return dp[i][prev] = res; + } +} +``` + +```cpp +class Solution { +public: + vector> dp; + + vector longestObstacleCourseAtEachPosition(vector& obstacles) { + int n = obstacles.size(); + this->dp = vector>(n, vector(n + 1, -1)); + + dfs(n - 1, n, obstacles); + + vector res(n, 1); + for (int i = 1; i < n; i++) { + res[i] = 1 + dp[i - 1][i]; + } + return res; + } + +private: + int dfs(int i, int prev, vector& obstacles) { + if (i < 0) { + return 0; + } + if (dp[i][prev] != -1) { + return dp[i][prev]; + } + + int res = dfs(i - 1, prev, obstacles); + if (prev == obstacles.size() || obstacles[prev] >= obstacles[i]) { + res = max(res, 1 + dfs(i - 1, i, obstacles)); + } + return dp[i][prev] = res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} obstacles + * @return {number[]} + */ + longestObstacleCourseAtEachPosition(obstacles) { + const n = obstacles.length; + const dp = Array.from({ length: n }, () => new Array(n + 1).fill(-1)); + + const dfs = (i, prev) => { + if (i < 0) { + return 0; + } + if (dp[i][prev] !== -1) { + return dp[i][prev]; + } + + let res = dfs(i - 1, prev); + if (prev === n || obstacles[prev] >= obstacles[i]) { + res = Math.max(res, 1 + dfs(i - 1, i)); + } + dp[i][prev] = res; + return res; + }; + + dfs(n - 1, n); + + const res = new Array(n).fill(1); + for (let i = 1; i < n; i++) { + res[i] = 1 + dp[i - 1][i]; + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(n ^ 2)$ + +--- + +## 2. Dynamic Programming (Binary Search) - I + +::tabs-start + +```python +class Solution: + def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]: + res = [] + dp = [10**8] * (len(obstacles) + 1) + + for num in obstacles: + index = bisect.bisect(dp, num) + res.append(index + 1) + dp[index] = num + + return res +``` + +```java +public class Solution { + public int[] longestObstacleCourseAtEachPosition(int[] obstacles) { + int n = obstacles.length; + int[] res = new int[n]; + int[] dp = new int[n + 1]; + Arrays.fill(dp, (int) 1e8); + + for (int i = 0; i < n; i++) { + int index = upperBound(dp, obstacles[i]); + res[i] = index + 1; + dp[index] = obstacles[i]; + } + + return res; + } + + private int upperBound(int[] dp, int target) { + int left = 0, right = dp.length; + while (left < right) { + int mid = left + (right - left) / 2; + if (dp[mid] > target) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } +} +``` + +```cpp +class Solution { +public: + vector longestObstacleCourseAtEachPosition(vector& obstacles) { + int n = obstacles.size(); + vector res(n); + vector dp(n + 1, 1e8); + + for (int i = 0; i < n; i++) { + int index = upper_bound(dp.begin(), dp.end(), obstacles[i]) - dp.begin(); + res[i] = index + 1; + dp[index] = obstacles[i]; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} obstacles + * @return {number[]} + */ + longestObstacleCourseAtEachPosition(obstacles) { + let n = obstacles.length; + let res = new Array(n).fill(0); + let dp = new Array(n + 1).fill(1e8); + + const upperBound = (dp, target) => { + let left = 0, right = dp.length; + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (dp[mid] > target) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + }; + + for (let i = 0; i < n; i++) { + let index = upperBound(dp, obstacles[i]); + res[i] = index + 1; + dp[index] = obstacles[i]; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Dynamic Programming (Binary Search) - II + +::tabs-start + +```python +class Solution: + def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]: + res = [] + dp = [] + + for num in obstacles: + index = bisect.bisect_right(dp, num) + res.append(index + 1) + + if index == len(dp): + dp.append(num) + else: + dp[index] = num + + return res +``` + +```java +public class Solution { + public int[] longestObstacleCourseAtEachPosition(int[] obstacles) { + int n = obstacles.length; + int[] res = new int[n]; + List dp = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + int index = upperBound(dp, obstacles[i]); + res[i] = index + 1; + + if (index == dp.size()) { + dp.add(obstacles[i]); + } else { + dp.set(index, obstacles[i]); + } + } + + return res; + } + + private int upperBound(List dp, int target) { + int left = 0, right = dp.size(); + while (left < right) { + int mid = left + (right - left) / 2; + if (dp.get(mid) > target) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } +} +``` + +```cpp +class Solution { +public: + vector longestObstacleCourseAtEachPosition(vector& obstacles) { + int n = obstacles.size(); + vector res(n); + vector dp; + + for (int i = 0; i < n; i++) { + int index = upper_bound(dp.begin(), dp.end(), obstacles[i]) - dp.begin(); + res[i] = index + 1; + + if (index == dp.size()) { + dp.push_back(obstacles[i]); + } else { + dp[index] = obstacles[i]; + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} obstacles + * @return {number[]} + */ + longestObstacleCourseAtEachPosition(obstacles) { + let n = obstacles.length; + let res = new Array(n).fill(0); + let dp = []; + + const upperBound = (dp, target) => { + let left = 0, right = dp.length; + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (dp[mid] > target) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + }; + + for (let i = 0; i < n; i++) { + let index = upperBound(dp, obstacles[i]); + res[i] = index + 1; + + if (index === dp.length) { + dp.push(obstacles[i]); + } else { + dp[index] = obstacles[i]; + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/least-number-of-unique-integers-after-k-removals.md b/articles/least-number-of-unique-integers-after-k-removals.md new file mode 100644 index 000000000..ddffbfb03 --- /dev/null +++ b/articles/least-number-of-unique-integers-after-k-removals.md @@ -0,0 +1,343 @@ +## 1. Sorting + +::tabs-start + +```python +class Solution: + def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: + freq = sorted(Counter(arr).values()) + n = len(freq) + for i in range(n): + if k >= freq[i]: + k -= freq[i] + else: + return n - i + return 0 +``` + +```java +public class Solution { + public int findLeastNumOfUniqueInts(int[] arr, int k) { + Map freqMap = new HashMap<>(); + for (int num : arr) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + + List freq = new ArrayList<>(freqMap.values()); + Collections.sort(freq); + + int n = freq.size(); + for (int i = 0; i < n; i++) { + if (k >= freq.get(i)) { + k -= freq.get(i); + } else { + return n - i; + } + } + return 0; + } +} +``` + +```cpp +class Solution { +public: + int findLeastNumOfUniqueInts(vector& arr, int k) { + unordered_map freqMap; + for (int num : arr) { + freqMap[num]++; + } + + vector freq; + for (auto& [_, count] : freqMap) { + freq.push_back(count); + } + + sort(freq.begin(), freq.end()); + + int n = freq.size(); + for (int i = 0; i < n; i++) { + if (k >= freq[i]) { + k -= freq[i]; + } else { + return n - i; + } + } + return 0; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @return {number} + */ + findLeastNumOfUniqueInts(arr, k) { + let freqMap = new Map(); + for (let num of arr) { + freqMap.set(num, (freqMap.get(num) || 0) + 1); + } + + let freq = Array.from(freqMap.values()).sort((a, b) => a - b); + + let n = freq.length; + for (let i = 0; i < n; i++) { + if (k >= freq[i]) { + k -= freq[i]; + } else { + return n - i; + } + } + return 0; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Min-Heap + +::tabs-start + +```python +class Solution: + def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: + freq = Counter(arr) + heap = list(freq.values()) + heapq.heapify(heap) + + res = len(heap) + while k > 0 and heap: + f = heapq.heappop(heap) + if k >= f: + k -= f + res -= 1 + return res +``` + +```java +public class Solution { + public int findLeastNumOfUniqueInts(int[] arr, int k) { + Map freqMap = new HashMap<>(); + for (int num : arr) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + + PriorityQueue minHeap = new PriorityQueue<>(freqMap.values()); + + int res = minHeap.size(); + while (k > 0 && !minHeap.isEmpty()) { + int f = minHeap.poll(); + if (k >= f) { + k -= f; + res--; + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int findLeastNumOfUniqueInts(vector& arr, int k) { + unordered_map freqMap; + for (int num : arr) { + freqMap[num]++; + } + + priority_queue, greater> minHeap; + for (auto& [_, count] : freqMap) { + minHeap.push(count); + } + + int res = minHeap.size(); + while (k > 0 && !minHeap.empty()) { + int f = minHeap.top(); + minHeap.pop(); + if (k >= f) { + k -= f; + res--; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @return {number} + */ + findLeastNumOfUniqueInts(arr, k) { + let freqMap = new Map(); + for (let num of arr) { + freqMap.set(num, (freqMap.get(num) || 0) + 1); + } + + const minHeap = MinPriorityQueue.fromArray([...freqMap.values()]); + + let res = minHeap.size(); + while (k > 0 && !minHeap.isEmpty()) { + let f = minHeap.pop(); + if (k >= f) { + k -= f; + res--; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Bucket Sort + +::tabs-start + +```python +class Solution: + def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: + freq = Counter(arr) + freq_list = [0] * (len(arr) + 1) + + for n, f in freq.items(): + freq_list[f] += 1 + + res = len(freq) + for f in range(1, len(freq_list)): + remove = freq_list[f] + if k >= f * remove: + k -= f * remove + res -= remove + else: + remove = k // f + res -= remove + break + return res +``` + +```java +public class Solution { + public int findLeastNumOfUniqueInts(int[] arr, int k) { + Map freqMap = new HashMap<>(); + for (int num : arr) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + + int[] freqList = new int[arr.length + 1]; + for (int f : freqMap.values()) { + freqList[f]++; + } + + int res = freqMap.size(); + for (int f = 1; f < freqList.length; f++) { + int remove = freqList[f]; + if (k >= f * remove) { + k -= f * remove; + res -= remove; + } else { + remove = k / f; + res -= remove; + break; + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int findLeastNumOfUniqueInts(vector& arr, int k) { + unordered_map freqMap; + for (int num : arr) { + freqMap[num]++; + } + + vector freqList(arr.size() + 1, 0); + for (auto& [_, f] : freqMap) { + freqList[f]++; + } + + int res = freqMap.size(); + for (int f = 1; f < freqList.size(); f++) { + int remove = freqList[f]; + if (k >= f * remove) { + k -= f * remove; + res -= remove; + } else { + remove = k / f; + res -= remove; + break; + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} arr + * @param {number} k + * @return {number} + */ + findLeastNumOfUniqueInts(arr, k) { + let freqMap = new Map(); + for (let num of arr) { + freqMap.set(num, (freqMap.get(num) || 0) + 1); + } + + let freqList = new Array(arr.length + 1).fill(0); + for (let f of freqMap.values()) { + freqList[f]++; + } + + let res = freqMap.size; + for (let f = 1; f < freqList.length; f++) { + let remove = freqList[f]; + if (k >= f * remove) { + k -= f * remove; + res -= remove; + } else { + remove = Math.floor(k / f); + res -= remove; + break; + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/maximum-performance-of-a-team.md b/articles/maximum-performance-of-a-team.md new file mode 100644 index 000000000..3fc63e969 --- /dev/null +++ b/articles/maximum-performance-of-a-team.md @@ -0,0 +1,238 @@ +## 1. Brute Force (Recursion) + +::tabs-start + +```python +class Solution: + def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: + MOD = 1000000007 + res = 0 + + def dfs(i, k, speedSum, minEff): + nonlocal res + res = max(res, speedSum * minEff) + if i == n or k == 0: + return + + dfs(i + 1, k, speedSum, minEff) + dfs(i + 1, k - 1, speedSum + speed[i], min(minEff, efficiency[i])) + + dfs(0, k, 0, float("inf")) + return res % MOD +``` + +```java +public class Solution { + private static final int MOD = 1000000007; + private int[] speed, efficiency; + private int n; + private long res; + + public int maxPerformance(int n, int[] speed, int[] efficiency, int k) { + this.n = n; + this.speed = speed; + this.efficiency = efficiency; + this.res = 0; + + dfs(0, k, Integer.MAX_VALUE, 0); + return (int) (res % MOD); + } + + private void dfs(int i, int k, int minEff, long speedSum) { + res = Math.max(res, speedSum * minEff); + if (i == n || k == 0) return; + + dfs(i + 1, k, minEff, speedSum); + dfs(i + 1, k - 1, Math.min(minEff, efficiency[i]), speedSum + speed[i]); + } +} +``` + +```cpp +class Solution { +private: + static constexpr int MOD = 1000000007; + vector speed, efficiency; + int n; + long long res; + +public: + int maxPerformance(int n, vector& speed, vector& efficiency, int k) { + this->n = n; + this->speed = speed; + this->efficiency = efficiency; + res = 0; + + dfs(0, k, INT_MAX, 0); + return int(res % MOD); + } + +private: + void dfs(int i, int k, int minEff, long long speedSum) { + res = max(res, speedSum * minEff); + if (i == n || k == 0) return; + + dfs(i + 1, k, minEff, speedSum); + dfs(i + 1, k - 1, min(minEff, efficiency[i]), speedSum + speed[i]); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @param {number[]} speed + * @param {number[]} efficiency + * @param {number} k + * @return {number} + */ + maxPerformance(n, speed, efficiency, k) { + const MOD = 1000000007; + let res = 0; + + const dfs = (i, k, minEff, speedSum) => { + res = Math.max(res, minEff === Infinity ? 0 : speedSum * minEff); + if (i === n || k === 0) return; + + dfs(i + 1, k, minEff, speedSum); + dfs(i + 1, k - 1, Math.min(minEff, efficiency[i]), speedSum + speed[i]); + }; + + dfs(0, k, Infinity, 0); + return res % MOD; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(2 ^ n)$ +* Space complexity: $O(n)$ for recursion stack. + +--- + +## 2. Sorting + Min-Heap + +::tabs-start + +```python +class Solution: + def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: + MOD = 10**9 + 7 + eng = sorted(zip(efficiency, speed), reverse=True) + + res = speedSum = 0 + minHeap = [] + + for eff, spd in eng: + if len(minHeap) == k: + speedSum -= heapq.heappop(minHeap) + speedSum += spd + heapq.heappush(minHeap, spd) + res = max(res, eff * speedSum) + + return res % MOD +``` + +```java +public class Solution { + public int maxPerformance(int n, int[] speed, int[] efficiency, int k) { + final int MOD = 1_000_000_007; + int[][] engineers = new int[n][2]; + + for (int i = 0; i < n; i++) { + engineers[i] = new int[]{efficiency[i], speed[i]}; + } + + Arrays.sort(engineers, (a, b) -> Integer.compare(b[0], a[0])); + + PriorityQueue minHeap = new PriorityQueue<>(); + long speedSum = 0, res = 0; + + for (int[] eng : engineers) { + if (minHeap.size() == k) { + speedSum -= minHeap.poll(); + } + speedSum += eng[1]; + minHeap.offer(eng[1]); + res = Math.max(res, speedSum * eng[0]); + } + + return (int) (res % MOD); + } +} +``` + +```cpp +class Solution { +public: + int maxPerformance(int n, vector& speed, vector& efficiency, int k) { + constexpr int MOD = 1'000'000'007; + vector> engineers; + + for (int i = 0; i < n; i++) { + engineers.emplace_back(efficiency[i], speed[i]); + } + + sort(engineers.rbegin(), engineers.rend()); + + priority_queue, greater> minHeap; + long long speedSum = 0, res = 0; + + for (const auto& [eff, spd] : engineers) { + if (minHeap.size() == k) { + speedSum -= minHeap.top(); + minHeap.pop(); + } + speedSum += spd; + minHeap.push(spd); + res = max(res, speedSum * eff); + } + + return res % MOD; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @param {number[]} speed + * @param {number[]} efficiency + * @param {number} k + * @return {number} + */ + maxPerformance(n, speed, efficiency, k) { + const MOD = BigInt(1e9 + 7); + const engineers = efficiency.map((eff, i) => [eff, speed[i]]); + engineers.sort((a, b) => b[0] - a[0]); + + const minHeap = new MinPriorityQueue(); + let speedSum = BigInt(0), res = BigInt(0); + + for (const [eff, spd] of engineers) { + if (minHeap.size() === k) { + speedSum -= BigInt(minHeap.dequeue()); + } + speedSum += BigInt(spd); + minHeap.enqueue(spd); + res = res > speedSum * BigInt(eff) ? res : speedSum * BigInt(eff); + } + + return Number(res % MOD); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * (\log n + \log k))$ +* Space complexity: $O(n + k)$ + +> Where $n$ is the number of engineers and $k$ is the maximum number of engineers that can be selected. \ No newline at end of file diff --git a/articles/maximum-profit-in-job-scheduling.md b/articles/maximum-profit-in-job-scheduling.md new file mode 100644 index 000000000..181614b62 --- /dev/null +++ b/articles/maximum-profit-in-job-scheduling.md @@ -0,0 +1,655 @@ +## 1. Dynamic Programming (Top-Down) + +::tabs-start + +```python +class Solution: + def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int: + intervals = sorted(zip(startTime, endTime, profit)) + cache = {} + + def dfs(i): + if i == len(intervals): + return 0 + if i in cache: + return cache[i] + + # don't include + res = dfs(i + 1) + + # include + j = i + 1 + while j < len(intervals): + if intervals[i][1] <= intervals[j][0]: + break + j += 1 + + cache[i] = res = max(res, intervals[i][2] + dfs(j)) + return res + + return dfs(0) +``` + +```java +public class Solution { + private int[][] intervals; + private int[] cache; + + public int jobScheduling(int[] startTime, int[] endTime, int[] profit) { + int n = startTime.length; + intervals = new int[n][3]; + cache = new int[n]; + Arrays.fill(cache, -1); + + for (int i = 0; i < n; i++) { + intervals[i] = new int[]{startTime[i], endTime[i], profit[i]}; + } + Arrays.sort(intervals, Comparator.comparingInt(a -> a[0])); + + return dfs(0); + } + + private int dfs(int i) { + if (i == intervals.length) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + // Don't include + int res = dfs(i + 1); + + // Include + int j = i + 1; + while (j < intervals.length && intervals[i][1] > intervals[j][0]) { + j++; + } + + return cache[i] = Math.max(res, intervals[i][2] + dfs(j)); + } +} +``` + +```cpp +class Solution { +public: + vector> intervals; + vector cache; + + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + int n = startTime.size(); + intervals.resize(n, vector(3)); + cache.assign(n, -1); + + for (int i = 0; i < n; i++) { + intervals[i] = {startTime[i], endTime[i], profit[i]}; + } + sort(intervals.begin(), intervals.end()); + + return dfs(0); + } + +private: + int dfs(int i) { + if (i == intervals.size()) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + // Don't include + int res = dfs(i + 1); + + // Include + int j = i + 1; + while (j < intervals.size() && intervals[i][1] > intervals[j][0]) { + j++; + } + + return cache[i] = max(res, intervals[i][2] + dfs(j)); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} startTime + * @param {number[]} endTime + * @param {number[]} profit + * @return {number} + */ + jobScheduling(startTime, endTime, profit) { + let n = startTime.length; + let intervals = new Array(n).fill(null).map((_, i) => + [startTime[i], endTime[i], profit[i]] + ); + intervals.sort((a, b) => a[0] - b[0]); + + let cache = new Array(n).fill(-1); + + const dfs = (i) => { + if (i === n) { + return 0; + } + if (cache[i] !== -1) { + return cache[i]; + } + + // Don't include + let res = dfs(i + 1); + + // Include + let j = i + 1; + while (j < n && intervals[i][1] > intervals[j][0]) { + j++; + } + + return (cache[i] = Math.max(res, intervals[i][2] + dfs(j))); + }; + + return dfs(0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n ^ 2)$ +* Space complexity: $O(n)$ + +--- + +## 2. Dynamic Programming (Top-Down) + Binary Search + +::tabs-start + +```python +class Solution: + def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int: + intervals = sorted(zip(startTime, endTime, profit)) + cache = {} + + def dfs(i): + if i == len(intervals): + return 0 + if i in cache: + return cache[i] + + # don't include + res = dfs(i + 1) + + # include + j = bisect.bisect(intervals, (intervals[i][1], -1, -1)) + cache[i] = res = max(res, intervals[i][2] + dfs(j)) + return res + + return dfs(0) +``` + +```java +public class Solution { + private int[][] intervals; + private int[] cache; + + public int jobScheduling(int[] startTime, int[] endTime, int[] profit) { + int n = startTime.length; + intervals = new int[n][3]; + cache = new int[n]; + Arrays.fill(cache, -1); + + for (int i = 0; i < n; i++) { + intervals[i] = new int[]{startTime[i], endTime[i], profit[i]}; + } + Arrays.sort(intervals, Comparator.comparingInt(a -> a[0])); + + return dfs(0); + } + + private int dfs(int i) { + if (i == intervals.length) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + int res = dfs(i + 1); + + int left = i + 1, right = intervals.length, j = intervals.length; + while (left < right) { + int mid = left + (right - left) / 2; + if (intervals[mid][0] >= intervals[i][1]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return cache[i] = Math.max(res, intervals[i][2] + dfs(j)); + } +} +``` + +```cpp +class Solution { +public: + vector> intervals; + vector cache; + + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + int n = startTime.size(); + intervals.resize(n, vector(3)); + cache.assign(n, -1); + + for (int i = 0; i < n; i++) { + intervals[i] = {startTime[i], endTime[i], profit[i]}; + } + sort(intervals.begin(), intervals.end()); + + return dfs(0); + } + +private: + int dfs(int i) { + if (i == intervals.size()) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + int res = dfs(i + 1); + + int left = i + 1, right = intervals.size(), j = intervals.size(); + while (left < right) { + int mid = left + (right - left) / 2; + if (intervals[mid][0] >= intervals[i][1]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return cache[i] = max(res, intervals[i][2] + dfs(j)); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} startTime + * @param {number[]} endTime + * @param {number[]} profit + * @return {number} + */ + jobScheduling(startTime, endTime, profit) { + let n = startTime.length; + let intervals = new Array(n).fill(null).map((_, i) => + [startTime[i], endTime[i], profit[i]] + ); + intervals.sort((a, b) => a[0] - b[0]); + + let cache = new Array(n).fill(-1); + + const dfs = (i) => { + if (i === n) { + return 0; + } + if (cache[i] !== -1) { + return cache[i]; + } + + let res = dfs(i + 1); + + let left = i + 1, right = n, j = n; + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (intervals[mid][0] >= intervals[i][1]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return (cache[i] = Math.max(res, intervals[i][2] + dfs(j))); + }; + + return dfs(0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Dynamic Programming (Top-Down) + Binary Search (Optimal) + +::tabs-start + +```python +class Solution: + def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int: + n = len(startTime) + index = list(range(n)) + index.sort(key=lambda i: startTime[i]) + + cache = [-1] * n + + def dfs(i): + if i == n: + return 0 + if cache[i] != -1: + return cache[i] + + res = dfs(i + 1) + + left, right, j = i + 1, n, n + while left < right: + mid = (left + right) // 2 + if startTime[index[mid]] >= endTime[index[i]]: + j = mid + right = mid + else: + left = mid + 1 + + cache[i] = res = max(res, profit[index[i]] + dfs(j)) + return res + + return dfs(0) +``` + +```java +public class Solution { + private int[] startTime, endTime, profit, cache; + private Integer[] index; + private int n; + + public int jobScheduling(int[] startTime, int[] endTime, int[] profit) { + this.n = startTime.length; + this.startTime = startTime; + this.endTime = endTime; + this.profit = profit; + this.index = new Integer[n]; + this.cache = new int[n]; + Arrays.fill(cache, -1); + + for (int i = 0; i < n; i++) { + index[i] = i; + } + Arrays.sort(index, Comparator.comparingInt(i -> startTime[i])); + + return dfs(0); + } + + private int dfs(int i) { + if (i == n) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + int res = dfs(i + 1); + + int left = i + 1, right = n, j = n; + while (left < right) { + int mid = left + (right - left) / 2; + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return cache[i] = Math.max(res, profit[index[i]] + dfs(j)); + } +} +``` + +```cpp +class Solution { +public: + vector startTime, endTime, profit, index, cache; + int n; + + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + this->n = startTime.size(); + this->startTime = startTime; + this->endTime = endTime; + this->profit = profit; + this->index.resize(n); + this->cache.assign(n, -1); + + for (int i = 0; i < n; i++) { + index[i] = i; + } + sort(index.begin(), index.end(), [&](int i, int j) { + return startTime[i] < startTime[j]; + }); + + return dfs(0); + } + +private: + int dfs(int i) { + if (i == n) { + return 0; + } + if (cache[i] != -1) { + return cache[i]; + } + + int res = dfs(i + 1); + + int left = i + 1, right = n, j = n; + while (left < right) { + int mid = left + (right - left) / 2; + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return cache[i] = max(res, profit[index[i]] + dfs(j)); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} startTime + * @param {number[]} endTime + * @param {number[]} profit + * @return {number} + */ + jobScheduling(startTime, endTime, profit) { + let n = startTime.length; + let index = Array.from({ length: n }, (_, i) => i); + index.sort((a, b) => startTime[a] - startTime[b]); + + let cache = new Array(n).fill(-1); + + const dfs = (i) => { + if (i === n) { + return 0; + } + if (cache[i] !== -1) { + return cache[i]; + } + + let res = dfs(i + 1); + + let left = i + 1, right = n, j = n; + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + + return (cache[i] = Math.max(res, profit[index[i]] + dfs(j))); + }; + + return dfs(0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Dynamic Programming (Bottom-Up) + Binary Search + +::tabs-start + +```python +class Solution: + def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int: + n = len(startTime) + index = list(range(n)) + index.sort(key=lambda i: startTime[i]) + + dp = [0] * (n + 1) + + for i in range(n - 1, -1, -1): + left, right, j = i + 1, n, n + while left < right: + mid = (left + right) // 2 + if startTime[index[mid]] >= endTime[index[i]]: + j = mid + right = mid + else: + left = mid + 1 + + dp[i] = max(dp[i + 1], profit[index[i]] + dp[j]) + + return dp[0] +``` + +```java +public class Solution { + public int jobScheduling(int[] startTime, int[] endTime, int[] profit) { + int n = startTime.length; + Integer[] index = new Integer[n]; + for (int i = 0; i < n; i++) index[i] = i; + Arrays.sort(index, Comparator.comparingInt(i -> startTime[i])); + + int[] dp = new int[n + 1]; + + for (int i = n - 1; i >= 0; i--) { + int left = i + 1, right = n, j = n; + while (left < right) { + int mid = left + (right - left) / 2; + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + dp[i] = Math.max(dp[i + 1], profit[index[i]] + dp[j]); + } + + return dp[0]; + } +} +``` + +```cpp +class Solution { +public: + int jobScheduling(vector& startTime, vector& endTime, vector& profit) { + int n = startTime.size(); + vector index(n), dp(n + 1, 0); + for (int i = 0; i < n; i++) index[i] = i; + sort(index.begin(), index.end(), [&](int i, int j) { + return startTime[i] < startTime[j]; + }); + + for (int i = n - 1; i >= 0; i--) { + int left = i + 1, right = n, j = n; + while (left < right) { + int mid = left + (right - left) / 2; + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + dp[i] = max(dp[i + 1], profit[index[i]] + dp[j]); + } + + return dp[0]; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} startTime + * @param {number[]} endTime + * @param {number[]} profit + * @return {number} + */ + jobScheduling(startTime, endTime, profit) { + let n = startTime.length; + let index = Array.from({ length: n }, (_, i) => i); + index.sort((a, b) => startTime[a] - startTime[b]); + + let dp = new Array(n + 1).fill(0); + + for (let i = n - 1; i >= 0; i--) { + let left = i + 1, right = n, j = n; + while (left < right) { + let mid = Math.floor((left + right) / 2); + if (startTime[index[mid]] >= endTime[index[i]]) { + j = mid; + right = mid; + } else { + left = mid + 1; + } + } + dp[i] = Math.max(dp[i + 1], profit[index[i]] + dp[j]); + } + + return dp[0]; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/maximum-subsequence-score.md b/articles/maximum-subsequence-score.md new file mode 100644 index 000000000..97c461caa --- /dev/null +++ b/articles/maximum-subsequence-score.md @@ -0,0 +1,412 @@ +## 1. Brute Force (Recursion) + +::tabs-start + +```python +class Solution: + def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int: + n = len(nums1) + + def dfs(i, k, minVal, curSum): + if k == 0: + return curSum * minVal + if i == n or (n - i) < k: + return float("-inf") + if minVal == 0: + return 0 + + res = dfs(i + 1, k, minVal, curSum) + res = max(res, dfs(i + 1, k - 1, min(minVal, nums2[i]), curSum + nums1[i])) + return res + + return dfs(0, k, float("inf"), 0) +``` + +```java +public class Solution { + private int[] nums1, nums2; + private int n; + + public long maxScore(int[] nums1, int[] nums2, int k) { + this.nums1 = nums1; + this.nums2 = nums2; + this.n = nums1.length; + return dfs(0, k, Integer.MAX_VALUE, 0); + } + + private long dfs(int i, int k, int minVal, long curSum) { + if (k == 0) { + return curSum * minVal; + } + if (i == n || (n - i) < k) { + return Integer.MIN_VALUE; + } + if (minVal == 0) { + return 0; + } + + long res = dfs(i + 1, k, minVal, curSum); + res = Math.max( + res, + dfs(i + 1, k - 1, Math.min(minVal, nums2[i]), curSum + nums1[i]) + ); + return res; + } +} +``` + +```cpp +class Solution { +private: + vector nums1, nums2; + int n; + +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + this->nums1 = nums1; + this->nums2 = nums2; + this->n = nums1.size(); + return dfs(0, k, INT_MAX, 0); + } + +private: + long long dfs(int i, int k, int minVal, long long curSum) { + if (k == 0) { + return curSum * minVal; + } + if (i == n || (n - i) < k) { + return INT_MIN; + } + if (minVal == 0) { + return 0; + } + + long long res = dfs(i + 1, k, minVal, curSum); + res = max(res, dfs(i + 1, k - 1, min(minVal, nums2[i]), curSum + nums1[i])); + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @param {number} k + * @return {number} + */ + maxScore(nums1, nums2, k) { + const n = nums1.length; + + const dfs = (i, k, minVal, curSum) => { + if (k === 0) { + return curSum * minVal; + } + if (i === n || (n - i) < k) { + return -Infinity; + } + if (minVal === 0) { + return 0; + } + + let res = dfs(i + 1, k, minVal, curSum); + res = Math.max( + res, + dfs(i + 1, k - 1, Math.min(minVal, nums2[i]), curSum + nums1[i]) + ); + return res; + }; + + return dfs(0, k, Infinity, 0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(2 ^ n)$ +* Space complexity: $O(n)$ for recursion stack. + +--- + +## 2. Min-Heap - I + +::tabs-start + +```python +class Solution: + def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int: + pairs = sorted(zip(nums1, nums2), key=lambda p: p[1], reverse=True) + + minHeap = [] + n1Sum = 0 + res = 0 + + for n1, n2 in pairs: + n1Sum += n1 + heapq.heappush(minHeap, n1) + + if len(minHeap) > k: + n1Sum -= heapq.heappop(minHeap) + + if len(minHeap) == k: + res = max(res, n1Sum * n2) + + return res +``` + +```java +public class Solution { + public long maxScore(int[] nums1, int[] nums2, int k) { + int n = nums1.length; + int[][] pairs = new int[n][2]; + + for (int i = 0; i < n; i++) { + pairs[i][0] = nums1[i]; + pairs[i][1] = nums2[i]; + } + + Arrays.sort(pairs, (a, b) -> Integer.compare(b[1], a[1])); + + PriorityQueue minHeap = new PriorityQueue<>(); + long n1Sum = 0, res = 0; + + for (int[] pair : pairs) { + n1Sum += pair[0]; + minHeap.offer(pair[0]); + + if (minHeap.size() > k) { + n1Sum -= minHeap.poll(); + } + + if (minHeap.size() == k) { + res = Math.max(res, n1Sum * pair[1]); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + int n = nums1.size(); + vector> pairs(n); + + for (int i = 0; i < n; i++) { + pairs[i] = {nums1[i], nums2[i]}; + } + + sort(pairs.begin(), pairs.end(), [](const auto& a, const auto& b) { + return b.second < a.second; + }); + + priority_queue, greater> minHeap; + long long n1Sum = 0, res = 0; + + for (auto& pair : pairs) { + n1Sum += pair.first; + minHeap.push(pair.first); + + if (minHeap.size() > k) { + n1Sum -= minHeap.top(); + minHeap.pop(); + } + + if (minHeap.size() == k) { + res = max(res, n1Sum * (long long)pair.second); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @param {number} k + * @return {number} + */ + maxScore(nums1, nums2, k) { + let pairs = nums1.map((n1, i) => [n1, nums2[i]]); + pairs.sort((a, b) => b[1] - a[1]); + + let minHeap = new MinPriorityQueue(); + let n1Sum = 0, res = 0; + + for (let [n1, n2] of pairs) { + n1Sum += n1; + minHeap.enqueue(n1); + + if (minHeap.size() > k) { + n1Sum -= minHeap.dequeue(); + } + + if (minHeap.size() === k) { + res = Math.max(res, n1Sum * n2); + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Min-Heap - II + +::tabs-start + +```python +class Solution: + def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int: + n = len(nums1) + arr = [(nums2[i] << 30) | nums1[i] for i in range(n)] + arr.sort(reverse=True) + + minHeap = [] + n1Sum = 0 + res = 0 + + for num in arr: + n1, n2 = num & ((1 << 30) - 1), num >> 30 + n1Sum += n1 + heapq.heappush(minHeap, n1) + + if len(minHeap) > k: + n1Sum -= heapq.heappop(minHeap) + + if len(minHeap) == k: + res = max(res, n1Sum * n2) + + return res +``` + +```java +public class Solution { + public long maxScore(int[] nums1, int[] nums2, int k) { + int n = nums1.length; + long[] arr = new long[n]; + for (int i = 0; i < n; i++) { + arr[i] = ((long) nums2[i] << 30) | nums1[i]; + } + + Arrays.sort(arr); + PriorityQueue minHeap = new PriorityQueue<>(); + long n1Sum = 0, res = 0; + + for (int i = n - 1; i >= 0; i--) { + int n1 = (int) (arr[i] & ((1L << 30) - 1)); + int n2 = (int) (arr[i] >> 30); + n1Sum += n1; + minHeap.offer(n1); + + if (minHeap.size() > k) { + n1Sum -= minHeap.poll(); + } + if (minHeap.size() == k) { + res = Math.max(res, n1Sum * (long) n2); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + long long maxScore(vector& nums1, vector& nums2, int k) { + int n = nums1.size(); + vector arr(n); + for (int i = 0; i < n; i++) { + arr[i] = ((long long) nums2[i] << 30) | nums1[i]; + } + + sort(arr.rbegin(), arr.rend()); + priority_queue, greater> minHeap; + long long n1Sum = 0, res = 0; + + for (long long& num : arr) { + int n1 = num & ((1LL << 30) - 1); + int n2 = num >> 30; + n1Sum += n1; + minHeap.push(n1); + + if (minHeap.size() > k) { + n1Sum -= minHeap.top(); + minHeap.pop(); + } + if (minHeap.size() == k) { + res = max(res, n1Sum * (long long)n2); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @param {number} k + * @return {number} + */ + maxScore(nums1, nums2, k) { + const n = nums1.length; + const arr = []; + for (let i = 0; i < n; i++) { + arr.push((BigInt(nums2[i]) << BigInt(30)) | BigInt(nums1[i])); + } + + arr.sort((a, b) => Number(b - a)); + const minHeap = new MinPriorityQueue(); + let n1Sum = 0n, res = 0n; + + for (let num of arr) { + let n1 = Number(num & ((1n << 30n) - 1n)); + let n2 = Number(num >> 30n); + n1Sum += BigInt(n1); + minHeap.enqueue(n1); + + if (minHeap.size() > k) { + n1Sum -= BigInt(minHeap.dequeue()); + } + if (minHeap.size() === k) { + res = BigInt(Math.max(Number(res), Number(n1Sum * BigInt(n2)))); + } + } + + return Number(res); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n \log n)$ +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/minimize-deviation-in-array.md b/articles/minimize-deviation-in-array.md new file mode 100644 index 000000000..34f231a21 --- /dev/null +++ b/articles/minimize-deviation-in-array.md @@ -0,0 +1,468 @@ +## 1. Sorting + Sliding Window + +::tabs-start + +```python +class Solution: + def minimumDeviation(self, nums: List[int]) -> int: + n = len(nums) + arr = [] + for i, num in enumerate(nums): + if num & 1: + arr.append((num, i)) + arr.append((num * 2, i)) + else: + while num % 2 == 0: + arr.append((num, i)) + num //= 2 + arr.append((num, i)) + + arr.sort() + res = float("inf") + + seen = [0] * n + count = i = 0 + for j in range(len(arr)): + seen[arr[j][1]] += 1 + if seen[arr[j][1]] == 1: + count += 1 + while count == n: + res = min(res, arr[j][0] - arr[i][0]) + seen[arr[i][1]] -= 1 + if seen[arr[i][1]] == 0: + count -= 1 + i += 1 + + return res +``` + +```java +public class Solution { + public int minimumDeviation(int[] nums) { + int n = nums.length; + List arr = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + int num = nums[i]; + if (num % 2 == 1) { + arr.add(new int[]{num, i}); + arr.add(new int[]{num * 2, i}); + } else { + while (num % 2 == 0) { + arr.add(new int[]{num, i}); + num /= 2; + } + arr.add(new int[]{num, i}); + } + } + + arr.sort(Comparator.comparingInt(a -> a[0])); + int res = Integer.MAX_VALUE; + + int[] seen = new int[n]; + int count = 0, i = 0; + + for (int j = 0; j < arr.size(); j++) { + seen[arr.get(j)[1]]++; + if (seen[arr.get(j)[1]] == 1) { + count++; + while (count == n) { + res = Math.min(res, arr.get(j)[0] - arr.get(i)[0]); + seen[arr.get(i)[1]]--; + if (seen[arr.get(i)[1]] == 0) { + count--; + } + i++; + } + } + } + return res; + } +} +``` + +```cpp +class Solution { +public: + int minimumDeviation(vector& nums) { + int n = nums.size(); + vector> arr; + + for (int i = 0; i < n; i++) { + int num = nums[i]; + if (num % 2 == 1) { + arr.emplace_back(num, i); + arr.emplace_back(num * 2, i); + } else { + while (num % 2 == 0) { + arr.emplace_back(num, i); + num /= 2; + } + arr.emplace_back(num, i); + } + } + + sort(arr.begin(), arr.end()); + int res = INT_MAX; + + vector seen(n, 0); + int count = 0, i = 0; + + for (int j = 0; j < arr.size(); j++) { + seen[arr[j].second]++; + if (seen[arr[j].second] == 1) { + count++; + while (count == n) { + res = min(res, arr[j].first - arr[i].first); + seen[arr[i].second]--; + if (seen[arr[i].second] == 0) { + count--; + } + i++; + } + } + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + minimumDeviation(nums) { + let n = nums.length; + let arr = []; + + for (let i = 0; i < n; i++) { + let num = nums[i]; + if (num % 2 === 1) { + arr.push([num, i]); + arr.push([num * 2, i]); + } else { + while (num % 2 === 0) { + arr.push([num, i]); + num /= 2; + } + arr.push([num, i]); + } + } + + arr.sort((a, b) => a[0] - b[0]); + let res = Infinity; + + let seen = new Array(n).fill(0); + let count = 0, i = 0; + + for (let j = 0; j < arr.length; j++) { + seen[arr[j][1]]++; + if (seen[arr[j][1]] === 1) { + count++; + while (count === n) { + res = Math.min(res, arr[j][0] - arr[i][0]); + seen[arr[i][1]]--; + if (seen[arr[i][1]] === 0) { + count--; + } + i++; + } + } + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O((n \log m) * \log (n \log m))$ +* Space complexity: $O(n \log m)$ + +> Where $n$ is the size of the array $nums$ and $m$ is the maximum element in $nums$. + +--- + +## 2. Min-Heap + +::tabs-start + +```python +class Solution: + def minimumDeviation(self, nums: List[int]) -> int: + minHeap, heapMax = [], 0 + + for n in nums: + tmp = n + while n % 2 == 0: + n //= 2 + minHeap.append((n, max(tmp, 2 * n))) + heapMax = max(heapMax, n) + + res = float("inf") + heapq.heapify(minHeap) + + while len(minHeap) == len(nums): + n, nMax = heapq.heappop(minHeap) + res = min(res, heapMax - n) + + if n < nMax: + heapq.heappush(minHeap, (n * 2, nMax)) + heapMax = max(heapMax, n * 2) + + return res +``` + +```java +public class Solution { + public int minimumDeviation(int[] nums) { + PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]); + int heapMax = 0; + + for (int num : nums) { + int tmp = num; + while (num % 2 == 0) { + num /= 2; + } + minHeap.offer(new int[]{num, Math.max(tmp, 2 * num)}); + heapMax = Math.max(heapMax, num); + } + + int res = Integer.MAX_VALUE; + + while (minHeap.size() == nums.length) { + int[] minElement = minHeap.poll(); + int n = minElement[0], nMax = minElement[1]; + res = Math.min(res, heapMax - n); + + if (n < nMax) { + minHeap.offer(new int[]{n * 2, nMax}); + heapMax = Math.max(heapMax, n * 2); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int minimumDeviation(vector& nums) { + priority_queue, vector>, greater<>> minHeap; + int heapMax = 0; + + for (int num : nums) { + int tmp = num; + while (num % 2 == 0) { + num /= 2; + } + minHeap.push({num, max(tmp, 2 * num)}); + heapMax = max(heapMax, num); + } + + int res = INT_MAX; + + while (minHeap.size() == nums.size()) { + auto [n, nMax] = minHeap.top(); + minHeap.pop(); + res = min(res, heapMax - n); + + if (n < nMax) { + minHeap.push({n * 2, nMax}); + heapMax = max(heapMax, n * 2); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + minimumDeviation(nums) { + const minHeap = new MinPriorityQueue(x => x[0]); + let heapMax = 0; + + for (let num of nums) { + let tmp = num; + while (num % 2 === 0) { + num /= 2; + } + minHeap.enqueue([num, Math.max(tmp, num * 2)]); + heapMax = Math.max(heapMax, num); + } + + let res = Infinity; + + while (minHeap.size() === nums.length) { + let [n, nMax] = minHeap.dequeue(); + res = Math.min(res, heapMax - n); + + if (n < nMax) { + minHeap.enqueue([n * 2, nMax]); + heapMax = Math.max(heapMax, n * 2); + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n *\log n * \log m)$ +* Space complexity: $O(n)$ + +> Where $n$ is the size of the array $nums$ and $m$ is the maximum element in $nums$. + +--- + +## 3. Max-Heap + +::tabs-start + +```python +class Solution: + def minimumDeviation(self, nums: List[int]) -> int: + maxHeap = [] + minVal = float("inf") + + for num in nums: + if num % 2 == 1: + num *= 2 + heapq.heappush(maxHeap, -num) + minVal = min(minVal, num) + + res = float("inf") + + while maxHeap: + maxVal = -heapq.heappop(maxHeap) + res = min(res, maxVal - minVal) + if maxVal % 2 == 1: + break + + nextVal = maxVal // 2 + heapq.heappush(maxHeap, -nextVal) + minVal = min(minVal, nextVal) + + return res +``` + +```java +public class Solution { + public int minimumDeviation(int[] nums) { + PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); + int minVal = Integer.MAX_VALUE; + + for (int num : nums) { + if (num % 2 == 1) num *= 2; + maxHeap.offer(num); + minVal = Math.min(minVal, num); + } + + int res = Integer.MAX_VALUE; + + while (!maxHeap.isEmpty()) { + int maxVal = maxHeap.poll(); + res = Math.min(res, maxVal - minVal); + + if (maxVal % 2 == 1) break; + + int nextVal = maxVal / 2; + maxHeap.offer(nextVal); + minVal = Math.min(minVal, nextVal); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + int minimumDeviation(vector& nums) { + priority_queue maxHeap; + int minVal = INT_MAX; + + for (int num : nums) { + if (num % 2 == 1) num *= 2; + maxHeap.push(num); + minVal = min(minVal, num); + } + + int res = INT_MAX; + + while (!maxHeap.empty()) { + int maxVal = maxHeap.top(); + maxHeap.pop(); + res = min(res, maxVal - minVal); + + if (maxVal % 2 == 1) break; + + int nextVal = maxVal / 2; + maxHeap.push(nextVal); + minVal = min(minVal, nextVal); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} nums + * @return {number} + */ + minimumDeviation(nums) { + const maxHeap = new MaxPriorityQueue(); + let minVal = Infinity; + + for (let num of nums) { + if (num % 2 === 1) num *= 2; + maxHeap.enqueue(num); + minVal = Math.min(minVal, num); + } + + let res = Infinity; + + while (!maxHeap.isEmpty()) { + let maxVal = maxHeap.dequeue(); + res = Math.min(res, maxVal - minVal); + + if (maxVal % 2 === 1) break; + + let nextVal = maxVal / 2; + maxHeap.enqueue(nextVal); + minVal = Math.min(minVal, nextVal); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n *\log n * \log m)$ +* Space complexity: $O(n)$ + +> Where $n$ is the size of the array $nums$ and $m$ is the maximum element in $nums$. \ No newline at end of file diff --git a/articles/minimum-cost-to-hire-k-workers.md b/articles/minimum-cost-to-hire-k-workers.md new file mode 100644 index 000000000..f205b01e7 --- /dev/null +++ b/articles/minimum-cost-to-hire-k-workers.md @@ -0,0 +1,144 @@ +## 1. Greedy + Max-Heap + +::tabs-start + +```python +class Solution: + def mincostToHireWorkers(self, quality: List[int], wage: List[int], k: int) -> float: + pairs = sorted([(w / q, q) for q, w in zip(quality, wage)], key=lambda p: p[0]) + + maxHeap = [] + total_quality = 0 + res = float("inf") + + for rate, q in pairs: + heapq.heappush(maxHeap, -q) + total_quality += q + + if len(maxHeap) > k: + total_quality += heapq.heappop(maxHeap) + + if len(maxHeap) == k: + res = min(res, total_quality * rate) + + return res +``` + +```java +public class Solution { + public double mincostToHireWorkers(int[] quality, int[] wage, int k) { + int n = quality.length; + double res = Double.MAX_VALUE; + double totalQuality = 0; + + double[][] workers = new double[n][2]; + for (int i = 0; i < n; i++) { + workers[i] = new double[]{ + (double) wage[i] / quality[i], (double) quality[i] + }; + } + + Arrays.sort(workers, Comparator.comparingDouble(a -> a[0])); + PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); + + for (double[] worker : workers) { + double ratio = worker[0]; + int q = (int) worker[1]; + + maxHeap.add(q); + totalQuality += q; + + if (maxHeap.size() > k) { + totalQuality -= maxHeap.poll(); + } + + if (maxHeap.size() == k) { + res = Math.min(res, totalQuality * ratio); + } + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + double mincostToHireWorkers(vector& quality, vector& wage, int k) { + int n = quality.size(); + vector> workers(n); + for (int i = 0; i < n; i++) { + workers[i] = { (double)wage[i] / quality[i], quality[i] }; + } + + sort(workers.begin(), workers.end()); + priority_queue maxHeap; + double totalQuality = 0, res = DBL_MAX; + + for (auto& worker : workers) { + double ratio = worker.first; + int q = worker.second; + maxHeap.push(q); + totalQuality += q; + + if (maxHeap.size() > k) { + totalQuality -= maxHeap.top(); + maxHeap.pop(); + } + + if (maxHeap.size() == k) { + res = min(res, totalQuality * ratio); + } + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} quality + * @param {number[]} wage + * @param {number} k + * @return {number} + */ + mincostToHireWorkers(quality, wage, k) { + const n = quality.length; + const workers = []; + for (let i = 0; i < n; i++) { + workers.push([wage[i] / quality[i], quality[i]]); + } + + workers.sort((a, b) => a[0] - b[0]); + const maxHeap = new MaxPriorityQueue(); + let totalQuality = 0, res = Number.MAX_VALUE; + + for (let [ratio, q] of workers) { + maxHeap.enqueue(q); + totalQuality += q; + + if (maxHeap.size() > k) { + totalQuality -= maxHeap.dequeue(); + } + + if (maxHeap.size() === k) { + res = Math.min(res, totalQuality * ratio); + } + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n * (\log n + \log k))$ +* Space complexity: $O(n)$ + +> Where $n$ is the number of workers, and $k$ is the number of workers to be hired. \ No newline at end of file diff --git a/articles/number-of-flowers-in-full-bloom.md b/articles/number-of-flowers-in-full-bloom.md new file mode 100644 index 000000000..2b60ec927 --- /dev/null +++ b/articles/number-of-flowers-in-full-bloom.md @@ -0,0 +1,675 @@ +## 1. Brute Force + +::tabs-start + +```python +class Solution: + def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + res = [] + + for time in people: + cnt = 0 + for start, end in flowers: + if start <= time <= end: + cnt += 1 + res.append(cnt) + + return res +``` + +```java +public class Solution { + public int[] fullBloomFlowers(int[][] flowers, int[] people) { + int m = people.length; + int[] res = new int[m]; + + for (int i = 0; i < m; i++) { + int count = 0; + for (int[] flower : flowers) { + if (flower[0] <= people[i] && people[i] <= flower[1]) { + count++; + } + } + res[i] = count; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& people) { + int m = people.size(); + vector res(m); + + for (int i = 0; i < m; i++) { + int count = 0; + for (auto& flower : flowers) { + if (flower[0] <= people[i] && people[i] <= flower[1]) { + count++; + } + } + res[i] = count; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} flowers + * @param {number[]} people + * @return {number[]} + */ + fullBloomFlowers(flowers, people) { + let res = new Array(people.length).fill(0); + + for (let i = 0; i < people.length; i++) { + let count = 0; + for (let [start, end] of flowers) { + if (start <= people[i] && people[i] <= end) { + count++; + } + } + res[i] = count; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * n)$ +* Space complexity: $O(m)$ for the output array. + +> Where $n$ is the size of the array $flowers$, and $m$ is the size of the array $people$. + +--- + +## 2. Two Min-Heaps + +::tabs-start + +```python +class Solution: + def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + people = sorted((p, i) for i, p in enumerate(people)) + res = [0] * len(people) + count = 0 + + start = [f[0] for f in flowers] + end = [f[1] for f in flowers] + + heapq.heapify(start) + heapq.heapify(end) + + for p, i in people: + while start and start[0] <= p: + heapq.heappop(start) + count += 1 + while end and end[0] < p: + heapq.heappop(end) + count -= 1 + res[i] = count + + return res +``` + +```java +public class Solution { + public int[] fullBloomFlowers(int[][] flowers, int[] people) { + int m = people.length; + int[] res = new int[m]; + + List sortedPeople = new ArrayList<>(); + for (int i = 0; i < m; i++) { + sortedPeople.add(new int[]{people[i], i}); + } + sortedPeople.sort(Comparator.comparingInt(a -> a[0])); + + PriorityQueue startHeap = new PriorityQueue<>(); + PriorityQueue endHeap = new PriorityQueue<>(); + for (int[] f : flowers) { + startHeap.offer(f[0]); + endHeap.offer(f[1]); + } + + int count = 0; + for (int[] person : sortedPeople) { + int p = person[0], index = person[1]; + + while (!startHeap.isEmpty() && startHeap.peek() <= p) { + startHeap.poll(); + count++; + } + while (!endHeap.isEmpty() && endHeap.peek() < p) { + endHeap.poll(); + count--; + } + + res[index] = count; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& people) { + int m = people.size(); + vector res(m); + + vector> sortedPeople; + for (int i = 0; i < m; i++) { + sortedPeople.push_back({people[i], i}); + } + sort(sortedPeople.begin(), sortedPeople.end()); + + priority_queue, greater> startHeap, endHeap; + for (const auto& f : flowers) { + startHeap.push(f[0]); + endHeap.push(f[1]); + } + + int count = 0; + for (const auto& person : sortedPeople) { + int p = person.first, index = person.second; + + while (!startHeap.empty() && startHeap.top() <= p) { + startHeap.pop(); + count++; + } + while (!endHeap.empty() && endHeap.top() < p) { + endHeap.pop(); + count--; + } + + res[index] = count; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} flowers + * @param {number[]} people + * @return {number[]} + */ + fullBloomFlowers(flowers, people) { + const m = people.length; + const res = new Array(m).fill(0); + + const sortedPeople = people.map((p, i) => [p, i]); + sortedPeople.sort((a, b) => a[0] - b[0]); + + const startHeap = new MinPriorityQueue(); + const endHeap = new MinPriorityQueue(); + for (const [s, e] of flowers) { + startHeap.enqueue(s); + endHeap.enqueue(e); + } + + let count = 0; + for (const [p, index] of sortedPeople) { + while (!startHeap.isEmpty() && startHeap.front() <= p) { + startHeap.dequeue(); + count++; + } + while (!endHeap.isEmpty() && endHeap.front() < p) { + endHeap.dequeue(); + count--; + } + res[index] = count; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m \log m + n \log n)$ +* Space complexity: $O(m + n)$ + +> Where $n$ is the size of the array $flowers$, and $m$ is the size of the array $people$. + +--- + +## 3. Min-Heap + +::tabs-start + +```python +class Solution: + def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + people = sorted((p, i) for i, p in enumerate(people)) + res = [0] * len(people) + flowers.sort() + end = [] + + j = 0 + for p, i in people: + while j < len(flowers) and flowers[j][0] <= p: + heapq.heappush(end, flowers[j][1]) + j += 1 + while end and end[0] < p: + heapq.heappop(end) + res[i] = len(end) + + return res +``` + +```java +public class Solution { + public int[] fullBloomFlowers(int[][] flowers, int[] people) { + int m = people.length; + int[] res = new int[m]; + int[][] indexedPeople = new int[m][2]; + + for (int i = 0; i < m; i++) { + indexedPeople[i] = new int[]{people[i], i}; + } + Arrays.sort(indexedPeople, Comparator.comparingInt(a -> a[0])); + Arrays.sort(flowers, Comparator.comparingInt(a -> a[0])); + + PriorityQueue endHeap = new PriorityQueue<>(); + int j = 0, n = flowers.length; + + for (int[] person : indexedPeople) { + int p = person[0], index = person[1]; + + while (j < n && flowers[j][0] <= p) { + endHeap.offer(flowers[j][1]); + j++; + } + while (!endHeap.isEmpty() && endHeap.peek() < p) { + endHeap.poll(); + } + res[index] = endHeap.size(); + } + return res; + } +} +``` + +```cpp +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& people) { + int m = people.size(); + vector res(m); + vector> indexedPeople; + + for (int i = 0; i < m; i++) { + indexedPeople.emplace_back(people[i], i); + } + sort(indexedPeople.begin(), indexedPeople.end()); + sort(flowers.begin(), flowers.end()); + + priority_queue, greater> endHeap; + int j = 0, n = flowers.size(); + + for (auto [p, index] : indexedPeople) { + while (j < n && flowers[j][0] <= p) { + endHeap.push(flowers[j][1]); + j++; + } + while (!endHeap.empty() && endHeap.top() < p) { + endHeap.pop(); + } + res[index] = endHeap.size(); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} flowers + * @param {number[]} people + * @return {number[]} + */ + fullBloomFlowers(flowers, people) { + const m = people.length; + const res = new Array(m).fill(0); + const indexedPeople = people.map((p, i) => [p, i]); + + indexedPeople.sort((a, b) => a[0] - b[0]); + flowers.sort((a, b) => a[0] - b[0]); + + const endHeap = new MinPriorityQueue(); + let j = 0, n = flowers.length; + + for (const [p, index] of indexedPeople) { + while (j < n && flowers[j][0] <= p) { + endHeap.enqueue(flowers[j][1]); + j++; + } + while (!endHeap.isEmpty() && endHeap.front() < p) { + endHeap.dequeue(); + } + res[index] = endHeap.size(); + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m \log m + n \log n)$ +* Space complexity: $O(m + n)$ + +> Where $n$ is the size of the array $flowers$, and $m$ is the size of the array $people$. + +--- + +## 4. Sorting + Two Pointers + +::tabs-start + +```python +class Solution: + def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + start = sorted(f[0] for f in flowers) + end = sorted(f[1] for f in flowers) + + res = [0] * len(people) + peopleIndex = sorted((p, i) for i, p in enumerate(people)) + + i = j = count = 0 + for p, index in peopleIndex: + while i < len(start) and start[i] <= p: + count += 1 + i += 1 + while j < len(end) and end[j] < p: + count -= 1 + j += 1 + res[index] = count + + return res +``` + +```java +public class Solution { + public int[] fullBloomFlowers(int[][] flowers, int[] people) { + int m = people.length; + int[] res = new int[m]; + List start = new ArrayList<>(), end = new ArrayList<>(); + for (int[] f : flowers) { + start.add(f[0]); + end.add(f[1]); + } + + Collections.sort(start); + Collections.sort(end); + + int count = 0, i = 0, j = 0; + List peopleIndex = new ArrayList<>(); + for (int k = 0; k < m; k++) { + peopleIndex.add(new int[]{people[k], k}); + } + peopleIndex.sort(Comparator.comparingInt(a -> a[0])); + + for (int[] p : peopleIndex) { + int time = p[0], index = p[1]; + + while (i < start.size() && start.get(i) <= time) { + count++; + i++; + } + while (j < end.size() && end.get(j) < time) { + count--; + j++; + } + res[index] = count; + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& people) { + int m = people.size(); + vector res(m), start, end; + for (auto& f : flowers) { + start.push_back(f[0]); + end.push_back(f[1]); + } + + sort(start.begin(), start.end()); + sort(end.begin(), end.end()); + + int count = 0, i = 0, j = 0; + vector> peopleIndex; + for (int k = 0; k < m; k++) { + peopleIndex.emplace_back(people[k], k); + } + sort(peopleIndex.begin(), peopleIndex.end()); + + for (auto& [p, index] : peopleIndex) { + while (i < start.size() && start[i] <= p) { + count++; + i++; + } + while (j < end.size() && end[j] < p) { + count--; + j++; + } + res[index] = count; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} flowers + * @param {number[]} people + * @return {number[]} + */ + fullBloomFlowers(flowers, people) { + const start = [], end = []; + for (let f of flowers) { + start.push(f[0]); + end.push(f[1]); + } + + start.sort((a, b) => a - b); + end.sort((a, b) => a - b); + + let count = 0, i = 0, j = 0; + const peopleIndex = people.map((p, idx) => [p, idx]); + peopleIndex.sort((a, b) => a[0] - b[0]); + + const res = new Array(people.length); + + for (let [p, index] of peopleIndex) { + while (i < start.length && start[i] <= p) { + count++; + i++; + } + while (j < end.length && end[j] < p) { + count--; + j++; + } + res[index] = count; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m \log m + n \log n)$ +* Space complexity: $O(m + n)$ + +> Where $n$ is the size of the array $flowers$, and $m$ is the size of the array $people$. + +--- + +## 5. Line Sweep + +::tabs-start + +```python +class Solution: + def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]: + events = [] + for start, end in flowers: + events.append((start, 1)) + events.append((end + 1, -1)) + + events.sort() + queries = sorted((p, i) for i, p in enumerate(people)) + res = [0] * len(people) + + count = j = 0 + for time, index in queries: + while j < len(events) and events[j][0] <= time: + count += events[j][1] + j += 1 + res[index] = count + + return res +``` + +```java +public class Solution { + public int[] fullBloomFlowers(int[][] flowers, int[] people) { + List events = new ArrayList<>(); + for (int[] f : flowers) { + events.add(new int[]{f[0], 1}); + events.add(new int[]{f[1] + 1, -1}); + } + + Collections.sort(events, (a, b) -> a[0] - b[0]); + int[][] queries = new int[people.length][2]; + for (int i = 0; i < people.length; i++) { + queries[i] = new int[]{people[i], i}; + } + Arrays.sort(queries, (a, b) -> Integer.compare(a[0], b[0])); + + int[] res = new int[people.length]; + int count = 0, j = 0; + for (int[] query : queries) { + int time = query[0], index = query[1]; + while (j < events.size() && events.get(j)[0] <= time) { + count += events.get(j)[1]; + j++; + } + res[index] = count; + } + return res; + } +} +``` + +```cpp +class Solution { +public: + vector fullBloomFlowers(vector>& flowers, vector& people) { + vector> events; + for (auto& f : flowers) { + events.emplace_back(f[0], 1); + events.emplace_back(f[1] + 1, -1); + } + + sort(events.begin(), events.end()); + vector> queries; + for (int i = 0; i < people.size(); i++) { + queries.emplace_back(people[i], i); + } + + sort(queries.begin(), queries.end()); + vector res(people.size()); + int count = 0, j = 0; + + for (auto& [time, index] : queries) { + while (j < events.size() && events[j].first <= time) { + count += events[j++].second; + } + res[index] = count; + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} flowers + * @param {number[]} people + * @return {number[]} + */ + fullBloomFlowers(flowers, people) { + let events = []; + for (let [start, end] of flowers) { + events.push([start, 1]); + events.push([end + 1, -1]); + } + + events.sort((a, b) => a[0] - b[0]); + let queries = people.map((p, i) => [p, i]).sort((a, b) => a[0] - b[0]); + let res = new Array(people.length).fill(0); + + let count = 0, j = 0; + for (let [time, index] of queries) { + while (j < events.length && events[j][0] <= time) { + count += events[j][1]; + j++; + } + res[index] = count; + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m \log m + n \log n)$ +* Space complexity: $O(m + n)$ + +> Where $n$ is the size of the array $flowers$, and $m$ is the size of the array $people$. \ No newline at end of file diff --git a/articles/paint-house.md b/articles/paint-house.md new file mode 100644 index 000000000..91b9896cd --- /dev/null +++ b/articles/paint-house.md @@ -0,0 +1,445 @@ +## 1. Recursion + +::tabs-start + +```python +class Solution: + def min_cost(self, costs: List[List[int]]) -> int: + n = len(costs) + + def dfs(i, prevColor): + if i == n: + return 0 + + res = float("inf") + for c in range(3): + if c == prevColor: + continue + res = min(res, costs[i][c] + dfs(i + 1, c)) + return res + + return dfs(0, -1) +``` + +```java +public class Solution { + private int[][] costs; + private int n; + + public int minCost(int[][] costs) { + this.costs = costs; + this.n = costs.length; + return dfs(0, -1); + } + + private int dfs(int i, int prevColor) { + if (i == n) { + return 0; + } + + int res = Integer.MAX_VALUE; + for (int c = 0; c < 3; c++) { + if (c == prevColor) { + continue; + } + res = Math.min(res, costs[i][c] + dfs(i + 1, c)); + } + return res; + } +} +``` + +```cpp +class Solution { +private: + vector> costs; + int n; + + int dfs(int i, int prevColor) { + if (i == n) { + return 0; + } + + int res = INT_MAX; + for (int c = 0; c < 3; c++) { + if (c == prevColor) { + continue; + } + res = min(res, costs[i][c] + dfs(i + 1, c)); + } + return res; + } + +public: + int minCost(vector>& costs) { + this->costs = costs; + this->n = costs.size(); + return dfs(0, -1); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} costs + * @return {number} + */ + minCost(costs) { + const n = costs.length; + + const dfs = (i, prevColor) => { + if (i === n) return 0; + + let res = Infinity; + for (let c = 0; c < 3; c++) { + if (c === prevColor) continue; + res = Math.min(res, costs[i][c] + dfs(i + 1, c)); + } + return res; + }; + + return dfs(0, -1); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(2 ^ n)$ +* Space complexity: $O(n)$ for recursion stack. + +--- + +## 2. Dynamic Programming (Top-Down) + +::tabs-start + +```python +class Solution: + def min_cost(self, costs: List[List[int]]) -> int: + n = len(costs) + dp = [[-1] * 4 for _ in range(n)] + + def dfs(i, prevColor): + if i == n: + return 0 + if dp[i][prevColor + 1] != -1: + return dp[i][prevColor + 1] + + res = float("inf") + for c in range(3): + if c == prevColor: + continue + res = min(res, costs[i][c] + dfs(i + 1, c)) + + dp[i][prevColor + 1] = res + return res + + return dfs(0, -1) +``` + +```java +public class Solution { + private int[][] dp; + private int[][] costs; + + public int minCost(int[][] costs) { + int n = costs.length; + this.costs = costs; + this.dp = new int[n][4]; + + for (int i = 0; i < n; i++) { + Arrays.fill(dp[i], -1); + } + + return dfs(0, -1); + } + + private int dfs(int i, int prevColor) { + if (i == costs.length) { + return 0; + } + if (dp[i][prevColor + 1] != -1) { + return dp[i][prevColor + 1]; + } + + int res = Integer.MAX_VALUE; + for (int c = 0; c < 3; c++) { + if (c == prevColor) continue; + res = Math.min(res, costs[i][c] + dfs(i + 1, c)); + } + + return dp[i][prevColor + 1] = res; + } +} +``` + +```cpp +class Solution { +public: + vector> dp; + vector> costs; + + int minCost(vector>& costs) { + int n = costs.size(); + this->costs = costs; + dp.assign(n, vector(4, -1)); + return dfs(0, -1); + } + +private: + int dfs(int i, int prevColor) { + if (i == costs.size()) { + return 0; + } + if (dp[i][prevColor + 1] != -1) { + return dp[i][prevColor + 1]; + } + + int res = INT_MAX; + for (int c = 0; c < 3; c++) { + if (c == prevColor) continue; + res = min(res, costs[i][c] + dfs(i + 1, c)); + } + + return dp[i][prevColor + 1] = res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} costs + * @return {number} + */ + minCost(costs) { + const n = costs.length; + const dp = Array.from({ length: n }, () => Array(4).fill(-1)); + + const dfs = (i, prevColor) => { + if (i === n) { + return 0; + } + if (dp[i][prevColor + 1] !== -1) { + return dp[i][prevColor + 1]; + } + + let res = Infinity; + for (let c = 0; c < 3; c++) { + if (c === prevColor) continue; + res = Math.min(res, costs[i][c] + dfs(i + 1, c)); + } + + return (dp[i][prevColor + 1] = res); + }; + + return dfs(0, -1); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Dynamic Programming (Bottom-Up) + +::tabs-start + +```python +class Solution: + def min_cost(self, costs: List[List[int]]) -> int: + n = len(costs) + if n == 0: + return 0 + + dp = [[0] * 3 for _ in range(n)] + for c in range(3): + dp[0][c] = costs[0][c] + + for i in range(1, n): + for c in range(3): + dp[i][c] = ( + costs[i][c] + + min(dp[i - 1][(c + 1) % 3], dp[i - 1][(c + 2) % 3]) + ) + + return min(dp[n - 1]) +``` + +```java +public class Solution { + public int minCost(int[][] costs) { + int n = costs.length; + if (n == 0) return 0; + + int[][] dp = new int[n][3]; + for (int c = 0; c < 3; c++) { + dp[0][c] = costs[0][c]; + } + + for (int i = 1; i < n; i++) { + for (int c = 0; c < 3; c++) { + dp[i][c] = costs[i][c] + + Math.min(dp[i - 1][(c + 1) % 3], dp[i - 1][(c + 2) % 3]); + } + } + + return Math.min(dp[n - 1][0], Math.min(dp[n - 1][1], dp[n - 1][2])); + } +} +``` + +```cpp +class Solution { +public: + int minCost(vector>& costs) { + int n = costs.size(); + if (n == 0) return 0; + + vector> dp(n, vector(3, 0)); + for (int c = 0; c < 3; c++) { + dp[0][c] = costs[0][c]; + } + + for (int i = 1; i < n; i++) { + for (int c = 0; c < 3; c++) { + dp[i][c] = costs[i][c] + + min(dp[i - 1][(c + 1) % 3], dp[i - 1][(c + 2) % 3]); + } + } + + return min({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]}); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} costs + * @return {number} + */ + minCost(costs) { + let n = costs.length; + if (n === 0) return 0; + + let dp = Array.from({ length: n }, () => Array(3).fill(0)); + for (let c = 0; c < 3; c++) { + dp[0][c] = costs[0][c]; + } + + for (let i = 1; i < n; i++) { + for (let c = 0; c < 3; c++) { + dp[i][c] = costs[i][c] + + Math.min(dp[i - 1][(c + 1) % 3], dp[i - 1][(c + 2) % 3]); + } + } + + return Math.min(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Dynamic Programming (Space Optimized) + +::tabs-start + +```python +class Solution: + def minCost(self, costs: List[List[int]]) -> int: + dp = [0, 0, 0] + + for i in range(len(costs)): + dp0 = costs[i][0] + min(dp[1], dp[2]) + dp1 = costs[i][1] + min(dp[0], dp[2]) + dp2 = costs[i][2] + min(dp[0], dp[1]) + dp = [dp0, dp1, dp2] + + return min(dp) +``` + +```java +public class Solution { + public int minCost(int[][] costs) { + int dp0 = 0, dp1 = 0, dp2 = 0; + + for (int i = 0; i < costs.length; i++) { + int newDp0 = costs[i][0] + Math.min(dp1, dp2); + int newDp1 = costs[i][1] + Math.min(dp0, dp2); + int newDp2 = costs[i][2] + Math.min(dp0, dp1); + dp0 = newDp0; + dp1 = newDp1; + dp2 = newDp2; + } + + return Math.min(dp0, Math.min(dp1, dp2)); + } +} +``` + +```cpp +class Solution { +public: + int minCost(vector>& costs) { + int dp0 = 0, dp1 = 0, dp2 = 0; + + for (const auto& cost : costs) { + int newDp0 = cost[0] + min(dp1, dp2); + int newDp1 = cost[1] + min(dp0, dp2); + int newDp2 = cost[2] + min(dp0, dp1); + dp0 = newDp0; + dp1 = newDp1; + dp2 = newDp2; + } + + return min({dp0, dp1, dp2}); + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[][]} costs + * @return {number} + */ + minCost(costs) { + let dp = [0, 0, 0]; + + for (let i = 0; i < costs.length; i++) { + let dp0 = costs[i][0] + Math.min(dp[1], dp[2]); + let dp1 = costs[i][1] + Math.min(dp[0], dp[2]); + let dp2 = costs[i][2] + Math.min(dp[0], dp[1]); + dp = [dp0, dp1, dp2]; + } + + return Math.min(dp[0], dp[1], dp[2]); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ extra space. \ No newline at end of file diff --git a/articles/process-tasks-using-servers.md b/articles/process-tasks-using-servers.md new file mode 100644 index 000000000..f79a9c753 --- /dev/null +++ b/articles/process-tasks-using-servers.md @@ -0,0 +1,501 @@ +## 1. Brute Force (Simulation) + +::tabs-start + +```python +class Solution: + def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]: + n, m = len(servers), len(tasks) + available = [True] * n + finishTime = [0] * n + res = [] + time = 0 + + for t in range(m): + time = max(time, t) + + for i in range(n): + if finishTime[i] <= time: + available[i] = True + + if not any(available): + time = min(finishTime) + for i in range(n): + if finishTime[i] <= time: + available[i] = True + + minIdx = -1 + for i in range(n): + if (available[i] and + (minIdx == -1 or servers[i] < servers[minIdx] or + (servers[i] == servers[minIdx] and i < minIdx)) + ): + minIdx = i + + res.append(minIdx) + available[minIdx] = False + finishTime[minIdx] = time + tasks[t] + + return res +``` + +```java +public class Solution { + public int[] assignTasks(int[] servers, int[] tasks) { + int n = servers.length, m = tasks.length; + boolean[] available = new boolean[n]; + Arrays.fill(available, true); + int[] finishTime = new int[n]; + int[] res = new int[m]; + int time = 0; + + for (int t = 0; t < m; t++) { + time = Math.max(time, t); + for (int i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + + if (!anyAvailable(available)) { + time = Arrays.stream(finishTime).min().getAsInt(); + for (int i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + } + + int minIdx = -1; + for (int i = 0; i < n; i++) { + if (available[i] && (minIdx == -1 || servers[i] < servers[minIdx] || + (servers[i] == servers[minIdx] && i < minIdx))) { + minIdx = i; + } + } + + res[t] = minIdx; + available[minIdx] = false; + finishTime[minIdx] = time + tasks[t]; + } + return res; + } + + private boolean anyAvailable(boolean[] available) { + for (boolean v : available) { + if (v) return true; + } + return false; + } +} +``` + +```cpp +class Solution { +public: + vector assignTasks(vector& servers, vector& tasks) { + int n = servers.size(), m = tasks.size(); + vector available(n, true); + vector finishTime(n, 0), res(m); + int time = 0; + + for (int t = 0; t < m; t++) { + time = max(time, t); + + for (int i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + + if (!any_of(available.begin(), available.end(), [](bool v) { return v; })) { + time = *min_element(finishTime.begin(), finishTime.end()); + for (int i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + } + + int minIdx = -1; + for (int i = 0; i < n; i++) { + if (available[i] && (minIdx == -1 || servers[i] < servers[minIdx] || + (servers[i] == servers[minIdx] && i < minIdx))) { + minIdx = i; + } + } + + res[t] = minIdx; + available[minIdx] = false; + finishTime[minIdx] = time + tasks[t]; + } + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} servers + * @param {number[]} tasks + * @return {number[]} + */ + assignTasks(servers, tasks) { + const n = servers.length, m = tasks.length; + const available = Array(n).fill(true); + const finishTime = Array(n).fill(0); + const res = []; + let time = 0; + + for (let t = 0; t < m; t++) { + time = Math.max(time, t); + + for (let i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + + if (!available.some(v => v)) { + time = Math.min(...finishTime); + for (let i = 0; i < n; i++) { + if (finishTime[i] <= time) { + available[i] = true; + } + } + } + + let minIdx = -1; + for (let i = 0; i < n; i++) { + if (available[i] && (minIdx === -1 || servers[i] < servers[minIdx] || + (servers[i] === servers[minIdx] && i < minIdx))) { + minIdx = i; + } + } + + res.push(minIdx); + available[minIdx] = false; + finishTime[minIdx] = time + tasks[t]; + } + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(m * n)$ +* Space complexity: + * $O(n)$ extra space. + * $O(m)$ space for the output array. + +> Where $m$ is the number of tasks and $n$ is the number of servers. + +--- + +## 2. Two Min-Heaps - I + +::tabs-start + +```python +class Solution: + def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]: + res = [0] * len(tasks) + available = [(servers[i], i) for i in range(len(servers))] + heapq.heapify(available) + unavailable = [] + + t = 0 + for i in range(len(tasks)): + t = max(t, i) + + if not available: + t = unavailable[0][0] + + while unavailable and t >= unavailable[0][0]: + timeFree, weight, index = heapq.heappop(unavailable) + heapq.heappush(available, (weight, index)) + + weight, index = heapq.heappop(available) + res[i] = index + heapq.heappush(unavailable, (t + tasks[i], weight, index)) + + return res +``` + +```java +public class Solution { + public int[] assignTasks(int[] servers, int[] tasks) { + int n = servers.length, m = tasks.length; + int[] res = new int[m]; + + PriorityQueue available = new PriorityQueue<>( + (a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]) + ); + PriorityQueue unavailable = new PriorityQueue<>( + Comparator.comparingInt(a -> a[0]) + ); + + for (int i = 0; i < n; i++) { + available.offer(new int[]{servers[i], i}); + } + + int time = 0; + for (int i = 0; i < m; i++) { + time = Math.max(time, i); + + if (available.isEmpty()) { + time = unavailable.peek()[0]; + } + + while (!unavailable.isEmpty() && unavailable.peek()[0] <= time) { + int[] server = unavailable.poll(); + available.offer(new int[]{server[1], server[2]}); + } + + int[] bestServer = available.poll(); + res[i] = bestServer[1]; + unavailable.offer(new int[]{time + tasks[i], bestServer[0], bestServer[1]}); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector assignTasks(vector& servers, vector& tasks) { + int n = servers.size(), m = tasks.size(); + vector res(m); + + priority_queue, vector>, greater<>> available; + priority_queue, vector>, greater<>> unavailable; + + for (int i = 0; i < n; i++) { + available.emplace(servers[i], i); + } + + int time = 0; + for (int i = 0; i < m; i++) { + time = max(time, i); + + if (available.empty()) { + time = unavailable.top()[0]; + } + + while (!unavailable.empty() && unavailable.top()[0] <= time) { + auto server = unavailable.top(); unavailable.pop(); + available.emplace(server[1], server[2]); + } + + auto [weight, index] = available.top(); available.pop(); + res[i] = index; + unavailable.push({time + tasks[i], weight, index}); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} servers + * @param {number[]} tasks + * @return {number[]} + */ + assignTasks(servers, tasks) { + const n = servers.length; + const available = new PriorityQueue( + (a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0] + ); + const unavailable = new PriorityQueue((a, b) => a[0] - b[0]); + const res = new Array(tasks.length); + + for (let i = 0; i < n; i++) { + available.enqueue([servers[i], i]); + } + + let time = 0; + for (let i = 0; i < tasks.length; i++) { + time = Math.max(time, i); + if (available.isEmpty()) { + time = unavailable.front()[0]; + } + while (!unavailable.isEmpty() && unavailable.front()[0] <= time) { + const [timeFree, weight, index] = unavailable.dequeue(); + available.enqueue([weight, index]); + } + const [weight, index] = available.dequeue(); + res[i] = index; + unavailable.enqueue([time + tasks[i], weight, index]); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O((n + m) \log n)$ +* Space complexity: + * $O(n)$ extra space. + * $O(m)$ space for the output array. + +> Where $m$ is the number of tasks and $n$ is the number of servers. + +--- + +## 3. Two Min-Heaps - II + +::tabs-start + +```python +class Solution: + def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]: + res = [] + available = [[weight, i, 0] for i, weight in enumerate(servers)] + unavailable = [] + heapq.heapify(available) + + for i, task in enumerate(tasks): + while unavailable and unavailable[0][0] <= i or not available: + timeFree, weight, index = heapq.heappop(unavailable) + heapq.heappush(available, [weight, index, timeFree]) + + weight, index, timeFree = heapq.heappop(available) + res.append(index) + heapq.heappush(unavailable, [max(timeFree, i) + task, weight, index]) + + return res +``` + +```java +public class Solution { + public int[] assignTasks(int[] servers, int[] tasks) { + int m = tasks.length, n = servers.length; + int[] res = new int[m]; + + PriorityQueue available = new PriorityQueue<>((a, b) -> { + if(a[0] != b[0]) return Integer.compare(a[0], b[0]); + if(a[1] != b[1]) return Integer.compare(a[1], b[1]); + return Integer.compare(a[2], b[2]); + }); + + PriorityQueue unavailable = new PriorityQueue<>((a, b) -> { + if(a[0] != b[0]) return Integer.compare(a[0], b[0]); + if(a[1] != b[1]) return Integer.compare(a[1], b[1]); + return Integer.compare(a[2], b[2]); + }); + + for (int i = 0; i < n; i++) { + available.offer(new int[]{servers[i], i, 0}); + } + + for (int i = 0; i < m; i++) { + while ((!unavailable.isEmpty() && unavailable.peek()[0] <= i) || + available.isEmpty()) { + int[] server = unavailable.poll(); + available.offer(new int[]{server[1], server[2], server[0]}); + } + int[] server = available.poll(); + res[i] = server[1]; + unavailable.offer(new int[]{ + Math.max(server[2], i) + tasks[i], server[0], server[1]} + ); + } + + return res; + } +} +``` + +```cpp +class Solution { +public: + vector assignTasks(vector& servers, vector& tasks) { + int n = servers.size(), m = tasks.size(); + vector res(m); + + priority_queue, vector>, greater<>> available; + priority_queue, vector>, greater<>> unavailable; + + for (int i = 0; i < n; ++i) { + available.push({servers[i], i, 0}); + } + + for (int i = 0; i < m; ++i) { + while (!unavailable.empty() && (unavailable.top()[0] <= i || + available.empty())) { + auto [timeFree, weight, index] = unavailable.top(); + unavailable.pop(); + available.push({weight, index, timeFree}); + } + + auto [weight, index, timeFree] = available.top(); + available.pop(); + res[i] = index; + unavailable.push({max(timeFree, i) + tasks[i], weight, index}); + } + + return res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number[]} servers + * @param {number[]} tasks + * @return {number[]} + */ + assignTasks(servers, tasks) { + const res = new Array(tasks.length); + const available = new PriorityQueue( + (a, b) => a[0] === b[0] ? (a[1] === b[1] ? a[2] - b[2] : a[1] - b[1]) : a[0] - b[0] + ); + const unavailable = new PriorityQueue( + (a, b) => a[0] === b[0] ? (a[1] === b[1] ? a[2] - b[2] : a[1] - b[1]) : a[0] - b[0] + ); + + for (let i = 0; i < servers.length; i++) { + available.enqueue([servers[i], i, 0]); + } + + for (let i = 0; i < tasks.length; i++) { + while ((!unavailable.isEmpty() && unavailable.front()[0] <= i) || + available.isEmpty()) { + const [timeFree, weight, index] = unavailable.dequeue(); + available.enqueue([weight, index, timeFree]); + } + + const [weight, index, timeFree] = available.dequeue(); + res[i] = index; + unavailable.enqueue([Math.max(timeFree, i) + tasks[i], weight, index]); + } + + return res; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O((n + m) \log n)$ +* Space complexity: + * $O(n)$ extra space. + * $O(m)$ space for the output array. + +> Where $m$ is the number of tasks and $n$ is the number of servers. \ No newline at end of file diff --git a/articles/seat-reservation-manager.md b/articles/seat-reservation-manager.md new file mode 100644 index 000000000..c546e1be8 --- /dev/null +++ b/articles/seat-reservation-manager.md @@ -0,0 +1,407 @@ +## 1. Brute Force + +::tabs-start + +```python +class SeatManager: + + def __init__(self, n: int): + self.seats = [False] * n + + def reserve(self) -> int: + for i in range(len(self.seats)): + if not self.seats[i]: + self.seats[i] = True + return i + 1 + + def unreserve(self, seatNumber: int) -> None: + self.seats[seatNumber - 1] = False +``` + +```java +public class SeatManager { + private boolean[] seats; + + public SeatManager(int n) { + seats = new boolean[n]; + } + + public int reserve() { + for (int i = 0; i < seats.length; i++) { + if (!seats[i]) { + seats[i] = true; + return i + 1; + } + } + return -1; + } + + public void unreserve(int seatNumber) { + seats[seatNumber - 1] = false; + } +} +``` + +```cpp +class SeatManager { +private: + vector seats; + +public: + SeatManager(int n) : seats(n, false) {} + + int reserve() { + for (int i = 0; i < seats.size(); i++) { + if (!seats[i]) { + seats[i] = true; + return i + 1; + } + } + return -1; + } + + void unreserve(int seatNumber) { + seats[seatNumber - 1] = false; + } +}; +``` + +```javascript +class SeatManager { + /** + * @param {number} n + */ + constructor(n) { + this.seats = new Array(n).fill(false); + } + + /** + * @return {number} + */ + reserve() { + for (let i = 0; i < this.seats.length; i++) { + if (!this.seats[i]) { + this.seats[i] = true; + return i + 1; + } + } + } + + /** + * @param {number} seatNumber + * @return {void} + */ + unreserve(seatNumber) { + this.seats[seatNumber - 1] = false; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: + * $O(n)$ time for initialization. + * $O(n)$ time for each $reserve()$ function call. + * $O(1)$ time for each $unreserve()$ function call. +* Space complexity: $O(n)$ + +--- + +## 2. Min-Heap + +::tabs-start + +```python +class SeatManager: + def __init__(self, n: int): + self.unres = list(range(1, n + 1)) + heapq.heapify(self.unres) + + def reserve(self) -> int: + return heapq.heappop(self.unres) + + def unreserve(self, seatNumber: int) -> None: + heapq.heappush(self.unres, seatNumber) +``` + +```java +public class SeatManager { + private PriorityQueue unres; + + public SeatManager(int n) { + unres = new PriorityQueue<>(); + for (int i = 1; i <= n; i++) { + unres.offer(i); + } + } + + public int reserve() { + return unres.poll(); + } + + public void unreserve(int seatNumber) { + unres.offer(seatNumber); + } +} +``` + +```cpp +class SeatManager { +private: + priority_queue, greater> unres; + +public: + SeatManager(int n) { + for (int i = 1; i <= n; i++) { + unres.push(i); + } + } + + int reserve() { + int seat = unres.top(); + unres.pop(); + return seat; + } + + void unreserve(int seatNumber) { + unres.push(seatNumber); + } +}; +``` + +```javascript +class SeatManager { + /** + * @param {number} n + */ + constructor(n) { + this.unres = new MinPriorityQueue(); + for (let i = 1; i <= n; i++) { + this.unres.enqueue(i); + } + } + + /** + * @return {number} + */ + reserve() { + return this.unres.dequeue(); + } + + /** + * @param {number} seatNumber + * @return {void} + */ + unreserve(seatNumber) { + this.unres.enqueue(seatNumber); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: + * $O(n \log n)$ time for initialization. + * $O(\log n)$ time for each $reserve()$ function call. + * $O(\log n)$ time for each $unreserve()$ function call. +* Space complexity: $O(n)$ + +--- + +## 3. Min-Heap (Optimal) + +::tabs-start + +```python +class SeatManager: + def __init__(self, n: int): + self.minHeap = [] + self.nextSeat = 1 + + def reserve(self) -> int: + if self.minHeap: + return heapq.heappop(self.minHeap) + + seat = self.nextSeat + self.nextSeat += 1 + return seat + + def unreserve(self, seatNumber: int) -> None: + heapq.heappush(self.minHeap, seatNumber) +``` + +```java +public class SeatManager { + private PriorityQueue minHeap; + private int nextSeat; + + public SeatManager(int n) { + minHeap = new PriorityQueue<>(); + nextSeat = 1; + } + + public int reserve() { + if (!minHeap.isEmpty()) { + return minHeap.poll(); + } + return nextSeat++; + } + + public void unreserve(int seatNumber) { + minHeap.offer(seatNumber); + } +} +``` + +```cpp +class SeatManager { +private: + priority_queue, greater> minHeap; + int nextSeat; + +public: + SeatManager(int n) { + nextSeat = 1; + } + + int reserve() { + if (!minHeap.empty()) { + int seat = minHeap.top(); + minHeap.pop(); + return seat; + } + return nextSeat++; + } + + void unreserve(int seatNumber) { + minHeap.push(seatNumber); + } +}; +``` + +```javascript +class SeatManager { + /** + * @param {number} n + */ + constructor(n) { + this.minHeap = new MinPriorityQueue(); + this.nextSeat = 1; + } + + /** + * @return {number} + */ + reserve() { + if (!this.minHeap.isEmpty()) { + return this.minHeap.dequeue(); + } + return this.nextSeat++; + } + + /** + * @param {number} seatNumber + * @return {void} + */ + unreserve(seatNumber) { + this.minHeap.enqueue(seatNumber); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: + * $O(1)$ time for initialization. + * $O(\log n)$ time for each $reserve()$ function call. + * $O(\log n)$ time for each $unreserve()$ function call. +* Space complexity: $O(n)$ + +--- + +## 4. Ordered Set + +::tabs-start + +```python +class SeatManager: + def __init__(self, n: int): + self.available = SortedSet() + self.nextSeat = 1 + + def reserve(self) -> int: + if self.available: + return self.available.pop(0) + + seat = self.nextSeat + self.nextSeat += 1 + return seat + + def unreserve(self, seatNumber: int) -> None: + self.available.add(seatNumber) +``` + +```java +public class SeatManager { + private TreeSet available; + private int nextSeat; + + public SeatManager(int n) { + available = new TreeSet<>(); + nextSeat = 1; + } + + public int reserve() { + if (!available.isEmpty()) { + return available.pollFirst(); + } + return nextSeat++; + } + + public void unreserve(int seatNumber) { + available.add(seatNumber); + } +} +``` + +```cpp +class SeatManager { +private: + set available; + int nextSeat; + +public: + SeatManager(int n) { + nextSeat = 1; + } + + int reserve() { + if (!available.empty()) { + int seat = *available.begin(); + available.erase(available.begin()); + return seat; + } + return nextSeat++; + } + + void unreserve(int seatNumber) { + available.insert(seatNumber); + } +}; +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: + * $O(1)$ time for initialization. + * $O(\log n)$ time for each $reserve()$ function call. + * $O(\log n)$ time for each $unreserve()$ function call. +* Space complexity: $O(n)$ \ No newline at end of file diff --git a/articles/student-attendance-record-ii.md b/articles/student-attendance-record-ii.md new file mode 100644 index 000000000..b941f3076 --- /dev/null +++ b/articles/student-attendance-record-ii.md @@ -0,0 +1,854 @@ +## 1. Dynamic Programming (Top-Down) - I + +::tabs-start + +```python +class Solution: + def checkRecord(self, n: int) -> int: + MOD = 1000000007 + cache = [[[-1 for _ in range(3)] for _ in range(2)] for _ in range(n + 1)] + + def dfs(i, cntA, cntL): + if i == 0: + return 1 + if cache[i][cntA][cntL] != -1: + return cache[i][cntA][cntL] + + res = dfs(i - 1, cntA, 0) % MOD + + if cntA == 0: + res = (res + dfs(i - 1, 1, 0)) % MOD + + if cntL < 2: + res = (res + dfs(i - 1, cntA, cntL + 1)) % MOD + + cache[i][cntA][cntL] = res + return res + + return dfs(n, 0, 0) +``` + +```java +public class Solution { + private static final int MOD = 1000000007; + private int[][][] cache; + + public int checkRecord(int n) { + this.cache = new int[n + 1][2][3]; + for (int[][] matrix : cache) { + for (int[] row : matrix) { + Arrays.fill(row, -1); + } + } + return dfs(n, 0, 0); + } + + private int dfs(int i, int cntA, int cntL) { + if (i == 0) { + return 1; + } + if (cache[i][cntA][cntL] != -1) { + return cache[i][cntA][cntL]; + } + + int res = dfs(i - 1, cntA, 0) % MOD; + + if (cntA == 0) { + res = (res + dfs(i - 1, 1, 0)) % MOD; + } + + if (cntL < 2) { + res = (res + dfs(i - 1, cntA, cntL + 1)) % MOD; + } + + return cache[i][cntA][cntL] = res; + } +} +``` + +```cpp +class Solution { + const int MOD = 1000000007; + vector>> cache; + +public: + int checkRecord(int n) { + cache.assign(n + 1, vector>(2, vector(3, -1))); + return dfs(n, 0, 0); + } + +private: + int dfs(int i, int cntA, int cntL) { + if (i == 0) { + return 1; + } + if (cache[i][cntA][cntL] != -1) { + return cache[i][cntA][cntL]; + } + + int res = dfs(i - 1, cntA, 0) % MOD; + + if (cntA == 0) { + res = (res + dfs(i - 1, 1, 0)) % MOD; + } + + if (cntL < 2) { + res = (res + dfs(i - 1, cntA, cntL + 1)) % MOD; + } + + return cache[i][cntA][cntL] = res; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + checkRecord(n) { + const MOD = 1000000007; + let cache = Array.from({ length: n + 1 }, () => + Array.from({ length: 2 }, () => new Array(3).fill(-1)) + ); + + const dfs = (i, cntA, cntL) => { + if (i === 0) return 1; + if (cache[i][cntA][cntL] !== -1) return cache[i][cntA][cntL]; + + let res = dfs(i - 1, cntA, 0) % MOD; + + if (cntA === 0) { + res = (res + dfs(i - 1, 1, 0)) % MOD; + } + + if (cntL < 2) { + res = (res + dfs(i - 1, cntA, cntL + 1)) % MOD; + } + + return (cache[i][cntA][cntL] = res); + }; + + return dfs(n, 0, 0); + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 2. Dynamic Programming (Top-Down) - II + +::tabs-start + +```python +class Solution: + def checkRecord(self, n: int) -> int: + MOD = 10**9 + 7 + cache = {} + + def count(n): + if n == 1: + # (A, L) + return { + (0, 0): 1, (0, 1): 1, (0, 2): 0, + (1, 0): 1, (1, 1): 0, (1, 2): 0 + } + + if n in cache: + return cache[n] + + tmp = count(n - 1) + res = defaultdict(int) + + # Choose P + res[(0, 0)] = ((tmp[(0, 0)] + tmp[(0, 1)]) % MOD + tmp[(0, 2)]) % MOD + res[(1, 0)] = ((tmp[(1, 0)] + tmp[(1, 1)]) % MOD + tmp[(1, 2)]) % MOD + + # Choose L + res[(0, 1)] = tmp[(0, 0)] + res[(0, 2)] = tmp[(0, 1)] + res[(1, 1)] = tmp[(1, 0)] + res[(1, 2)] = tmp[(1, 1)] + + # Choose A + res[(1, 0)] += ((tmp[(0, 0)] + tmp[(0, 1)]) % MOD + tmp[(0, 2)]) % MOD + + cache[n] = res + return res + + return sum(count(n).values()) % MOD +``` + +```java +public class Solution { + private static final int MOD = 1000000007; + private int[][][] cache; + private int[][] baseCase; + + public int checkRecord(int n) { + cache = new int[n + 1][2][3]; + baseCase = new int[][]{{1, 1, 0}, {1, 0, 0}}; + for (int[][] matrix : cache) { + for (int[] row : matrix) { + Arrays.fill(row, -1); + } + } + int[][] result = count(n); + int total = 0; + for (int[] row : result) { + for (int val : row) { + total = (total + val) % MOD; + } + } + return total; + } + + private int[][] count(int n) { + if (n == 1) { + // (A, L) + return baseCase; + } + + if (cache[n][0][0] != -1) { + return cache[n]; + } + + int[][] prev = count(n - 1); + int[][] res = cache[n]; + + // Choose P + res[0][0] = ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD; + res[1][0] = ((prev[1][0] + prev[1][1]) % MOD + prev[1][2]) % MOD; + + // Choose L + res[0][1] = prev[0][0]; + res[0][2] = prev[0][1]; + res[1][1] = prev[1][0]; + res[1][2] = prev[1][1]; + + // Choose A + res[1][0] = (res[1][0] + ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD) % MOD; + + return cache[n]; + } +} +``` + +```cpp +class Solution { +private: + static constexpr int MOD = 1000000007; + vector> baseCase = {{1, 1, 0}, {1, 0, 0}}; + vector>> cache; + +public: + int checkRecord(int n) { + cache.assign(n + 1, vector>(2, vector(3, -1))); + const vector>& result = count(n); + int total = 0; + for (const auto& row : result) { + for (int val : row) { + total = (total + val) % MOD; + } + } + return total; + } + +private: + const vector>& count(int n) { + if (n == 1) { + return baseCase; + } + + if (cache[n][0][0] != -1) { + return cache[n]; + } + + const vector>& prev = count(n - 1); + auto& res = cache[n]; + + // Choose P + res[0][0] = ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD; + res[1][0] = ((prev[1][0] + prev[1][1]) % MOD + prev[1][2]) % MOD; + + // Choose L + res[0][1] = prev[0][0]; + res[0][2] = prev[0][1]; + res[1][1] = prev[1][0]; + res[1][2] = prev[1][1]; + + // Choose A + res[1][0] = (res[1][0] + ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD) % MOD; + + return cache[n]; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + checkRecord(n) { + const MOD = 1000000007; + const baseCase = [ + [1, 1, 0], // (A = 0, L = 0, 1, 2) + [1, 0, 0] // (A = 1, L = 0, 1, 2) + ]; + let cache = Array.from({ length: n + 1 }, () => + Array.from({ length: 2 }, () => new Array(3).fill(-1)) + ); + + const count = (n) => { + if (n === 1) return baseCase; + if (cache[n][0][0] !== -1) return cache[n]; + + const prev = count(n - 1); + const res = cache[n]; + + // Choose P + res[0][0] = ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD; + res[1][0] = ((prev[1][0] + prev[1][1]) % MOD + prev[1][2]) % MOD; + + // Choose L + res[0][1] = prev[0][0]; + res[0][2] = prev[0][1]; + res[1][1] = prev[1][0]; + res[1][2] = prev[1][1]; + + // Choose A + res[1][0] = (res[1][0] + ((prev[0][0] + prev[0][1]) % MOD + prev[0][2]) % MOD) % MOD; + + return res; + }; + + const result = count(n); + let total = 0; + for (const row of result) { + for (const val of row) { + total = (total + val) % MOD; + } + } + return total; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 3. Dynamic Programming (Bottom-Up) + +::tabs-start + +```python +class Solution: + def checkRecord(self, n: int) -> int: + MOD = 1000000007 + dp = [[[0 for _ in range(3)] for _ in range(2)] for _ in range(n + 1)] + + dp[0][0][0] = 1 # Base case + + for i in range(1, n + 1): + for cntA in range(2): + for cntL in range(3): + # Choose P + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA][cntL]) % MOD + + # Choose A + if cntA > 0: + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA - 1][cntL]) % MOD + + # Choose L + if cntL > 0: + dp[i][cntA][cntL] = (dp[i][cntA][cntL] + dp[i - 1][cntA][cntL - 1]) % MOD + + return sum(dp[n][cntA][cntL] for cntA in range(2) for cntL in range(3)) % MOD +``` + +```java +public class Solution { + public int checkRecord(int n) { + final int MOD = 1000000007; + int[][][] dp = new int[n + 1][2][3]; + + dp[0][0][0] = 1; + + for (int i = 1; i <= n; i++) { + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + // Choose P + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + dp[i][cntA][cntL] = (dp[i][cntA][cntL] + dp[i - 1][cntA][cntL - 1]) % MOD; + } + } + } + } + + int result = 0; + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + result = (result + dp[n][cntA][cntL]) % MOD; + } + } + + return result; + } +} +``` + +```cpp +class Solution { +public: + int checkRecord(int n) { + const int MOD = 1000000007; + vector>> dp(n + 1, vector>(2, vector(3, 0))); + + dp[0][0][0] = 1; + + for (int i = 1; i <= n; i++) { + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + // Choose P + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + dp[i][cntA][cntL] = (dp[i][cntA][cntL] + dp[i - 1][cntA][cntL - 1]) % MOD; + } + } + } + } + + int result = 0; + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + result = (result + dp[n][cntA][cntL]) % MOD; + } + } + + return result; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + checkRecord(n) { + const MOD = 1000000007; + const dp = Array.from({ length: n + 1 }, () => + Array.from({ length: 2 }, () => new Array(3).fill(0)) + ); + + dp[0][0][0] = 1; + + for (let i = 1; i <= n; i++) { + for (let cntA = 0; cntA < 2; cntA++) { + for (let cntL = 0; cntL < 3; cntL++) { + // Choose P + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + dp[i][cntA][0] = (dp[i][cntA][0] + dp[i - 1][cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + dp[i][cntA][cntL] = (dp[i][cntA][cntL] + dp[i - 1][cntA][cntL - 1]) % MOD; + } + } + } + } + + let result = 0; + for (let cntA = 0; cntA < 2; cntA++) { + for (let cntL = 0; cntL < 3; cntL++) { + result = (result + dp[n][cntA][cntL]) % MOD; + } + } + + return result; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(n)$ + +--- + +## 4. Dynamic Programming (Space Optimized) - I + +::tabs-start + +```python +class Solution: + def checkRecord(self, n: int) -> int: + if n == 1: + return 3 + + MOD = 10**9 + 7 + dp = { + (0, 0): 1, (0, 1): 1, (0, 2): 0, + (1, 0): 1, (1, 1): 0, (1, 2): 0 + } + + for i in range(n - 1): + ndp = defaultdict(int) + + # Choose P + ndp[(0, 0)] = ((dp[(0, 0)] + dp[(0, 1)]) % MOD + dp[(0, 2)]) % MOD + ndp[(1, 0)] = ((dp[(1, 0)] + dp[(1, 1)]) % MOD + dp[(1, 2)]) % MOD + + # Choose L + ndp[(0, 1)] = dp[(0, 0)] + ndp[(1, 1)] = dp[(1, 0)] + ndp[(0, 2)] = dp[(0, 1)] + ndp[(1, 2)] = dp[(1, 1)] + + # Choose A + ndp[(1, 0)] = (ndp[(1, 0)] + (((dp[(0, 0)] + dp[(0, 1)]) % MOD + dp[(0, 2)]) % MOD)) % MOD + + dp = ndp + + return sum(dp.values()) % MOD +``` + +```java +public class Solution { + public int checkRecord(int n) { + if (n == 1) return 3; + + final int MOD = 1000000007; + int[][] dp = {{1, 1, 0}, {1, 0, 0}}; + + for (int i = 0; i < n - 1; i++) { + int[][] ndp = new int[2][3]; + + // Choose P + ndp[0][0] = ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD; + ndp[1][0] = ((dp[1][0] + dp[1][1]) % MOD + dp[1][2]) % MOD; + + // Choose L + ndp[0][1] = dp[0][0]; + ndp[1][1] = dp[1][0]; + ndp[0][2] = dp[0][1]; + ndp[1][2] = dp[1][1]; + + // Choose A + ndp[1][0] = (ndp[1][0] + ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD) % MOD; + + dp = ndp; + } + + int total = 0; + for (int[] row : dp) { + for (int val : row) { + total = (total + val) % MOD; + } + } + return total; + } +} +``` + +```cpp +class Solution { +public: + int checkRecord(int n) { + if (n == 1) return 3; + + const int MOD = 1000000007; + vector> dp = {{1, 1, 0}, {1, 0, 0}}; + + for (int i = 0; i < n - 1; i++) { + vector> ndp(2, vector(3, 0)); + + // Choose P + ndp[0][0] = ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD; + ndp[1][0] = ((dp[1][0] + dp[1][1]) % MOD + dp[1][2]) % MOD; + + // Choose L + ndp[0][1] = dp[0][0]; + ndp[1][1] = dp[1][0]; + ndp[0][2] = dp[0][1]; + ndp[1][2] = dp[1][1]; + + // Choose A + ndp[1][0] = (ndp[1][0] + ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD) % MOD; + + swap(dp, ndp); + } + + int total = 0; + for (auto& row : dp) { + for (int val : row) { + total = (total + val) % MOD; + } + } + return total; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + checkRecord(n) { + if (n === 1) return 3; + + const MOD = 1000000007; + let dp = [[1, 1, 0], [1, 0, 0]]; + + for (let i = 0; i < n - 1; i++) { + let ndp = Array.from({ length: 2 }, () => new Array(3).fill(0)); + + // Choose P + ndp[0][0] = ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD; + ndp[1][0] = ((dp[1][0] + dp[1][1]) % MOD + dp[1][2]) % MOD; + + // Choose L + ndp[0][1] = dp[0][0]; + ndp[1][1] = dp[1][0]; + ndp[0][2] = dp[0][1]; + ndp[1][2] = dp[1][1]; + + // Choose A + ndp[1][0] = (ndp[1][0] + ((dp[0][0] + dp[0][1]) % MOD + dp[0][2]) % MOD) % MOD; + + [dp, ndp] = [ndp, dp]; + } + + let total = 0; + for (let row of dp) { + for (let val of row) { + total = (total + val) % MOD; + } + } + return total; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ + +--- + +## 5. Dynamic Programming (Space Optimized) - II + +::tabs-start + +```python +class Solution: + def checkRecord(self, n: int) -> int: + MOD = 1000000007 + dp = [[0] * 3 for _ in range(2)] + + dp[0][0] = 1 # Base case + + for i in range(1, n + 1): + next_dp = [[0] * 3 for _ in range(2)] + + for cntA in range(2): + for cntL in range(3): + # Choose P + next_dp[cntA][0] = (next_dp[cntA][0] + dp[cntA][cntL]) % MOD + + # Choose A + if cntA > 0: + next_dp[cntA][0] = (next_dp[cntA][0] + dp[cntA - 1][cntL]) % MOD + + # Choose L + if cntL > 0: + next_dp[cntA][cntL] = (next_dp[cntA][cntL] + dp[cntA][cntL - 1]) % MOD + + dp = next_dp + + return sum(dp[cntA][cntL] for cntA in range(2) for cntL in range(3)) % MOD +``` + +```java +public class Solution { + public int checkRecord(int n) { + final int MOD = 1000000007; + int[][] dp = new int[2][3]; + + dp[0][0] = 1; + + for (int i = 1; i <= n; i++) { + int[][] nextDp = new int[2][3]; + + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + // Choose P + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + nextDp[cntA][cntL] = (nextDp[cntA][cntL] + dp[cntA][cntL - 1]) % MOD; + } + } + } + + dp = nextDp; + } + + int result = 0; + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + result = (result + dp[cntA][cntL]) % MOD; + } + } + return result; + } +} +``` + +```cpp +class Solution { +public: + int checkRecord(int n) { + const int MOD = 1000000007; + vector> dp(2, vector(3, 0)); + + dp[0][0] = 1; + + for (int i = 1; i <= n; i++) { + vector> nextDp(2, vector(3, 0)); + + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + // Choose P + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + nextDp[cntA][cntL] = (nextDp[cntA][cntL] + dp[cntA][cntL - 1]) % MOD; + } + } + } + + dp = nextDp; + } + + int result = 0; + for (int cntA = 0; cntA < 2; cntA++) { + for (int cntL = 0; cntL < 3; cntL++) { + result = (result + dp[cntA][cntL]) % MOD; + } + } + + return result; + } +}; +``` + +```javascript +class Solution { + /** + * @param {number} n + * @return {number} + */ + checkRecord(n) { + const MOD = 1000000007; + let dp = Array.from({ length: 2 }, () => new Array(3).fill(0)); + + dp[0][0] = 1; + + for (let i = 1; i <= n; i++) { + let nextDp = Array.from({ length: 2 }, () => new Array(3).fill(0)); + + for (let cntA = 0; cntA < 2; cntA++) { + for (let cntL = 0; cntL < 3; cntL++) { + // Choose P + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA][cntL]) % MOD; + + // Choose A + if (cntA > 0) { + nextDp[cntA][0] = (nextDp[cntA][0] + dp[cntA - 1][cntL]) % MOD; + } + + // Choose L + if (cntL > 0) { + nextDp[cntA][cntL] = (nextDp[cntA][cntL] + dp[cntA][cntL - 1]) % MOD; + } + } + } + + dp = nextDp; + } + + let result = 0; + for (let cntA = 0; cntA < 2; cntA++) { + for (let cntL = 0; cntL < 3; cntL++) { + result = (result + dp[cntA][cntL]) % MOD; + } + } + + return result; + } +} +``` + +::tabs-end + +### Time & Space Complexity + +* Time complexity: $O(n)$ +* Space complexity: $O(1)$ \ No newline at end of file