<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    
    <title>Leetcode</title>
    
    
    <description>leetcode.ca</description>
    
    <link>https://leetcode.ca/</link>
    <atom:link href="https://leetcode.ca/feed.xml" rel="self" type="application/rss+xml" />
    
    
      <item>
        <title>3907 - Count Smaller Elements With Opposite Parity</title>
        <description>
          
          Welcome to Subscribe On Youtube 3907. Count Smaller Elements With Opposite Parity 🔒 Description You are given an integer array nums of length n. The score of an index i is defined as the number of indices j such that: i &amp;lt; j &amp;lt; n nums[j] &amp;lt; nums[i] nums[i] and nums[j] have different parity (one is even and the other is odd). Return an integer array answer of length n, where answer[i] is the score of index i. &amp;nbsp; Example 1: Input: nums = [5,2,4,1,3] Output: [2,1,2,0,0] Explanation: For i = 0, the elements nums[1] = 2 and nums[2] = 4 are smaller and have different parity. For i = 1, the element nums[3] = 1 is smaller and has different parity. For i = 2, the elements nums[3] = 1 and nums[4] = 3 are smaller and have different parity. No valid elements exist for the remaining indices. Thus, the answer = [2, 1, 2, 0, 0]. Example 2: Input: nums = [4,4,1] Output: [1,1,0] Explanation:​​​​​​​ For i = 0 and i = 1, the element nums[2] = 1 is smaller and has different parity. Thus, the answer = [1, 1, 0]. Example 3: Input: nums = [7] Output: [0] Explanation: No elements exist to the right of index 0, so its score is 0. Thus, the answer = [0]. &amp;nbsp; Constraints: 1 &amp;lt;= nums.length &amp;lt;= 105 1 &amp;lt;= nums[i] &amp;lt;= 109​​​​​​​ Solutions Solution 1: Ordered List or Binary Indexed Tree We can use two ordered lists (or Binary Indexed Trees) to separately maintain even and odd elements. For each element, we query the number of smaller elements in the other list, and then add the current element to its corresponding list. The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array. Java C++ Python Go TypeScript class BinaryIndexedTree { int n; int[] c; BinaryIndexedTree(int n) { this.n = n; this.c = new int[n + 1]; } void update(int x, int delta) { while (x &amp;lt;= n) { c[x] += delta; x += x &amp;amp; -x; } } int query(int x) { int s = 0; while (x &amp;gt; 0) { s += c[x]; x -= x &amp;amp; -x; } return s; } } class Solution { public int[] countSmallerOppositeParity(int[] nums) { int n = nums.length; int[] sorted = nums.clone(); Arrays.sort(sorted); int m = 0; for (int i = 0; i &amp;lt; n; i++) { if (i == 0 || sorted[i] != sorted[i - 1]) { sorted[m++] = sorted[i]; } } BinaryIndexedTree[] bit = {new BinaryIndexedTree(m), new BinaryIndexedTree(m)}; int[] ans = new int[n]; for (int i = n - 1; i &amp;gt;= 0; --i) { int x = Arrays.binarySearch(sorted, 0, m, nums[i]) + 1; ans[i] = bit[nums[i] &amp;amp; 1 ^ 1].query(x - 1); bit[nums[i] &amp;amp; 1].update(x, 1); } return ans; } } struct BIT { int n; vector&amp;lt;int&amp;gt; c; BIT(int n) : n(n) , c(n + 1, 0) {} void update(int x, int delta) { for (;...
        </description>
        <pubDate>Thu, 04 Jun 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-06-04-3907-Count-Smaller-Elements-With-Opposite-Parity/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-06-04-3907-Count-Smaller-Elements-With-Opposite-Parity/</guid>
      </item>
    
      <item>
        <title>3906 - Count Good Integers on a Grid Path</title>
        <description>
          
          Welcome to Subscribe On Youtube 3906. Count Good Integers on a Grid Path Description You are given two integers l and r, and a string directions consisting of exactly three &amp;#39;D&amp;#39; characters and three &amp;#39;R&amp;#39; characters. For each integer x in the range [l, r] (inclusive), perform the following steps: If x has fewer than 16 digits, pad it on the left with leading zeros to obtain a 16-digit string. Place the 16 digits into a 4 &amp;times; 4 grid in row-major order (the first 4 digits form the first row from left to right, the next 4 digits form the second row, and so on). Starting at the top-left cell (row = 0, column = 0), apply the 6 characters of directions in order: &amp;#39;D&amp;#39; increments the row by 1. &amp;#39;R&amp;#39; increments the column by 1. Record the sequence of digits visited along the path (including the starting cell), producing a sequence of length 7. The integer x is considered good if the recorded sequence is non-decreasing. Return an integer representing the number of good integers in the range [l, r]. &amp;nbsp; Example 1: Input: l = 8, r = 10, directions = &amp;quot;DDDRRR&amp;quot; Output: 2 Explanation: The grid for x = 8: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 Path: (0,0) &amp;rarr; (1,0) &amp;rarr; (2,0) &amp;rarr; (3,0) &amp;rarr; (3,1) &amp;rarr; (3,2) &amp;rarr; (3,3) The sequence of digits visited is [0, 0, 0, 0, 0, 0, 8]. As the sequence of digits visited is non-decreasing, 8 is a good integer. The grid for x = 9: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 The sequence of digits visited is [0, 0, 0, 0, 0, 0, 9]. As the sequence of digits visited is non-decreasing, 9 is a good integer. The grid for x = 10: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 The sequence of digits visited is [0, 0, 0, 0, 0, 1, 0]. As the sequence of digits visited is not non-decreasing, 10 is not a good integer. Hence, only 8 and 9 are good, giving a total of 2 good integers in the range. Example 2: Input: l = 123456789, r = 123456790, directions = &amp;quot;DDRRDR&amp;quot; Output: 1 Explanation: The grid for x = 123456789: 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 Path: (0,0) &amp;rarr; (1,0) &amp;rarr; (2,0) &amp;rarr; (2,1) &amp;rarr; (2,2) &amp;rarr; (3,2) &amp;rarr; (3,3) The sequence of digits visited is [0, 0, 2, 3, 4, 8, 9]. As the sequence of digits visited is non-decreasing, 123456789 is a good integer. The grid for x = 123456790: 0 0 0 0 0 0 0 1 2 3 4 5 6 7 9 0 The sequence of digits visited is [0, 0, 2, 3, 4, 9, 0]. As the sequence of digits visited is not non-decreasing, 123456790 is not a...
        </description>
        <pubDate>Wed, 03 Jun 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-06-03-3906-Count-Good-Integers-on-a-Grid-Path/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-06-03-3906-Count-Good-Integers-on-a-Grid-Path/</guid>
      </item>
    
      <item>
        <title>3905 - Multi Source Flood Fill</title>
        <description>
          
          Welcome to Subscribe On Youtube 3905. Multi Source Flood Fill Description You are given two integers n and m representing the number of rows and columns of a grid, respectively. You are also given a 2D integer array sources, where sources[i] = [ri, ci, color​​​​​​​i] indicates that the cell (ri, ci) is initially colored with colori. All other cells are initially uncolored and represented as 0. At each time step, every currently colored cell spreads its color to all adjacent uncolored cells in the four directions: up, down, left, and right. All spreads happen simultaneously. If multiple colors reach the same uncolored cell at the same time step, the cell takes the color with the maximum value. The process continues until no more cells can be colored. Return a 2D integer array representing the final state of the grid, where each cell contains its final color. &amp;nbsp; Example 1: Input: n = 3, m = 3, sources = [[0,0,1],[2,2,2]] Output: [[1,1,2],[1,2,2],[2,2,2]] Explanation: The grid at each time step is as follows: ​​​​​​​ At time step 2, cells (0, 2), (1, 1), and (2, 0) are reached by both colors, so they are assigned color 2 as it has the maximum value among them. Example 2: Input: n = 3, m = 3, sources = [[0,1,3],[1,1,5]] Output: [[3,3,3],[5,5,5],[5,5,5]] Explanation: The grid at each time step is as follows: Example 3: Input: n = 2, m = 2, sources = [[1,1,5]] Output: [[5,5],[5,5]] Explanation: The grid at each time step is as follows: ​​​​​​​ Since there is only one source, all cells are assigned the same color. &amp;nbsp; Constraints: 1 &amp;lt;= n, m &amp;lt;= 105 1 &amp;lt;= n * m &amp;lt;= 105 1 &amp;lt;= sources.length &amp;lt;= n * m sources[i] = [ri, ci, colori] 0 &amp;lt;= ri &amp;lt;= n - 1 0 &amp;lt;= ci &amp;lt;= m - 1 1 &amp;lt;= colori &amp;lt;= 106​​​​​​​ All (ri, ci​​​​​​​) in sources are distinct. Solutions Solution 1: Multi-source BFS We can use multi-source BFS to simulate this process. Define a queue $q$ to store the cells that are currently spreading their color. Initially, add all source cells to the queue and set their colors in the answer array $\textit{ans}$. In each iteration, we use a hash table $\textit{vis}$ to record the cells visited at the current time step and the maximum color value for each cell. For each cell in the queue, we try to spread its color to the four adjacent directions (up, down, left, right). If a neighboring cell is uncolored, we add it to $\textit{vis}$ and update its color to be the maximum of the current cell’s color and any existing color in $\textit{vis}$. After processing all cells in the current queue, we clear the queue and add the cells from $\textit{vis}$ to the queue, updating their colors in the answer array $\textit{ans}$ accordingly. We repeat this process until the queue is empty, meaning no more cells can be colored. The time complexity is $O(n \times m)$ and the space complexity is $O(n...
        </description>
        <pubDate>Tue, 02 Jun 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-06-02-3905-Multi-Source-Flood-Fill/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-06-02-3905-Multi-Source-Flood-Fill/</guid>
      </item>
    
      <item>
        <title>3904 - Smallest Stable Index II</title>
        <description>
          
          Welcome to Subscribe On Youtube 3904. Smallest Stable Index II Description You are given an integer array nums of length n and an integer k. For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]). In other words: max(nums[0..i]) is the largest value among the elements from index 0 to index i. min(nums[i..n - 1]) is the smallest value among the elements from index i to index n - 1. An index i is called stable if its instability score is less than or equal to k. Return the smallest stable index. If no such index exists, return -1. &amp;nbsp; Example 1: Input: nums = [5,0,1,4], k = 3 Output: 3 Explanation: At index 0: The maximum in [5] is 5, and the minimum in [5, 0, 1, 4] is 0, so the instability score is 5 - 0 = 5. At index 1: The maximum in [5, 0] is 5, and the minimum in [0, 1, 4] is 0, so the instability score is 5 - 0 = 5. At index 2: The maximum in [5, 0, 1] is 5, and the minimum in [1, 4] is 1, so the instability score is 5 - 1 = 4. At index 3: The maximum in [5, 0, 1, 4] is 5, and the minimum in [4] is 4, so the instability score is 5 - 4 = 1. This is the first index with an instability score less than or equal to k = 3. Thus, the answer is 3. Example 2: Input: nums = [3,2,1], k = 1 Output: -1 Explanation: At index 0, the instability score is 3 - 1 = 2. At index 1, the instability score is 3 - 1 = 2. At index 2, the instability score is 3 - 1 = 2. None of these values is less than or equal to k = 1, so the answer is -1. Example 3: Input: nums = [0], k = 0 Output: 0 Explanation: At index 0, the instability score is 0 - 0 = 0, which is less than or equal to k = 0. Therefore, the answer is 0. &amp;nbsp; Constraints: 1 &amp;lt;= nums.length &amp;lt;= 105 0 &amp;lt;= nums[i] &amp;lt;= 109 0 &amp;lt;= k &amp;lt;= 109 Solutions Solution 1: Preprocessing + Enumeration First, we preprocess an array $\textit{right}$, where $\textit{right}[i]$ represents the minimum value among the elements in $nums$ from index $i$ to index $n - 1$. We can compute the $\textit{right}$ array by traversing $nums$ from back to front. Next, we traverse the $nums$ array from front to back, maintaining a variable $\textit{left}$, which represents the maximum value among the elements in $nums$ from index $0$ to index $i$. For each index $i$, we calculate the instability score as $\textit{left} - \textit{right}[i]$. If the instability score is less than or equal to $k$, we return index $i$. If no such index is found after the traversal, we return $-1$. The time complexity is $O(n)$ and the space complexity is...
        </description>
        <pubDate>Mon, 01 Jun 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-06-01-3904-Smallest-Stable-Index-II/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-06-01-3904-Smallest-Stable-Index-II/</guid>
      </item>
    
      <item>
        <title>3903 - Smallest Stable Index I</title>
        <description>
          
          Welcome to Subscribe On Youtube 3903. Smallest Stable Index I Description You are given an integer array nums of length n and an integer k. For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]). In other words: max(nums[0..i]) is the largest value among the elements from index 0 to index i. min(nums[i..n - 1]) is the smallest value among the elements from index i to index n - 1. An index i is called stable if its instability score is less than or equal to k. Return the smallest stable index. If no such index exists, return -1. &amp;nbsp; Example 1: Input: nums = [5,0,1,4], k = 3 Output: 3 Explanation: At index 0: The maximum in [5] is 5, and the minimum in [5, 0, 1, 4] is 0, so the instability score is 5 - 0 = 5. At index 1: The maximum in [5, 0] is 5, and the minimum in [0, 1, 4] is 0, so the instability score is 5 - 0 = 5. At index 2: The maximum in [5, 0, 1] is 5, and the minimum in [1, 4] is 1, so the instability score is 5 - 1 = 4. At index 3: The maximum in [5, 0, 1, 4] is 5, and the minimum in [4] is 4, so the instability score is 5 - 4 = 1. This is the first index with an instability score less than or equal to k = 3. Thus, the answer is 3. Example 2: Input: nums = [3,2,1], k = 1 Output: -1 Explanation: At index 0, the instability score is 3 - 1 = 2. At index 1, the instability score is 3 - 1 = 2. At index 2, the instability score is 3 - 1 = 2. None of these values is less than or equal to k = 1, so the answer is -1. Example 3: Input: nums = [0], k = 0 Output: 0 Explanation: At index 0, the instability score is 0 - 0 = 0, which is less than or equal to k = 0. Therefore, the answer is 0. &amp;nbsp; Constraints: 1 &amp;lt;= nums.length &amp;lt;= 100 0 &amp;lt;= nums[i] &amp;lt;= 109 0 &amp;lt;= k &amp;lt;= 109 Solutions Solution 1: Preprocessing + Enumeration First, we preprocess an array $\textit{right}$, where $\textit{right}[i]$ represents the minimum value among the elements in $nums$ from index $i$ to index $n - 1$. We can compute the $\textit{right}$ array by traversing $nums$ from back to front. Next, we traverse the $nums$ array from front to back, maintaining a variable $\textit{left}$, which represents the maximum value among the elements in $nums$ from index $0$ to index $i$. For each index $i$, we calculate the instability score as $\textit{left} - \textit{right}[i]$. If the instability score is less than or equal to $k$, we return index $i$. If no such index is found after the traversal, we return $-1$. The time complexity is $O(n)$ and the space complexity is...
        </description>
        <pubDate>Sun, 31 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-31-3903-Smallest-Stable-Index-I/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-31-3903-Smallest-Stable-Index-I/</guid>
      </item>
    
      <item>
        <title>3902 - Zigzag Level Sum of Binary Tree</title>
        <description>
          
          Welcome to Subscribe On Youtube 3902. Zigzag Level Sum of Binary Tree 🔒 Description You are given the root of a binary tree. Traverse the tree level by level using a zigzag pattern: At odd-numbered levels (1-indexed), traverse nodes from left to right. At even-numbered levels, traverse nodes from right to left. While traversing a level in the specified direction, process nodes in order and stop immediately before the first node that violates the condition: At odd levels: the node does not have a left child. At even levels: the node does not have a right child. Only the nodes processed before this stopping condition contribute to the level sum. Return an integer array ans where ans[i] is the sum of the node values that are processed at level i + 1. &amp;nbsp; Example 1: Input: root = [5,2,8,1,null,9,6] Output: [5,8,0] Explanation: ​​​​​​​ At level 1, nodes are processed left to right. Node 5 is included, thus ans[0] = 5. At level 2, nodes are processed right to left. Node 8 is included, but node 2 lacks a right child, so processing stops, thus ans[1] = 8. At level 3, nodes are processed left to right. The first node 1 lacks a left child, so no nodes are included, and ans[2] = 0. Thus, ans = [5, 8, 0]. Example 2: Input: root = [1,2,3,4,5,null,7] Output: [1,5,0] Explanation: At level 1, nodes are processed left to right. Node 1 is included, thus ans[0] = 1. At level 2, nodes are processed right to left. Nodes 3 and 2 are included since both have right children, thus ans[1] = 3 + 2 = 5. At level 3, nodes are processed left to right. The first node 4 lacks a left child, so no nodes are included, and ans[2] = 0. Thus, ans = [1, 5, 0]. &amp;nbsp; Constraints: The number of nodes in the tree is in the range [1, 105]. -105 &amp;lt;= Node.val &amp;lt;= 105 Solutions Solution 1: BFS We use a queue $q$ to perform a level-order traversal, and define a boolean variable $\textit{left}$ to indicate the traversal direction of the current level. For each level, we first add the nodes of the next level to the queue $nq$, and then compute the sum of the node values of the current level, denoted by $s$, according to the value of $\textit{left}$, and append $s$ to the answer array. Finally, we update the value of $\textit{left}$ and assign $nq$ to $q$ to continue traversing the next level. The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. Java C++ Python Go TypeScript /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } *...
        </description>
        <pubDate>Sat, 30 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-30-3902-Zigzag-Level-Sum-of-Binary-Tree/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-30-3902-Zigzag-Level-Sum-of-Binary-Tree/</guid>
      </item>
    
      <item>
        <title>3901 - Good Subsequence Queries</title>
        <description>
          
          Welcome to Subscribe On Youtube 3901. Good Subsequence Queries Description You are given an integer array nums of length n and an integer p. A non-empty subsequence of nums is called good if: Its length is strictly less than n. The greatest common divisor (GCD) of its elements is exactly p. You are also given a 2D integer array queries of length q, where each queries[i] = [indi, vali] indicates that you should update nums[indi] to vali. After each query, determine whether there exists any good subsequence in the current array. Return the number of queries for which a good subsequence exists. The term gcd(a, b) denotes the greatest common divisor of a and b. &amp;nbsp; Example 1: Input: nums = [4,8,12,16], p = 2, queries = [[0,3],[2,6]] Output: 1 Explanation: i [indi, vali] Operation Updated nums Any good Subsequence 0 [0, 3] Update nums[0] to 3 [3, 8, 12, 16] No, as no subsequence has GCD exactly p = 2 1 [2, 6] Update nums[2] to 6 [3, 8, 6, 16] Yes, subsequence [8, 6] has GCD exactly p = 2 Thus, the answer is 1. Example 2: Input: nums = [4,5,7,8], p = 3, queries = [[0,6],[1,9],[2,3]] Output: 2 Explanation: i [indi, vali] Operation Updated nums Any good Subsequence 0 [0, 6] Update nums[0] to 6 [6, 5, 7, 8] No, as no subsequence has GCD exactly p = 3 1 [1, 9] Update nums[1] to 9 [6, 9, 7, 8] Yes, subsequence [6, 9] has GCD exactly p = 3 2 [2, 3] Update nums[2] to 3 [6, 9, 3, 8] Yes, subsequence [6, 9, 3] has GCD exactly p = 3 Thus, the answer is 2. Example 3: Input: nums = [5,7,9], p = 2, queries = [[1,4],[2,8]] Output: 0 Explanation: i [indi, vali] Operation Updated nums Any good Subsequence 0 [1, 4] Update nums[1] to 4 [5, 4, 9] No, as no subsequence has GCD exactly p = 2 1 [2, 8] Update nums[2] to 8 [5, 4, 8] No, as no subsequence has GCD exactly p = 2 Thus, the answer is 0. &amp;nbsp; Constraints: 2 &amp;lt;= n == nums.length &amp;lt;= 5 * 104 1 &amp;lt;= nums[i] &amp;lt;= 5 * 104 1 &amp;lt;= queries.length &amp;lt;= 5 * 104 queries[i] = [indi, vali] 1 &amp;lt;= vali, p &amp;lt;= 5 * 104 0 &amp;lt;= indi &amp;lt;= n - 1 Solutions Solution 1: Segment Tree + GCD We only care about numbers that are multiples of $p$, because if a number is not divisible by $p$, it can never belong to a subsequence whose GCD is exactly $p$. Therefore, we can treat positions whose values are not divisible by $p$ as $0$, and only maintain the following value for each position in the segment tree: If $\textit{nums}[i]$ is divisible by $p$, store its actual value in the segment tree. Otherwise, store $0$. In this way, the whole segment tree maintains the GCD of all current multiples of $p$. Denote it by $g$: If $g \ne p$, then...
        </description>
        <pubDate>Fri, 29 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-29-3901-Good-Subsequence-Queries/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-29-3901-Good-Subsequence-Queries/</guid>
      </item>
    
      <item>
        <title>3900 - Longest Balanced Substring After One Swap</title>
        <description>
          
          Welcome to Subscribe On Youtube 3900. Longest Balanced Substring After One Swap Description You are given a binary string s consisting only of characters &amp;#39;0&amp;#39; and &amp;#39;1&amp;#39;. A string is balanced if it contains an equal number of &amp;#39;0&amp;#39;s and &amp;#39;1&amp;#39;s. You can perform at most one swap between any two characters in s. Then, you select a balanced substring from s. Return an integer representing the maximum length of the balanced substring you can select. &amp;nbsp; Example 1: Input: s = &amp;quot;100001&amp;quot; Output: 4 Explanation: Swap &amp;quot;100001&amp;quot;. The string becomes &amp;quot;101000&amp;quot;. Select the substring &amp;quot;101000&amp;quot;, which is balanced because it has two &amp;#39;0&amp;#39;s and two &amp;#39;1&amp;#39;s. Example 2: Input: s = &amp;quot;111&amp;quot; Output: 0 Explanation: Choose not to perform any swaps. Select the empty substring, which is balanced because it has zero &amp;#39;0&amp;#39;s and zero &amp;#39;1&amp;#39;s. &amp;nbsp; Constraints: 1 &amp;lt;= s.length &amp;lt;= 105 s consists only of the characters &amp;#39;0&amp;#39; and &amp;#39;1&amp;#39;. Solutions Solution 1: Prefix Sum + Hash Table Let the prefix sum $\textit{pre}$ denote the number of 1s minus the number of 0s in the current prefix. Then for any substring, if the numbers of 0s and 1s are equal, its corresponding prefix sum difference is $0$. Therefore, if the prefix sum at position $i$ is $x$, and some previous position also has prefix sum $x$, then the substring between these two positions is balanced, and we can directly use it to update the answer. Now the problem allows us to perform at most one swap between any two characters. One swap can only reduce the difference between the counts of 1 and 0 in a substring by $2$. So besides the case where the prefix sum difference is $0$, we also need to consider: A prefix sum difference of $2$, which means the substring contains 2 more 1s than 0s. In this case, if there is still at least one 0 outside the substring, we can make it balanced with one swap. A prefix sum difference of $-2$, which means the substring contains 2 more 0s than 1s. Similarly, if there is still at least one 1 outside the substring, we can make it balanced with one swap. To do this, we first count the total numbers of 0s and 1s in the whole string, denoted by $\textit{cnt0}$ and $\textit{cnt1}$. Then we use a hash table to record all positions where each prefix sum appears. While traversing the string up to position $i$, let the current prefix sum be $\textit{pre}$: Use the earliest occurrence of $\textit{pre}$ to update the longest balanced substring length without any swap. If prefix sum $\textit{pre} - 2$ exists, then we can try to form a substring with 2 more 1s than 0s. Suppose its length is $L$. Then the number of 0s inside it is $(L - 2) / 2$. Only when this value is strictly less than $\textit{cnt0}$ do we know there is at least one 0 outside the substring that can be swapped in. If prefix sum $\textit{pre} +...
        </description>
        <pubDate>Thu, 28 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-28-3900-Longest-Balanced-Substring-After-One-Swap/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-28-3900-Longest-Balanced-Substring-After-One-Swap/</guid>
      </item>
    
      <item>
        <title>3899 - Angles of a Triangle</title>
        <description>
          
          Welcome to Subscribe On Youtube 3899. Angles of a Triangle Description You are given a positive integer array sides of length 3. Determine if there exists a triangle with positive area whose three side lengths are given by the elements of sides. If such a triangle exists, return an array of three floating-point numbers representing its internal angles (in degrees), sorted in non-decreasing order. Otherwise, return an empty array. Answers within 10-5 of the actual answer will be accepted. &amp;nbsp; Example 1: Input: sides = [3,4,5] Output: [36.86990,53.13010,90.00000] Explanation: You can form a right-angled triangle with side lengths 3, 4, and 5. The internal angles of this triangle are approximately 36.869897646, 53.130102354, and 90 degrees respectively. Example 2: Input: sides = [2,4,2] Output: [] Explanation: You cannot form a triangle with positive area using side lengths 2, 4, and 2. &amp;nbsp; Constraints: sides.length == 3 1 &amp;lt;= sides[i] &amp;lt;= 1000 Solutions Solution 1: Sorting + Math We first sort the array $\textit{sides}$ in non-decreasing order, and denote the three side lengths as $a$, $b$, and $c$, where $a \le b \le c$. According to the triangle inequality, if $a + b \le c$, then these three sides cannot form a triangle with positive area, so we return an empty array directly. Otherwise, the three sides can form a valid triangle. By the law of cosines, we have: \[\cos A = \frac{b^2 + c^2 - a^2}{2bc}\] \[\cos B = \frac{a^2 + c^2 - b^2}{2ac}\] Therefore, we can compute angles $A$ and $B$ separately. Finally, using the fact that the sum of the internal angles of a triangle is $180^\circ$, we get: \[C = 180^\circ - A - B\] Finally, we return the three internal angles. The time complexity is $O(1)$, and the space complexity is $O(1)$. Java C++ Python Go TypeScript class Solution { public double[] internalAngles(int[] sides) { Arrays.sort(sides); int a = sides[0], b = sides[1], c = sides[2]; if (a + b &amp;lt;= c) { return new double[0]; } double A = Math.toDegrees(Math.acos((b * b + c * c - a * a) / (2.0 * b * c))); double B = Math.toDegrees(Math.acos((a * a + c * c - b * b) / (2.0 * a * c))); double C = 180.0 - A - B; return new double[] {A, B, C}; } } class Solution { public: vector&amp;lt;double&amp;gt; internalAngles(vector&amp;lt;int&amp;gt;&amp;amp; sides) { sort(sides.begin(), sides.end()); int a = sides[0], b = sides[1], c = sides[2]; if (a + b &amp;lt;= c) { return {}; } double A = acos((1.0 * b * b + 1.0 * c * c - 1.0 * a * a) / (2.0 * b * c)) * 180.0 / acos(-1.0); double B = acos((1.0 * a * a + 1.0 * c * c - 1.0 * b * b) / (2.0 * a * c)) * 180.0 / acos(-1.0); double C = 180.0 - A - B; return {A, B, C}; } }; class Solution: def internalAngles(self, sides: list[int]) -&amp;gt; list[float]: sides.sort() a,...
        </description>
        <pubDate>Wed, 27 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-27-3899-Angles-of-a-Triangle/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-27-3899-Angles-of-a-Triangle/</guid>
      </item>
    
      <item>
        <title>3898 - Find the Degree of Each Vertex</title>
        <description>
          
          Welcome to Subscribe On Youtube 3898. Find the Degree of Each Vertex Description You are given a 2D integer array matrix of size n x n representing the adjacency matrix of an undirected graph with n vertices labeled from 0 to n - 1. matrix[i][j] = 1 indicates that there is an edge between vertices i and j. matrix[i][j] = 0 indicates that there is no edge between vertices i and j. The degree of a vertex is the number of edges connected to it. Return an integer array ans of size n where ans[i] represents the degree of vertex i. &amp;nbsp; Example 1: Input: matrix = [[0,1,1],[1,0,1],[1,1,0]] Output: [2,2,2] Explanation: Vertex 0 is connected to vertices 1 and 2, so its degree is 2. Vertex 1 is connected to vertices 0 and 2, so its degree is 2. Vertex 2 is connected to vertices 0 and 1, so its degree is 2. Thus, the answer is [2, 2, 2]. Example 2: Input: matrix = [[0,1,0],[1,0,0],[0,0,0]] Output: [1,1,0] Explanation: Vertex 0 is connected to vertex 1, so its degree is 1. Vertex 1 is connected to vertex 0, so its degree is 1. Vertex 2 is not connected to any vertex, so its degree is 0. Thus, the answer is [1, 1, 0]. Example 3: Input: matrix = [[0]] Output: [0] Explanation: There is only one vertex and it has no edges connected to it. Thus, the answer is [0]. &amp;nbsp; Constraints: 1 &amp;lt;= n == matrix.length == matrix[i].length &amp;lt;= 100​​​​​​​ ​​​​​​​matrix[i][i] == 0 matrix[i][j] is either 0 or 1 matrix[i][j] == matrix[j][i] Solutions Solution 1: Simulation We can directly simulate the process of computing the degree of each vertex. For each vertex $i$, we traverse its corresponding row $\text{matrix}[i]$ and count the number of elements equal to 1, which is exactly the degree of vertex $i$. The time complexity is $O(n^2)$, where $n$ is the number of vertices in the graph. Ignoring the space consumed by the answer array, the space complexity is $O(1)$. Java C++ Python Go TypeScript class Solution { public int[] findDegrees(int[][] matrix) { int n = matrix.length; int[] ans = new int[n]; for (int i = 0; i &amp;lt; n; ++i) { for (int x : matrix[i]) { ans[i] += x; } } return ans; } } class Solution { public: vector&amp;lt;int&amp;gt; findDegrees(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; matrix) { int n = matrix.size(); vector&amp;lt;int&amp;gt; ans(n); for (int i = 0; i &amp;lt; n; ++i) { for (int x : matrix[i]) { ans[i] += x; } } return ans; } }; class Solution: def findDegrees(self, matrix: list[list[int]]) -&amp;gt; list[int]: ans = [0] * len(matrix) for i, row in enumerate(matrix): for x in row: ans[i] += x return ans func findDegrees(matrix [][]int) []int { ans := make([]int, len(matrix)) for i, row := range matrix { for _, x := range row { ans[i] += x } } return ans } function findDegrees(matrix: number[][]): number[] { const n = matrix.length; const ans: number[] = Array(n).fill(0); for (let i = 0; i &amp;lt;...
        </description>
        <pubDate>Tue, 26 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-26-3898-Find-the-Degree-of-Each-Vertex/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-26-3898-Find-the-Degree-of-Each-Vertex/</guid>
      </item>
    
      <item>
        <title>3897 - Maximum Value of Concatenated Binary Segments</title>
        <description>
          
          Welcome to Subscribe On Youtube 3897. Maximum Value of Concatenated Binary Segments Description You are given two integer arrays nums1 and nums0, each of size n. nums1[i] represents the number of &amp;#39;1&amp;#39;s in the ith segment. nums0[i] represents the number of &amp;#39;0&amp;#39;s in the ith segment. For each index i, construct a binary segment consisting of: nums1[i] occurrences of &amp;#39;1&amp;#39; followed by nums0[i] occurrences of &amp;#39;0&amp;#39;. You may rearrange the order of these segments in any way. After rearranging, concatenate all segments to form a single binary string. Return the maximum possible integer value of the concatenated binary string. Since the result can be very large, return the answer modulo 109 + 7. &amp;nbsp; Example 1: Input: nums1 = [1,2], nums0 = [1,0] Output: 14 Explanation: At index 0, nums1[0] = 1 and nums0[0] = 1, so the segment formed is &amp;quot;10&amp;quot;. At index 1, nums1[1] = 2 and nums0[1] = 0, so the segment formed is &amp;quot;11&amp;quot;. Reordering the segments as &amp;quot;11&amp;quot; followed by &amp;quot;10&amp;quot; produces the binary string &amp;quot;1110&amp;quot;. The binary number &amp;quot;1110&amp;quot; has value 14 which is the maximum possible value. Example 2: Input: nums1 = [3,1], nums0 = [0,3] Output: 120 Explanation: At index 0, nums1[0] = 3 and nums0[0] = 0, so the segment formed is &amp;quot;111&amp;quot;. At index 1, nums1[1] = 1 and nums0[1] = 3, so the segment formed is &amp;quot;1000&amp;quot;. Reordering the segments as &amp;quot;111&amp;quot; followed by &amp;quot;1000&amp;quot; produces the binary string &amp;quot;1111000&amp;quot;. The binary number &amp;quot;1111000&amp;quot; has value 120 which is the maximum possible value. &amp;nbsp; Constraints: 1 &amp;lt;= n == nums1.length == nums0.length &amp;lt;= 105 0 &amp;lt;= nums1[i], nums0[i] &amp;lt;= 104 nums1[i] + nums0[i] &amp;gt; 0 The total sum of all elements in nums1 and nums0 does not exceed 2 * 105. Solutions Solution 1: Sorting + Greedy Let the binary string corresponding to the $i$-th segment be $1^{x_i}0^{y_i}$, where $x_i = \textit{nums1}[i]$ and $y_i = \textit{nums0}[i]$. The problem allows us to rearrange these segments arbitrarily, and the goal is to maximize the integer value represented by the final concatenated binary string. Since comparing binary strings by value is essentially equivalent to comparing them lexicographically, we want as many 1s as possible to appear earlier. Consider the relative order of two segments $A = 1^a0^b$ and $B = 1^c0^d$. If we concatenate them as $AB$ or $BA$, we should clearly choose the one with the larger lexicographical order. Based on this rule, we can derive the following sorting strategy: If a segment satisfies $y = 0$, it consists only of some 1s. Such segments should be placed as early as possible because they do not introduce any 0 prematurely. Among these segments, the one with more 1s should come first. If two segments both satisfy $x &amp;gt; 0$ and $y &amp;gt; 0$, then the segment with more leading 1s should come first, so we sort by $x$ in descending order. If $x$ is the same, then the segment with fewer 0s should come first, so we sort by $y$ in...
        </description>
        <pubDate>Mon, 25 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-25-3897-Maximum-Value-of-Concatenated-Binary-Segments/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-25-3897-Maximum-Value-of-Concatenated-Binary-Segments/</guid>
      </item>
    
      <item>
        <title>3896 - Minimum Operations to Transform Array into Alternating Prime</title>
        <description>
          
          Welcome to Subscribe On Youtube 3896. Minimum Operations to Transform Array into Alternating Prime Description You are given an integer array nums. An array is considered alternating prime if: Elements at even indices (0-based) are prime numbers. Elements at odd indices are non-prime numbers. In one operation, you may increment any element by 1. Return the minimum number of operations required to transform nums into an alternating prime array. A prime number is a natural number greater than 1 with only two factors, 1 and itself. &amp;nbsp; Example 1: Input: nums = [1,2,3,4] Output: 3 Explanation: The element at index 0 must be prime. Increment nums[0] = 1 to 2, using 1 operation. The element at index 1 must be non-prime. Increment nums[1] = 2 to 4, using 2 operations. The element at index 2 is already prime. The element at index 3 is already non-prime. Total operations = 1 + 2 = 3. Example 2: Input: nums = [5,6,7,8] Output: 0 Explanation: The elements at indices 0 and 2 are already prime. The elements at indices 1 and 3 are already non-prime. No operations are needed. Example 3: Input: nums = [4,4] Output: 1 Explanation: The element at index 0 must be prime. Increment nums[0] = 4 to 5, using 1 operation. The element at index 1 is already non-prime. Total operations = 1. &amp;nbsp; Constraints: 1 &amp;lt;= nums.length &amp;lt;= 105 1 &amp;lt;= nums[i] &amp;lt;= 105 Solutions Solution 1: Preprocessing + Binary Search We can first preprocess a sufficiently large list of prime numbers, denoted as $\textit{primes}$, and a boolean array $\textit{isPrime}$, where $\textit{isPrime}[i]$ indicates whether $i$ is a prime number. Then we traverse each element in the array: If the index of the current element is even, we need to increase it to the next prime number. We can use binary search on $\textit{primes}$ to find the first prime number greater than or equal to the current element, and add the difference between them to the answer. If the index of the current element is odd and the current element is prime, we need to increase it to the next non-prime number. For the prime number 2, we need 2 increments to reach the next non-prime number 4; for other prime numbers, we only need 1 increment to reach the next non-prime number. Finally, return the answer. The time complexity is $O(n \times \log P)$, and the space complexity is $O(P)$. Here, $n$ and $P$ are the length of the array and the length of the preprocessed prime list, respectively. Java C++ Python Go TypeScript class Solution { private static final int MX = 200000; private static final boolean[] IS_PRIME = new boolean[MX + 1]; private static final List&amp;lt;Integer&amp;gt; PRIMES = new ArrayList&amp;lt;&amp;gt;(); static { Arrays.fill(IS_PRIME, true); IS_PRIME[0] = false; IS_PRIME[1] = false; for (int i = 2; i &amp;lt;= MX / i; ++i) { if (IS_PRIME[i]) { for (int j = i * i; j &amp;lt;= MX; j += i) { IS_PRIME[j] = false; } } }...
        </description>
        <pubDate>Sun, 24 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-24-3896-Minimum-Operations-to-Transform-Array-into-Alternating-Prime/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-24-3896-Minimum-Operations-to-Transform-Array-into-Alternating-Prime/</guid>
      </item>
    
      <item>
        <title>3895 - Count Digit Appearances</title>
        <description>
          
          Welcome to Subscribe On Youtube






3895. Count Digit Appearances

Description



You are given an integer array nums and an integer digit.

Return the total number of times digit appears in the decimal representation of all elements in nums.

&amp;nbsp;
Example 1:


Input: nums = [12,54,32,22], digit = 2

Output: 4

Explanation:

The digit 2 appears once in 12 and 32, and twice in 22. Thus, the total number of times digit 2 appears is 4.


Example 2:


Input: nums = [1,34,7], digit = 9

Output: 0

Explanation:

The digit 9 does not appear in the decimal representation of any element in nums, so the total number of times digit 9 appears is 0.


&amp;nbsp;
Constraints:


	1 &amp;lt;= nums.length &amp;lt;= 1000
	1 &amp;lt;= nums[i] &amp;lt;= 106​​​​​​​
	0 &amp;lt;= digit &amp;lt;= 9




Solutions



Solution 1: Simulation

We traverse each element in the array and count how many times $\textit{digit}$ appears. For each element, we can obtain each of its digits by repeatedly taking the modulo and dividing by 10, and compare each digit with $\textit{digit}$. If they are equal, we increment the answer by 1.

Finally, return the answer.

The time complexity is $O(n \times \log_{10} M)$, and the space complexity is $O(1)$. Here, $n$ and $M$ are the length of the array and the maximum value in the array, respectively.



	Java

	C++

	Python

	Go

	TypeScript





	
class Solution {
    public int countDigitOccurrences(int[] nums, int digit) {
        int ans = 0;
        for (int x : nums) {
            for (; x &amp;gt; 0; x /= 10) {
                if (x % 10 == digit) {
                    ++ans;
                }
            }
        }
        return ans;
    }
}




	
class Solution {
public:
    int countDigitOccurrences(vector&amp;lt;int&amp;gt;&amp;amp; nums, int digit) {
        int ans = 0;
        for (int x : nums) {
            for (; x &amp;gt; 0; x /= 10) {
                if (x % 10 == digit) {
                    ++ans;
                }
            }
        }
        return ans;
    }
};




	
class Solution:
    def countDigitOccurrences(self, nums: list[int], digit: int) -&amp;gt; int:
        ans = 0
        for x in nums:
            while x:
                v = x % 10
                if v == digit:
                    ans += 1
                x //= 10
        return ans




	
func countDigitOccurrences(nums []int, digit int) (ans int) {
	for _, x := range nums {
		for ; x &amp;gt; 0; x /= 10 {
			if x%10 == digit {
				ans++
			}
		}
	}
	return
}




	
function countDigitOccurrences(nums: number[], digit: number): number {
    let ans = 0;
    for (let x of nums) {
        for (; x; x = Math.floor(x / 10)) {
            if (x % 10 === digit) {
                ++ans;
            }
        }
    }
    return ans;
}






All Problems

All Solutions

        </description>
        <pubDate>Sat, 23 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-23-3895-Count-Digit-Appearances/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-23-3895-Count-Digit-Appearances/</guid>
      </item>
    
      <item>
        <title>3894 - Traffic Signal Color</title>
        <description>
          
          Welcome to Subscribe On Youtube






3894. Traffic Signal Color

Description



You are given an integer timer representing the remaining time (in seconds) on a traffic signal.

The signal follows these rules:


	If timer == 0, the signal is &amp;quot;Green&amp;quot;
	If timer == 30, the signal is &amp;quot;Orange&amp;quot;
	If 30 &amp;lt; timer &amp;lt;= 90, the signal is &amp;quot;Red&amp;quot;


Return the current state of the signal. If none of the above conditions are met, return &amp;quot;Invalid&amp;quot;.

&amp;nbsp;
Example 1:


Input: timer = 60

Output: &amp;quot;Red&amp;quot;

Explanation:

Since timer = 60, and 30 &amp;lt; timer &amp;lt;= 90, the answer is &amp;quot;Red&amp;quot;.


Example 2:


Input: timer = 5

Output: &amp;quot;Invalid&amp;quot;

Explanation:

Since timer = 5, it does not satisfy any of the given conditions, the answer is &amp;quot;Invalid&amp;quot;.


&amp;nbsp;
Constraints:


	0 &amp;lt;= timer &amp;lt;= 1000




Solutions



Solution 1: Simulation

We determine the answer according to the conditions described in the problem and return the corresponding string.

The time complexity is $O(1)$, and the space complexity is $O(1)$.



	Java

	C++

	Python

	Go

	TypeScript





	
class Solution {
    public String trafficSignal(int timer) {
        if (timer == 0) {
            return &quot;Green&quot;;
        }
        if (timer == 30) {
            return &quot;Orange&quot;;
        }
        if (timer &amp;gt; 30 &amp;amp;&amp;amp; timer &amp;lt;= 90) {
            return &quot;Red&quot;;
        }
        return &quot;Invalid&quot;;
    }
}




	
class Solution {
public:
    string trafficSignal(int timer) {
        if (timer == 0) {
            return &quot;Green&quot;;
        }
        if (timer == 30) {
            return &quot;Orange&quot;;
        }
        if (timer &amp;gt; 30 &amp;amp;&amp;amp; timer &amp;lt;= 90) {
            return &quot;Red&quot;;
        }
        return &quot;Invalid&quot;;
    }
};




	
class Solution:
    def trafficSignal(self, timer: int) -&amp;gt; str:
        if timer == 0:
            return &quot;Green&quot;
        if timer == 30:
            return &quot;Orange&quot;
        if 30 &amp;lt; timer &amp;lt;= 90:
            return &quot;Red&quot;
        return &quot;Invalid&quot;




	
func trafficSignal(timer int) string {
	switch {
	case timer == 0:
		return &quot;Green&quot;
	case timer == 30:
		return &quot;Orange&quot;
	case timer &amp;gt; 30 &amp;amp;&amp;amp; timer &amp;lt;= 90:
		return &quot;Red&quot;
	default:
		return &quot;Invalid&quot;
	}
}




	
function trafficSignal(timer: number): string {
    if (timer === 0) {
        return &apos;Green&apos;;
    }
    if (timer === 30) {
        return &apos;Orange&apos;;
    }
    if (timer &amp;gt; 30 &amp;amp;&amp;amp; timer &amp;lt;= 90) {
        return &apos;Red&apos;;
    }
    return &apos;Invalid&apos;;
}






All Problems

All Solutions

        </description>
        <pubDate>Fri, 22 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-22-3894-Traffic-Signal-Color/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-22-3894-Traffic-Signal-Color/</guid>
      </item>
    
      <item>
        <title>3893 - Maximum Team Size with Overlapping Intervals</title>
        <description>
          
          Welcome to Subscribe On Youtube 3893. Maximum Team Size with Overlapping Intervals 🔒 Description You are given two integer arrays startTime and endTime of length n. startTime[i] represents the start time of the ith employee. endTime[i] represents the end time of the ith employee. Two employees i and j can interact if their time intervals overlap. Two intervals are considered overlapping if they share at least one common time point. A team is valid if there exists at least one employee in the team who can interact with every other member of the team. Return an integer denoting the maximum possible size of such a team. &amp;nbsp; Example 1: Input: startTime = [1,2,3], endTime = [4,5,6] Output: 3 Explanation: For i = 0 with interval [1, 4]. It overlaps with i = 1 having interval [2, 5] and i = 2 having interval [3, 6]. Thus, index 0 can interact with all other indices, so the team size is 3. Example 2: Input: startTime = [2,5,8], endTime = [3,7,9] Output: 1 Explanation: For i = 0, interval [2, 3] does not overlap with [5, 7] or [8, 9]. For i = 1, interval [5, 7] does not overlap with [2, 3] or [8, 9]. For i = 2, interval [8, 9] does not overlap with [2, 3] or [5, 7]. Thus, no index can interact with others, so the maximum team size is 1. Example 3: Input: startTime = [3,4,6], endTime = [8,5,7] Output: 3 Explanation: For i = 0 with interval [3, 8]. It overlaps with i = 1 having interval [4, 5] and i = 2 having interval [6, 7]. Thus, index 0 can interact with all other indices, so the team size is 3. &amp;nbsp; Constraints: 1 &amp;lt;= n == startTime.length == endTime.length &amp;lt;= 105 0 &amp;lt;= startTime[i] &amp;lt;= endTime[i] &amp;lt;= 109 Solutions Solution 1: Binary Search We first combine each employee’s start and end times into an interval array, $\textit{intervals}$, and sort all start times and end times separately. For each employee $i$, we use binary search to compute how many employees have end times not earlier than employee $i$’s start time, and how many employees have start times not later than employee $i$’s end time. The difference between these two counts is the number of employees whose intervals overlap with employee $i$. We iterate through all employees, compute the overlap count for each one, and take the maximum as the answer. The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of employees. Java C++ Python Go TypeScript class Solution { public int maximumTeamSize(int[] startTime, int[] endTime) { int n = startTime.length; int[][] intervals = new int[n][2]; for (int i = 0; i &amp;lt; n; i++) { intervals[i][0] = startTime[i]; intervals[i][1] = endTime[i]; } Arrays.sort(startTime); Arrays.sort(endTime); int ans = 0; for (int[] it : intervals) { int l = it[0], r = it[1]; int i = search(endTime, l - 1); int j = search(startTime, r); ans...
        </description>
        <pubDate>Thu, 21 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-21-3893-Maximum-Team-Size-with-Overlapping-Intervals/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-21-3893-Maximum-Team-Size-with-Overlapping-Intervals/</guid>
      </item>
    
      <item>
        <title>3892 - Minimum Operations to Achieve At Least K Peaks</title>
        <description>
          
          Welcome to Subscribe On Youtube






3892. Minimum Operations to Achieve At Least K Peaks

Description



You are given a ​​​​​​​circular integer array​​​​​​​ nums of length n.

An index i is a peak if its value is strictly greater than its neighbors:


	The previous neighbor of i is nums[i - 1] if i &amp;gt; 0, otherwise nums[n - 1].
	The next neighbor of i is nums[i + 1] if i &amp;lt; n - 1, otherwise nums[0].


You are allowed to perform the following operation any number of times:


	Choose any index i and increase nums[i] by 1.


Return an integer denoting the minimum number of operations required to make the array contain at least k peaks. If it is impossible, return -1.

&amp;nbsp;
Example 1:


Input: nums = [2,1,2], k = 1

Output: 1

Explanation:


	To achieve at least k = 1 peak, we can increase nums[2] = 2 to 3.
	After this operation, nums[2] = 3 is strictly greater than its neighbors nums[0] = 2 and nums[1] = 1.
	Therefore, the minimum number of operations required is 1.



Example 2:


Input: nums = [4,5,3,6], k = 2

Output: 0

Explanation:


	The array already contains at least k = 2 peaks with zero operations.
	Index 1: nums[1] = 5 is strictly greater than its neighbors nums[0] = 4 and nums[2] = 3.
	Index 3: nums[3] = 6 is strictly greater than its neighbors nums[2] = 3 and nums[0] = 4.
	Therefore, the minimum number of operations required is 0.



Example 3:


Input: nums = [3,7,3], k = 2

Output: -1

Explanation:

It is impossible to have at least k = 2 peaks in this array. Therefore, the answer is -1.


&amp;nbsp;
Constraints:


	2 &amp;lt;= n == nums.length &amp;lt;= 5000
	-105 &amp;lt;= nums[i] &amp;lt;= 105
	0 &amp;lt;= k &amp;lt;= n​​​​​​​




Solutions



Solution 1

All Problems

All Solutions

        </description>
        <pubDate>Wed, 20 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-20-3892-Minimum-Operations-to-Achieve-At-Least-K-Peaks/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-20-3892-Minimum-Operations-to-Achieve-At-Least-K-Peaks/</guid>
      </item>
    
      <item>
        <title>3891 - Minimum Increase to Maximize Special Indices</title>
        <description>
          
          Welcome to Subscribe On Youtube 3891. Minimum Increase to Maximize Special Indices Description You are given an integer array nums of length n. An index i (0 &amp;lt; i &amp;lt; n - 1) is special if nums[i] &amp;gt; nums[i - 1] and nums[i] &amp;gt; nums[i + 1]. You may perform operations where you choose any index i and increase nums[i] by 1. Your goal is to: Maximize the number of special indices. Minimize the total number of operations required to achieve that maximum. Return an integer denoting the minimum total number of operations required. &amp;nbsp; Example 1: Input: nums = [1,2,2] Output: 1 Explanation:​​​​​​​ Start with nums = [1, 2, 2]. Increase nums[1] by 1, array becomes [1, 3, 2]. The final array is [1, 3, 2] has 1 special index, which is the maximum achievable. It is impossible to achieve this number of special indices with fewer operations. Thus, the answer is 1. Example 2: Input: nums = [2,1,1,3] Output: 2 Explanation:​​​​​​​ Start with nums = [2, 1, 1, 3]. Perform 2 operations at index 1, array becomes [2, 3, 1, 3]. The final array is [2, 3, 1, 3] has 1 special index, which is the maximum achievable. Thus, the answer is 2. Example 3: Input: nums = [5,2,1,4,3] Output: 4 Explanation:​​​​​​​​​​​​​​​​​​​​​ Start with nums = [5, 2, 1, 4, 3]. Perform 4 operations at index 1, array becomes [5, 6, 1, 4, 3]. The final array is [5, 6, 1, 4, 3] has 2 special indices, which is the maximum achievable. Thus, the answer is 4.​​​​​​​ &amp;nbsp; Constraints: 3 &amp;lt;= n &amp;lt;= 105 1 &amp;lt;= nums[i] &amp;lt;= 109 Solutions Solution 1: Memoized Search We observe that if the array length is odd, then increasing all elements at odd indices so that each is $1$ greater than both adjacent elements yields the maximum possible number of special indices. If the array length is even, then among indices in the range $[1, n - 2]$, we skip exactly one index, and for the remaining indices, increase every other element so that each is $1$ greater than both adjacent elements; this also yields the maximum possible number of special indices. Therefore, we design a function $\text{dfs}(i, j)$, which represents the minimum number of operations needed to obtain the maximum number of special indices starting from index $i$, with $j$ remaining skips. For each index $i$, we can either increase it so that it is $1$ greater than both neighbors, or skip it. We use memoized search to avoid repeated computation. The implementation of $\text{dfs}(i, j)$ is as follows: If $i \geq n - 1$, return $0$. Compute the number of operations required to increase $nums[i]$ so that it is $1$ greater than both adjacent elements, denoted as $cost$. Compute the total cost for choosing to increase $nums[i]$: $cost + \text{dfs}(i + 2, j)$. If $j &amp;gt; 0$, compute the total cost for choosing to skip $nums[i]$: $\text{dfs}(i + 1, 0)$, and update $ans$ to the smaller of the two. Finally,...
        </description>
        <pubDate>Tue, 19 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-19-3891-Minimum-Increase-to-Maximize-Special-Indices/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-19-3891-Minimum-Increase-to-Maximize-Special-Indices/</guid>
      </item>
    
      <item>
        <title>3890 - Integers With Multiple Sum of Two Cubes</title>
        <description>
          
          Welcome to Subscribe On Youtube 3890. Integers With Multiple Sum of Two Cubes Description You are given an integer n. An integer x is considered good if there exist at least two distinct pairs (a, b) such that: a and b are positive integers. a &amp;lt;= b x = a3 + b3 Return an array containing all good integers less than or equal to n, sorted in ascending order. &amp;nbsp; Example 1: Input: n = 4104 Output: [1729,4104] Explanation: Among integers less than or equal to 4104, the good integers are: 1729: 13 + 123 = 1729 and 93 + 103 = 1729. 4104: 23 + 163 = 4104 and 93 + 153 = 4104. Thus, the answer is [1729, 4104]. Example 2: Input: n = 578 Output: [] Explanation: There are no good integers less than or equal to 578, so the answer is an empty array. &amp;nbsp; Constraints: 1 &amp;lt;= n &amp;lt;= 109 Solutions Solution 1: Preprocessing + Binary Search We observe that when $a$ or $b$ is greater than $1000$, the expression $a^3 + b^3 &amp;gt; 10^9$. Therefore, we only need to enumerate $1 \leq a \leq b \leq 1000$ and count the occurrences of each integer $x = a^3 + b^3$. Finally, we filter out the integers that appear more than once and sort them in ascending order to obtain all good integers. We preprocess all good integers and store them in an array $\textit{GOOD}$. For each query, we use binary search to find the index $idx$ of the first integer in $\textit{GOOD}$ that is greater than $n$, then return the first $idx$ integers in $\textit{GOOD}$. The time complexity is $O(m^2 + k \log k)$, where $m = 1000$ is the enumeration range and $k$ is the number of good integers. The space complexity is $O(k)$. Java C++ Python Go TypeScript class Solution { private static final int LIMIT = (int) 1e9; private static final List&amp;lt;Integer&amp;gt; GOOD = new ArrayList&amp;lt;&amp;gt;(); static { Map&amp;lt;Integer, Integer&amp;gt; cnt = new HashMap&amp;lt;&amp;gt;(); int[] cubes = new int[1001]; for (int i = 0; i &amp;lt;= 1000; i++) { cubes[i] = i * i * i; } for (int a = 1; a &amp;lt;= 1000; a++) { for (int b = a; b &amp;lt;= 1000; b++) { int x = cubes[a] + cubes[b]; if (x &amp;gt; LIMIT) { break; } cnt.merge(x, 1, Integer::sum); } } for (Map.Entry&amp;lt;Integer, Integer&amp;gt; e : cnt.entrySet()) { if (e.getValue() &amp;gt; 1) { GOOD.add(e.getKey()); } } Collections.sort(GOOD); } public List&amp;lt;Integer&amp;gt; findGoodIntegers(int n) { int idx = Collections.binarySearch(GOOD, n + 1); if (idx &amp;lt; 0) { idx = -idx - 1; } return GOOD.subList(0, idx); } } vector&amp;lt;int&amp;gt; GOOD; auto init = [] { const int LIMIT = 1e9; unordered_map&amp;lt;int, int&amp;gt; cnt; vector&amp;lt;int&amp;gt; cubes(1001); for (int i = 0; i &amp;lt;= 1000; ++i) { cubes[i] = i * i * i; } for (int a = 1; a &amp;lt;= 1000; ++a) { for (int b = a; b &amp;lt;= 1000; ++b) { int x = cubes[a] + cubes[b]; if...
        </description>
        <pubDate>Mon, 18 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-18-3890-Integers-With-Multiple-Sum-of-Two-Cubes/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-18-3890-Integers-With-Multiple-Sum-of-Two-Cubes/</guid>
      </item>
    
      <item>
        <title>3889 - Mirror Frequency Distance</title>
        <description>
          
          Welcome to Subscribe On Youtube 3889. Mirror Frequency Distance Description You are given a string s consisting of lowercase English letters and digits. For each character, its mirror character is defined by reversing the order of its character set: For letters, the mirror of a character is the letter at the same position from the end of the alphabet. For example, the mirror of &amp;#39;a&amp;#39; is &amp;#39;z&amp;#39;, and the mirror of &amp;#39;b&amp;#39; is &amp;#39;y&amp;#39;, and so on. For digits, the mirror of a character is the digit at the same position from the end of the range &amp;#39;0&amp;#39; to &amp;#39;9&amp;#39;. For example, the mirror of &amp;#39;0&amp;#39; is &amp;#39;9&amp;#39;, and the mirror of &amp;#39;1&amp;#39; is &amp;#39;8&amp;#39;, and so on. For each unique character c in the string: Let m be its mirror character. Let freq(x) denote the number of times character x appears in the string. Compute the absolute difference between their frequencies, defined as: \|freq(c) - freq(m)\| The mirror pairs (c, m) and (m, c) are the same and must be counted only once. Return an integer denoting the total sum of these values over all such distinct mirror pairs. &amp;nbsp; Example 1: Input: s = &amp;quot;ab1z9&amp;quot; Output: 3 Explanation: For every mirror pair: c m freq(c) freq(m) \|freq(c) - freq(m)\| a z 1 1 0 b y 1 0 1 1 8 1 0 1 9 0 1 0 1 Thus, the answer is 0 + 1 + 1 + 1 = 3. Example 2: Input: s = &amp;quot;4m7n&amp;quot; Output: 2 Explanation: c m freq(c) freq(m) \|freq(c) - freq(m)\| 4 5 1 0 1 m n 1 1 0 7 2 1 0 1 Thus, the answer is 1 + 0 + 1 = 2.​​​​​​​ Example 3: Input: s = &amp;quot;byby&amp;quot; Output: 0 Explanation: c m freq(c) freq(m) \|freq(c) - freq(m)\| b y 2 2 0 Thus, the answer is 0. &amp;nbsp; Constraints: 1 &amp;lt;= s.length &amp;lt;= 5 * 105 s consists only of lowercase English letters and digits. Solutions Solution 1: Hash Table We first use a hash table $\textit{freq}$ to count the frequency of each character in string $s$. Then, we iterate over each key-value pair $(c, v)$ in $\textit{freq}$, where $c$ is the character and $v$ is the number of times character $c$ appears in string $s$. For each character $c$, we compute its mirror character $m$ and calculate $|freq(c) - freq(m)|$. To avoid counting mirror pairs twice, we use a hash set $\textit{vis}$ to track already-visited characters. Finally, we return the sum of absolute differences over all distinct mirror pairs. The time complexity is $O(n)$, where $n$ is the length of string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the set of distinct characters in string $s$. Java C++ Python Go TypeScript class Solution { public int mirrorFrequency(String s) { Map&amp;lt;Character, Integer&amp;gt; freq = new HashMap&amp;lt;&amp;gt;(); for (char c : s.toCharArray()) { freq.merge(c, 1, Integer::sum); } int ans = 0; Set&amp;lt;Character&amp;gt; vis = new HashSet&amp;lt;&amp;gt;(); for (Map.Entry&amp;lt;Character, Integer&amp;gt; entry : freq.entrySet()) { char...
        </description>
        <pubDate>Sun, 17 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-17-3889-Mirror-Frequency-Distance/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-17-3889-Mirror-Frequency-Distance/</guid>
      </item>
    
      <item>
        <title>3888 - Minimum Operations to Make All Grid Elements Equal</title>
        <description>
          
          Welcome to Subscribe On Youtube 3888. Minimum Operations to Make All Grid Elements Equal 🔒 Description You are given a 2D integer array grid of size m &amp;times; n, and an integer k. In one operation, you can: Select any k x k submatrix of grid, and Increment all elements inside that submatrix by 1. Return the minimum number of operations required to make all elements in the grid equal. If it is not possible, return -1. A submatrix (x1, y1, x2, y2) is a matrix that forms by choosing all cells matrix[x][y] where x1 &amp;lt;= x &amp;lt;= x2 and y1 &amp;lt;= y &amp;lt;= y2. &amp;nbsp; Example 1: Input: grid = [[3,3,5],[3,3,5]], k = 2 Output: 2 Explanation: Choose the left 2 x 2 submatrix (covering the first two columns) and apply the operation twice. After 1 operation: [[4, 4, 5], [4, 4, 5]] After 2 operations: [[5, 5, 5], [5, 5, 5]] All elements become equal to 5. Thus, the minimum number of operations is 2. Example 2: Input: grid = [[1,2],[2,3]], k = 1 Output: 4 Explanation: Since k = 1, each operation increments a single cell grid[i][j] by 1. To make all elements equal, the final value must be 3. Increase grid[0][0] = 1 to 3, requiring 2 operations. Increase grid[0][1] = 2 to 3, requiring 1 operation. Increase grid[1][0] = 2 to 3, requiring 1 operation. Thus, the minimum number of operations is 2 + 1 + 1 + 0 = 4. &amp;nbsp; Constraints: 1 &amp;lt;= m == grid.length &amp;lt;= 1000 1 &amp;lt;= n == grid[i].length &amp;lt;= 1000 -105 &amp;lt;= grid[i][j] &amp;lt;= 105 1 &amp;lt;= k &amp;lt;= min(m, n) Solutions Solution 1: 2D Difference Array + Greedy Since the operation can only increase the value of elements, all elements in the final grid must be equal to some target value $T$, and $T \ge \max(\textit{grid})$. Start traversing the grid from the top-left corner $(0, 0)$. For any position $(i, j)$, if its current value is less than $T$, since subsequent operations (with a more rightward or downward position as the top-left corner) cannot cover $(i, j)$, it is necessary to perform $T - \text{current_val}$ operations at the current position, each using $(i, j)$ as the top-left corner of a $k \times k$ increment operation. If each operation traverses the $k \times k$ region, the complexity will reach $O(m \cdot n \cdot k^2)$. We can use a 2D difference array $\textit{diff}$ to record the operations. By maintaining the 2D prefix sum of $\textit{diff}$ in real time, we can obtain the cumulative increment at the current position in $O(1)$ time, and update the future impact of a $k \times k$ region in $O(1)$ time. In most cases, $T = \max(\textit{grid})$ is sufficient. However, in some cases where $k \times k$ regions overlap, a smaller $T$ may cause the middle positions to be passively increased beyond $T$. According to mathematical consistency, if both $T = \max(\textit{grid})$ and $T = \max(\textit{grid}) + 1$ are not feasible, then it is impossible...
        </description>
        <pubDate>Sat, 16 May 2026 00:00:00 -0700</pubDate>
        <link>https://leetcode.ca/2026-05-16-3888-Minimum-Operations-to-Make-All-Grid-Elements-Equal/</link>
        <guid isPermaLink="true">https://leetcode.ca/2026-05-16-3888-Minimum-Operations-to-Make-All-Grid-Elements-Equal/</guid>
      </item>
    
  </channel>
</rss>
