[LeetCode] Maximum Subarray Sum After at Most K Swaps

3962. Maximum Subarray Sum After at Most K Swaps

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

You are allowed to perform at most k swap operations on the array.

In one swap operation, you may choose any two indices i and j and swap nums[i] and nums[j].

Return an integer denoting the maximum possible subarray sum after performing the swaps.

Read more
[LeetCode] Minimum Lights to Illuminate a Road

3964. Minimum Lights to Illuminate a Road

You are given an integer array lights of length n, representing positions 0 through n - 1 on a road.

For each position i:

  • If lights[i] = v, where v > 0, there is a working bulb at position i that illuminates every position from max(0, i - v) to min(n - 1, i + v), inclusive.
  • If lights[i] = 0, there is no working bulb at position i.

A position is visible if it is illuminated by at least one working bulb.

You may install additional bulbs at any positions. Each additional bulb installed at position j illuminates positions from max(0, j - 1) to min(n - 1, j + 1), inclusive.

Return the minimum number of additional bulbs required to make every position on the road visible.

Read more
[LeetCode] Distinct Gate Paths to LCA

3973. Distinct Gate Paths to LCA

You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1, represented by an array parent where parent[i] is the parent of node i.

Each node i has three types of gates, given in a 2D array gates where gates[i] = [red_i, blue_i, white_i] which represents the number of red, blue, and white gates at node i.

  • Red gate: usable only with a red card.
  • Blue gate: usable only with a blue card.
  • White gate: usable with either card, but flips the card color when used.

Alice and Bob start at given nodes with either a red or blue card (1 = red, 0 = blue). They must independently move upward to their lowest common ancestor (LCA).

At each node, a person may move to their parent only if they can use at least one gate at that node with their current card. White gates may be used any number of times to flip the card color.

Movement rules (one move = from u to parent[u]):

  • Movement is only upward toward the root.
  • At node u, pick exactly one specific gate instance. Identical gates are treated as separate and counted individually.
  • If holding a red card: use a red gate to remain red, or a white gate to change to blue.
  • If holding a blue card: use a blue gate to remain blue, or a white gate to change to red.
  • If no usable gate exists at u, the sequence ends.

You are also given a 2D array queries where queries[i] = [aNode_i, aCard_i, bNode_i, bCard_i]:

  • aNode_i, aCard_i: Alice’s starting node and card.
  • bNode_i, bCard_i: Bob’s starting node and card.

For each query, count the number of distinct valid ways modulo 10^9 + 7 for both to reach their LCA.

After computing the result for all queries, return the bitwise XOR of those values.

Note:

  • Two ways are distinct if the set of gates used differs for either Alice or Bob.
  • If any person is already at the LCA, then the number of ways for them is 1.
  • The lowest common ancestor (LCA) is defined between two nodes a and b as the lowest node in a tree that has both a and b as descendants (where a node is allowed to be a descendant of itself).
Read more
[LeetCode] Finish Time of Tasks II

3967. Finish Time of Tasks II

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 an undirectedtree. This is represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates an undirected connection between task u_i and task v_i.

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

For any chosen task as the root, 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].
    • Finish time of task i is latest + ownDuration.

Choose any task as the root and compute the finish time of that root based on the rules above.

Return the minimum possible finish time among all choices of root.

Read more
[LeetCode] Maximum Manhattan Distance After All Moves

3968. Maximum Manhattan Distance After All Moves

You are given a string moves consisting of the characters 'U', 'D', 'L', 'R', and '_'.

Starting from the origin (0, 0), each character represents one move on a 2D plane:

  • 'U': Move up by 1 unit.
  • 'D': Move down by 1 unit.
  • 'L': Move left by 1 unit.
  • 'R': Move right by 1 unit.
  • '_': Can be independently replaced with any one of 'U', 'D', 'L', or 'R'.

Return the maximum Manhattan distance from the origin that can be achieved after all moves have been performed.

Read more
[LeetCode] Maximum Total Value

3971. Maximum Total Value

You are given two integer arrays value and decay, and an integer m.

  • value[i] represents the initial value at index i.
  • decay[i] represents how much the value decreases after each selection of index i.

You may select any index multiple times. The total number of selections across all indices must not exceed m.

If you select index i for the t^th time, where t is 1-indexed, the value gained is value[i] - decay[i] * (t - 1).

Return the maximum total value you can obtain. Since the answer may be large, return it modulo 10^9 + 7.

Read more
[LeetCode] Shortest Path With At Most K Consecutive Identical Characters

3970. Shortest Path With At Most K Consecutive Identical Characters

You are given an integer n representing the number of nodes in a directed weighted graph, numbered from 0 to n - 1. This is represented by a 2D integer array edges, where edges[i] = [u_i, v_i, w_i] represents a directed edge from node u_i to node v_i with weight w_i.

You are also given a string labels of length n, where labels[i] is the character assigned to node i, and an integer k.

Return the minimum total edge weight of a path from node 0 to node n - 1 such that the concatenation of the labels of the nodes along the path contains at most k consecutive identical characters. If no valid path exists, return -1.

Read more
[LeetCode] Valid Subarrays With Matching Sum Digits I

3969. Valid Subarrays With Matching Sum Digits I

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

A subarray nums[l..r] is considered valid if the sum of its elements satisfies both of the following conditions:

  • The first digit of the sum is equal to x.
  • The last digit of the sum is equal to x.

Return the number of valid subarrays.

Read more
[LeetCode] Maximum Total Sum of K Selected Elements

3974. Maximum Total Sum of K Selected Elements

You are given an integer array nums and two integers k and mul.

Select exactly k elements from nums. Process these elements one by one in any order you choose.

For each selected element, independently choose one of the following:

  • Add the element’s value to the total sum, or
  • Multiply the element by the current value of mul and add the result to the total sum.

After processing each selected element, mul decreases by 1, regardless of which option was chosen. The current value of mul may become 0 or negative.

Return an integer denoting the maximum possible total sum.

Read more
[LeetCode] Filter Occupied Intervals

3975. Filter Occupied Intervals

You are given a 2D integer array occupiedIntervals, where occupiedIntervals[i] = [start_i, end_i] represents a time interval during which you are occupied. Each interval starts at start_i and ends at end_i, inclusive. These intervals may overlap.

You are also given two integers freeStart and freeEnd, which define a free time interval from freeStart to freeEnd, inclusive.

Your task is to merge all occupied intervals that overlap or touch, then remove all integer points in the free interval from the merged occupied intervals.

Two intervals touch if the second interval starts immediately after the first one ends. For example, [1, 1] and [2, 2] touch and should be merged into [1, 2].

Return the remaining occupied intervals in sorted order. The returned intervals must be non-overlapping and must contain the minimum number of intervals possible. If there are no remaining occupied points, return an empty list.

Read more