[LeetCode] Number of Elapsed Seconds Between Two Times

3986. Number of Elapsed Seconds Between Two Times

You are given two valid times startTime and endTime, each represented as a string in the format "HH:MM:SS".

Return the number of seconds that have elapsed from startTime to endTime.

Read more
[LeetCode] Minimum Total Cost to Process All Elements

3987. Minimum Total Cost to Process All Elements

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

Initially, you have k units of resources.

You must process the elements of nums from left to right. To process the i^th element, you need nums[i] resources.

If your available resources are less than nums[i], you may perform an operation that increases your available resources by k. The value of k is fixed and does not change throughout the process. The first such operation incurs a cost of 1, the second incurs a cost of 2, and so on.

After processing the i^th element, your available resources decrease by nums[i].

Return an integer denoting the minimum total cost required to process all elements. Since the answer may be very large, return it modulo 10^9 + 7.

Read more
[LeetCode] Create Grid With Exactly K Paths I

3988. Create Grid With Exactly K Paths I

You are given three integers m, n, and k.

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 are exactly k valid paths from the top-left cell to the bottom-right cell. If no such grid exists, return an empty array.

Read more
[LeetCode] Maximum Consistent Columns in a Grid

3989. Maximum Consistent Columns in a Grid

You are given a 2D integer array grid of size m x n, and an integer limit.

You may remove zero or more columns from the grid, but at least one column must remain. The relative order of the remaining columns must be preserved.

A grid is called consistent if for every row i, and for every pair of adjacent remaining columns a and b with a < b, the following holds: |grid[i][b] - grid[i][a]| <= limit.

Return the maximum number of columns that can remain such that the resulting grid is consistent.

Read more
[LeetCode] Sort Array Using Prefix Reversals

3991. Sort Array Using Prefix Reversals

You are given an integer array nums of length n, where nums is a permutation of the integers in the range [0, n - 1].

You are also given an integer array pre, where each pre[i] is a valid prefix length.

In one operation, you may choose any length x from pre and reverse the first x elements of nums.

For example, applying a prefix reversal of length 3 on [4, 1, 2, 3] results in [2, 1, 4, 3].

Return the minimum number of operations required to sort nums in ascending order. If it is impossible to sort nums, return -1.

Read more
[LeetCode] Rearrange String to Avoid Character Pair

3992. Rearrange String to Avoid Character Pair

You are given a string s and two distinct lowercase English letters x and y.

Rearrange the characters of s to construct a new string t such that:

  • t is a permutation of s.
  • Every occurrence of y appears before every occurrence of x in t.

Return any valid string t.

Read more
[LeetCode] Maximum Value of an Alternating Sequence

3993. Maximum Value of an Alternating Sequence

You are given three integers n, s, and m.

A sequence seq of integers of length n is considered valid if:

  • seq[0] = s.
  • The sequence is alternating, meaning that either:
    • seq[0] > seq[1] < seq[2] > ..., or
    • seq[0] < seq[1] > seq[2] < ....
  • For every adjacent pair, |seq[i] - seq[i - 1]| <= m.

A sequence of length 1 is considered alternating.

Return the maximum possible element that can appear in any valid sequence.

Read more
[LeetCode] Aggregate Two Time Series

4001. Aggregate Two Time Series

You are given two 2D integer arrays series1 and series2.

Each element in both series is of the form [timestamp, value], where:

  • timestamp is an integer representing the time.
  • value is an integer representing the value at that timestamp.

Each array is sorted in strictly increasing order of timestamp.

For any timestamp not present in a series, its value is taken from the next available timestamp in the same series if one exists. Otherwise, its value is considered 0.

The aggregated series is formed by summing the corresponding values from both series at every timestamp that appears in either series.

Return the aggregated series as a 2D integer array of [timestamp, summedValue] pairs, sorted in strictly increasing order of timestamp.

Read more
[LeetCode] Minimum Adjacent Swaps to Partition Array

3994. Minimum Adjacent Swaps to Partition Array

You are given an integer array nums and two integers a and b such that a < b.

An array is called good if it can be split into three contiguous parts, in this order, such that:

  • Every element in the first part is less than a.
  • Every element in the second part is in the range [a, b] inclusive.
  • Every element in the third part is greater than b.

Any of the three parts may be empty.

In one adjacent swap, you may swap two neighboring elements of nums.

Return the minimum number of adjacent swaps required to make nums good. Since the answer may be very large, return it modulo 10^9 + 7.

Read more
[LeetCode] Minimum Cost to Convert String III

3995. Minimum Cost to Convert String III

You are given two strings, source and target.

You are also given a 2D string array rules, where rules[i] = [pattern_i, replacement_i], and an integer array costs, where costs[i] is the base cost of applying rules[i]. Both arrays have the same length. Additionally, pattern_i and replacement_i have the same length.

You may apply any rule any number of times. Each rule application works as follows:

  • Choose an index l such that the range of positions from l to l + pattern_i.length - 1 exists in the current string and none of these positions has been used in a previous rule application.
  • For each index j, the character pattern_i[j] must either be equal to the current character at position l + j, or be '*'.
  • Replace the characters in this range with replacement_i. The replacement is used exactly as given and does not contain wildcards.
  • The cost of this rule application is costs[i] plus the number of '*' characters in pattern_i.
  • Once a character position has been used in a rule application, it cannot be used in any later rule application.

Since every pattern_i and replacement_i have the same length, character positions are preserved after every rule application.

Return the minimum total cost required to transform source into target. If it is impossible, return -1.

Read more