Leetcode Question of Today
69 subscribers
467 links
Send Question of Today from Leetcode everyday at 0:00 (UTC)
Download Telegram
2025-08-09
231. Power of Two

Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy

Problem:
Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2^x.

Example 1:

Input: n = 1
Output: true
Explanation: 2^0 = 1


Example 2:

Input: n = 16
Output: true
Explanation: 2^4 = 16


Example 3:

Input: n = 3
Output: false


Constraints:

-2^31 <= n <= 2^31 - 1

Follow up: Could you solve it without loops/recursion?
2025-08-10
869. Reordered Power of 2

Topic: Hash Table, Math, Sorting, Counting, Enumeration
Difficulty: Medium

Problem:
You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return true if and only if we can do this so that the resulting number is a power of two.

Example 1:

Input: n = 1
Output: true


Example 2:

Input: n = 10
Output: false


Constraints:

1 <= n <= 10^9
2025-08-11
2438. Range Product Queries of Powers

Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium

Problem:
Given a positive integer n, there exists a 0-indexed array called powers, composed of the minimum number of powers of 2 that sum to n. The array is sorted in non-decreasing order, and there is only one way to form the array.

You are also given a 0-indexed 2D integer array queries, where queries[i] = [left_i, right_i]. Each queries[i] represents a query where you have to find the product of all powers[j] with left_i <= j <= right_i.

Return an array answers, equal in length to queries, where answers[i] is the answer to the i^th query. Since the answer to the i^th query may be too large, each answers[i] should be returned modulo 10^9 + 7.

Example 1:

Input: n = 15, queries = [[0,1],[2,2],[0,3]]
Output: [2,4,64]
Explanation:
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
Answer to 2nd query: powers[2] = 4.
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
Each answer modulo 10^9 + 7 yields the same answer, so [2,4,64] is returned.


Example 2:

Input: n = 2, queries = [[0,0]]
Output: [2]
Explanation:
For n = 2, powers = [2].
The answer to the only query is powers[0] = 2. The answer modulo 10^9 + 7 is the same, so [2] is returned.


Constraints:

1 <= n <= 10^9
1 <= queries.length <= 10^5
0 <= start_i <= end_i < powers.length
2025-08-12
2787. Ways to Express an Integer as Sum of Powers

Topic: Dynamic Programming
Difficulty: Medium

Problem:
Given two positive integers n and x.

Return the number of ways n can be expressed as the sum of the x^th power of unique positive integers, in other words, the number of sets of unique integers [n_1, n_2, ..., n_k] where n = n_1^x + n_2^x + ... + n_k^x.

Since the result can be very large, return it modulo 10^9 + 7.

For example, if n = 160 and x = 3, one way to express n is n = 2^3 + 3^3 + 5^3.

Example 1:

Input: n = 10, x = 2
Output: 1
Explanation: We can express n as the following: n = 3^2 + 1^2 = 10.
It can be shown that it is the only way to express 10 as the sum of the 2^nd power of unique integers.


Example 2:

Input: n = 4, x = 1
Output: 2
Explanation: We can express n in the following ways:
- n = 4^1 = 4.
- n = 3^1 + 1^1 = 4.


Constraints:

1 <= n <= 300
1 <= x <= 5
2025-08-13
326. Power of Three

Topic: Math, Recursion
Difficulty: Easy

Problem:
Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3^x.

Example 1:

Input: n = 27
Output: true
Explanation: 27 = 3^3


Example 2:

Input: n = 0
Output: false
Explanation: There is no x where 3^x = 0.


Example 3:

Input: n = -1
Output: false
Explanation: There is no x where 3^x = (-1).


Constraints:

-2^31 <= n <= 2^31 - 1

Follow up: Could you solve it without loops/recursion?
2025-08-14
2264. Largest 3-Same-Digit Number in String

Topic: String
Difficulty: Easy

Problem:
You are given a string num representing a large integer. An integer is good if it meets the following conditions:

• It is a substring of num with length 3.
• It consists of only one unique digit.

Return the maximum good integer as a string or an empty string "" if no such integer exists.

Note:

• A substring is a contiguous sequence of characters within a string.
• There may be leading zeroes in num or a good integer.

Example 1:

Input: num = "6777133339"
Output: "777"
Explanation: There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".


Example 2:

Input: num = "2300019"
Output: "000"
Explanation: "000" is the only good integer.


Example 3:

Input: num = "42352338"
Output: ""
Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.


Constraints:

3 <= num.length <= 1000
num only consists of digits.
2025-08-15
342. Power of Four

Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy

Problem:
Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4^x.

Example 1:

Input: n = 16
Output: true


Example 2:

Input: n = 5
Output: false


Example 3:

Input: n = 1
Output: true


Constraints:

-2^31 <= n <= 2^31 - 1

Follow up: Could you solve it without loops/recursion?
2025-08-16
1323. Maximum 69 Number

Topic: Math, Greedy
Difficulty: Easy

Problem:
You are given a positive integer num consisting only of digits 6 and 9.

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

Example 1:

Input: num = 9669
Output: 9969
Explanation:
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Changing the fourth digit results in 9666.
The maximum number is 9969.


Example 2:

Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.


Example 3:

Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.


Constraints:

1 <= num <= 10^4
num consists of only 6 and 9 digits.
2025-08-17
837. New 21 Game

Topic: Math, Dynamic Programming, Sliding Window, Probability and Statistics
Difficulty: Medium

Problem:
Alice plays the following game, loosely based on the card game "21".

Alice starts with 0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where maxPts is an integer. Each draw is independent and the outcomes have equal probabilities.

Alice stops drawing numbers when she gets k or more points.

Return the probability that Alice has n or fewer points.

Answers within 10^-5 of the actual answer are considered accepted.

Example 1:

Input: n = 10, k = 1, maxPts = 10
Output: 1.00000
Explanation: Alice gets a single card, then stops.


Example 2:

Input: n = 6, k = 1, maxPts = 10
Output: 0.60000
Explanation: Alice gets a single card, then stops.
In 6 out of 10 possibilities, she is at or below 6 points.


Example 3:

Input: n = 21, k = 17, maxPts = 10
Output: 0.73278


Constraints:

0 <= k <= n <= 10^4
1 <= maxPts <= 10^4
2025-08-18
679. 24 Game

Topic: Array, Math, Backtracking
Difficulty: Hard

Problem:
You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24.

You are restricted with the following rules:

• The division operator '/' represents real division, not integer division.
• For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
• Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
• For example, if cards = [1, 1, 1, 1], the expression "-1 - 1 - 1 - 1" is not allowed.
• You cannot concatenate numbers together
• For example, if cards = [1, 2, 1, 2], the expression "12 + 12" is not valid.

Return true if you can get such expression that evaluates to 24, and false otherwise.

Example 1:

Input: cards = [4,1,8,7]
Output: true
Explanation: (8-4) * (7-1) = 24


Example 2:

Input: cards = [1,2,1,2]
Output: false


Constraints:

cards.length == 4
1 <= cards[i] <= 9
2025-08-19
2348. Number of Zero-Filled Subarrays

Topic: Array, Math
Difficulty: Medium

Problem:
Given an integer array nums, return the number of subarrays filled with 0.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,3,0,0,2,0,0,4]
Output: 6
Explanation:
There are 4 occurrences of [0] as a subarray.
There are 2 occurrences of [0,0] as a subarray.
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.


Example 2:

Input: nums = [0,0,0,2,0,0]
Output: 9
Explanation:
There are 5 occurrences of [0] as a subarray.
There are 3 occurrences of [0,0] as a subarray.
There is 1 occurrence of [0,0,0] as a subarray.
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.


Example 3:

Input: nums = [2,10,2019]
Output: 0
Explanation: There is no subarray filled with 0. Therefore, we return 0.


Constraints:

1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
2025-08-20
1277. Count Square Submatrices with All Ones

Topic: Array, Dynamic Programming, Matrix
Difficulty: Medium

Problem:
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.

Example 1:

Input: matrix =
[
  [0,1,1,1],
  [1,1,1,1],
  [0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.


Example 2:

Input: matrix = 
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.


Constraints:

1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1
2025-08-21
1504. Count Submatrices With All Ones

Topic: Array, Dynamic Programming, Stack, Matrix, Monotonic Stack
Difficulty: Medium

Problem:
Given an m x n binary matrix mat, return the number of submatrices that have all ones.

Example 1:

Image: https://assets.leetcode.com/uploads/2021/10/27/ones1-grid.jpg

Input: mat = [[1,0,1],[1,1,0],[1,1,0]]
Output: 13
Explanation:
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.


Example 2:

Image: https://assets.leetcode.com/uploads/2021/10/27/ones2-grid.jpg

Input: mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
Output: 24
Explanation:
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3.
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.


Constraints:

1 <= m, n <= 150
mat[i][j] is either 0 or 1.
2025-08-22
3195. Find the Minimum Area to Cover All Ones I

Topic: Array, Matrix
Difficulty: Medium

Problem:
You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1's in grid lie inside this rectangle.

Return the minimum possible area of the rectangle.

Example 1:

Input: grid = [0,1,0,1,0,1]

Output: 6

Explanation:

Image: https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png

The smallest rectangle has a height of 2 and a width of 3, so it has an area of 2 * 3 = 6.

Example 2:

Input: grid = [1,0,0,0]

Output: 1

Explanation:

Image: https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png

The smallest rectangle has both height and width 1, so its area is 1 * 1 = 1.

Constraints:

1 <= grid.length, grid[i].length <= 1000
grid[i][j] is either 0 or 1.
• The input is generated such that there is at least one 1 in grid.
2025-08-23
3197. Find the Minimum Area to Cover All Ones II

Topic: Array, Matrix, Enumeration
Difficulty: Hard

Problem:
You are given a 2D binary array grid. You need to find 3 non-overlapping rectangles having non-zero areas with horizontal and vertical sides such that all the 1's in grid lie inside these rectangles.

Return the minimum possible sum of the area of these rectangles.

Note that the rectangles are allowed to touch.

Example 1:

Input: grid = [1,0,1,1,1,1]

Output: 5

Explanation:

Image: https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png

• The 1's at (0, 0) and (1, 0) are covered by a rectangle of area 2.
• The 1's at (0, 2) and (1, 2) are covered by a rectangle of area 2.
• The 1 at (1, 1) is covered by a rectangle of area 1.

Example 2:

Input: grid = [1,0,1,0,0,1,0,1]

Output: 5

Explanation:

Image: https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png

• The 1's at (0, 0) and (0, 2) are covered by a rectangle of area 3.
• The 1 at (1, 1) is covered by a rectangle of area 1.
• The 1 at (1, 3) is covered by a rectangle of area 1.

Constraints:

1 <= grid.length, grid[i].length <= 30
grid[i][j] is either 0 or 1.
• The input is generated such that there are at least three 1's in grid.
2025-08-24
1493. Longest Subarray of 1's After Deleting One Element

Topic: Array, Dynamic Programming, Sliding Window
Difficulty: Medium

Problem:
Given a binary array nums, you should delete one element from it.

Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.


Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].


Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.


Constraints:

1 <= nums.length <= 10^5
nums[i] is either 0 or 1.
2025-08-25
498. Diagonal Traverse

Topic: Array, Matrix, Simulation
Difficulty: Medium

Problem:
Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

Example 1:

Image: https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg

Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,4,7,5,3,6,8,9]


Example 2:

Input: mat = [[1,2],[3,4]]
Output: [1,2,3,4]


Constraints:

m == mat.length
n == mat[i].length
1 <= m, n <= 10^4
1 <= m * n <= 10^4
-10^5 <= mat[i][j] <= 10^5
2025-08-26
3000. Maximum Area of Longest Diagonal Rectangle

Topic: Array
Difficulty: Easy

Problem:
You are given a 2D 0-indexed integer array dimensions.

For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.

Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.

Example 1:

Input: dimensions = [[9,3],[8,6]]
Output: 48
Explanation:
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.


Example 2:

Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.


Constraints:

1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
2025-08-27
3459. Length of Longest V-Shaped Diagonal Segment

Topic: Array, Dynamic Programming, Memoization, Matrix
Difficulty: Hard

Problem:
You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2.

A V-shaped diagonal segment is defined as:

• The segment starts with 1.
• The subsequent elements follow this infinite sequence: 2, 0, 2, 0, ....
• The segment:
• Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).
• Continues the sequence in the same diagonal direction.
• Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.

Image: https://assets.leetcode.com/uploads/2025/01/11/length_of_longest3.jpg

Return the length of the longest V-shaped diagonal segment. If no valid segment exists, return 0.

Example 1:

Input: grid = [2,2,1,2,2,2,0,2,2,0,2,0,1,1,0,1,0,2,2,2,2,0,0,2,2]

Output: 5

Explanation:

Image: https://assets.leetcode.com/uploads/2024/12/09/matrix_1-2.jpg

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) → (1,3) → (2,4), takes a 90-degree clockwise turn at (2,4), and continues as (3,3) → (4,2).

Example 2:

Input: grid = [2,2,2,2,2,2,0,2,2,0,2,0,1,1,0,1,0,2,2,2,2,0,0,2,2]

Output: 4

Explanation:

Image: https://assets.leetcode.com/uploads/2024/12/09/matrix_2.jpg

The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) → (3,2), takes a 90-degree clockwise turn at (3,2), and continues as (2,1) → (1,0).

Example 3:

Input: grid = [1,2,2,2,2,2,2,2,2,0,2,0,0,0,0,0,0,2,2,2,2,0,0,2,0]

Output: 5

Explanation:

Image: https://assets.leetcode.com/uploads/2024/12/09/matrix_3.jpg

The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) → (1,1) → (2,2) → (3,3) → (4,4).

Example 4:

Input: grid = [1]

Output: 1

Explanation:

The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0).

Constraints:

n == grid.length
m == grid[i].length
1 <= n, m <= 500
grid[i][j] is either 0, 1 or 2.
2025-08-28
3446. Sort Matrix by Diagonals

Topic: Array, Sorting, Matrix
Difficulty: Medium

Problem:
You are given an n x n square matrix of integers grid. Return the matrix such that:

• The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
• The diagonals in the top-right triangle are sorted in non-decreasing order.

Example 1:

Input: grid = [1,7,3,9,8,2,4,5,6]

Output: [8,2,3,9,6,7,4,5,1]

Explanation:

Image: https://assets.leetcode.com/uploads/2024/12/29/4052example1drawio.png

The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:

[1, 8, 6] becomes [8, 6, 1].
[9, 5] and [4] remain unchanged.

The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:

[7, 2] becomes [2, 7].
[3] remains unchanged.

Example 2:

Input: grid = [0,1,1,2]

Output: [2,1,1,0]

Explanation:

Image: https://assets.leetcode.com/uploads/2024/12/29/4052example2adrawio.png

The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.

Example 3:

Input: grid = [1]

Output: [1]

Explanation:

Diagonals with exactly one element are already in order, so no changes are needed.

Constraints:

grid.length == grid[i].length == n
1 <= n <= 10
-10^5 <= grid[i][j] <= 10^5