[LeetCode] Minimum Energy to Maintain Brightness

3951. Minimum Energy to Maintain Brightness

You are given an integer n, representing n light bulbs arranged in a line and indexed from 0 to n - 1.

You are also given an integer brightness and a 2D integer array intervals, where intervals[i] = [start_i, end_i] represents an inclusive time interval during which the lighting requirement must be satisfied.

At each time unit, every bulb can independently be either on or off. A bulb that is on illuminates its own position and its adjacent positions, if they exist.

The total illumination at a time unit is the number of illuminated positions. Each position is counted at most once.

For every integer time unit covered by at least one interval in intervals, the total illumination must be at least brightness. At time units not covered by any interval, all bulbs may remain off. Each bulb that is on consumes 1 unit of energy for that time unit.

Return an integer denoting the minimum total energy required.

Read more
[LeetCode] Valid Binary Strings With Cost Limit

3955. Valid Binary Strings With Cost Limit

You are given two integers n and k.

The cost of a binary string s is defined as the sum of all indices i (0-based) such that s[i] == '1'.

A binary string is considered valid if:

  • It does not contain two consecutive '1' characters.
  • Its cost is less than or equal to k.

Return a list of all valid binary strings of length n in any order.

Read more
[LeetCode] Count Good Integers in a Range

3966. Count Good Integers in a Range

You are given three integers l, r and k.

A number is considered good if the absolute difference between every pair of adjacent digits is at most k.

Return the number of good integers in the range [l, r] (inclusive).

The absolute difference between values x and y is defined as abs(x - y).

Read more
[LeetCode] Finish Time of Tasks I

3965. Finish Time of Tasks I

You are given an integer n representing the number of tasks in a project, numbered from 0 to n - 1. These tasks are connected as a tree rooted at task 0. This is represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that task u_i is the parent of task v_i.

You are also given an array baseTime of length n, where baseTime[i] represents the time to complete task i.

The finish time of each task is calculated as follows:

  • Leaf task: The finish time is baseTime[i].
  • Non-leaf task:
    • Let earliest be the minimum finish time among its children, and latest be the maximum finish time among its children.
    • Let ownDuration be (latest - earliest) + baseTime[i].
    • The finish time of task i is latest + ownDuration.

Return the finish time of the root task 0.

Read more
[LeetCode] Check Good Integer

3959. Check Good Integer

You are given a positive integer n.

Let digitSum be the sum of the digits of n, and let squareSum be the sum of the squares of the digits of n.

An integer is called good if squareSum - digitSum >= 50.

Return true if n is good. Otherwise, return false.

Read more
[LeetCode] Frequency Balance Subarray

3960. Frequency Balance Subarray

You are given an integer array ​​​​​​​nums.

Define a frequency balance subarray as follows:

  • If the subarray contains only one distinct value, it is frequency balanced.
  • Otherwise, there must exist a positive integer f such that every distinct value in the subarray occurs either f or 2 * f times, and both frequencies occur among the distinct values.

Return an integer denoting the length of the longest frequency balance subarray.

Read more
[LeetCode] Maximum Sum of M Non-Overlapping Subarrays I

3956. Maximum Sum of M Non-Overlapping Subarrays I

You are given an integer array nums of length n, and three integers m, l, and r.

Your task is to select at least one and at most m non-overlapping subarrays from nums such that:

  • Each selected subarray has a length between [l, r] (inclusive).
  • The total sum of all selected subarrays is maximized.

Return the maximum total sum you can achieve.

Read more
[LeetCode] Maximum Sum of M Non-Overlapping Subarrays II

3957. Maximum Sum of M Non-Overlapping Subarrays II

You are given an integer array nums of length n, and three integers m, l, and r.

Your task is to select at least one and at most m non-overlapping subarrays from nums such that:

  • Each selected subarray has a length between [l, r] (inclusive).
  • The total sum of all selected subarrays is maximized.

Return the maximum total sum you can achieve.

Read more
[LeetCode] Create Grid With Exactly One Path

3963. Create Grid With Exactly One Path

You are given two integers m and n, representing the number of rows and columns of a grid.

Construct any m x n grid consisting only of the characters '.' and '#', where:

  • '.' represents a free cell.
  • '#' represents an obstacle cell.

A valid path is a sequence of free cells that:

  • Starts at the top-left cell (0, 0).
  • Ends at the bottom-right cell (m - 1, n - 1).
  • Moves only:
    • Right, from (i, j) to (i, j + 1), or
    • Down, from (i, j) to (i + 1, j).

Return any grid such that there is exactly one valid path from the top-left cell to the bottom-right cell.

Read more
[LeetCode] Maximize Sum of Device Ratings

3961. Maximize Sum of Device Ratings

You are given a 2D integer array units of size m × n where units[i][j] represents the capacity of the j^th unit in the i^th device. Each device contains exactly n units.

The rating of a device is the minimum capacity among all its units.

You may perform the following operation any number of times (including zero):

  • Choose a device i that has not been used as a source before.
  • Remove exactly one unit from device i and add it to any different device.
  • Then mark device i as used, so it cannot be chosen again as a source.

Return the maximum possible sum of the ratings of all devices after any number of such operations.

Note:

  • Devices can receive units from multiple devices, regardless of whether they have been selected.
  • The rating of an empty device is 0.
Read more