[LeetCode] Minimum Swaps to Avoid Forbidden Values

3785. Minimum Swaps to Avoid Forbidden Values

You are given two integer arrays, nums and forbidden, each of length n.

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

  • Choose two distinct indices i and j, and swap nums[i] with nums[j].

Return the minimum number of swaps required such that, for every index i, the value of nums[i] is not equal to forbidden[i]. If no amount of swaps can ensure that every index avoids its forbidden value, return -1.

Read more
[LeetCode] Total Sum of Interaction Cost in Tree Groups

3786. Total Sum of Interaction Cost in Tree Groups

You are given an integer n and an undirected tree with n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of length n - 1, where edges[i] = [u_i, v_i] indicates an undirected edge between nodes u_i and v_i.

You are also given an integer array group of length n, where group[i] denotes the group label assigned to node i.

  • Two nodes u and v are considered part of the same group if group[u] == group[v].
  • The interaction cost between u and v is defined as the number of edges on the unique path connecting them in the tree.

Return an integer denoting the sum of interaction costs over all unordered pairs (u, v) with u != v such that group[u] == group[v].

Read more
[LeetCode] Count Routes to Climb a Rectangular Grid

3797. Count Routes to Climb a Rectangular Grid

You are given a string array grid of size n, where each string grid[i] has length m. The character grid[i][j] is one of the following symbols:

  • '.': The cell is available.
  • '#': The cell is blocked.

You want to count the number of different routes to climb grid. Each route must start from any cell in the bottom row (row n - 1) and end in the top row (row 0).

However, there are some constraints on the route.

  • You can only move from one available cell to another available cell.
  • The Euclidean distance of each move is at most d, where d is an integer parameter given to you. The Euclidean distance between two cells (r1, c1), (r2, c2) is sqrt((r1 - r2)^2 + (c1 - c2)^2).
  • Each move either stays on the same row or moves to the row directly above (from row r to r - 1).
  • You cannot stay on the same row for two consecutive turns. If you stay on the same row in a move (and this move is not the last move), your next move must go to the row above.

Return an integer denoting the number of such routes. Since the answer may be very large, return it modulo 10^9 + 7.

Read more
[LeetCode] Find Maximum Value in a Constrained Sequence

3796. Find Maximum Value in a Constrained Sequence

You are given an integer n, a 2D integer array restrictions, and an integer array diff of length n - 1. Your task is to construct a sequence of length n, denoted by a[0], a[1], ..., a[n - 1], such that it satisfies the following conditions:

  • a[0] is 0.
  • All elements in the sequence are non-negative.
  • For every index i (0 <= i <= n - 2), abs(a[i] - a[i + 1]) <= diff[i].
  • For each restrictions[i] = [idx, maxVal], the value at position idx in the sequence must not exceed maxVal (i.e., a[idx] <= maxVal).

Your goal is to construct a valid sequence that maximizes the largest value within the sequence while satisfying all the above conditions.

Return an integer denoting the largest value present in such an optimal sequence.

Read more
[LeetCode] Largest Even Number

3798. Largest Even Number

You are given a string s consisting only of the characters '1' and '2'.

You may delete any number of characters from s without changing the order of the remaining characters.

Return the largest possible resultant string that represents an even integer. If there is no such string, return the empty string "".

Read more
[LeetCode] Minimum Subarray Length With Distinct Sum At Least K

3795. Minimum Subarray Length With Distinct Sum At Least K

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

Return the minimum length of a subarray whose sum of the distinct values present in that subarray (each value counted once) is at least k. If no such subarray exists, return -1.

Read more
[LeetCode] Minimum Cost to Acquire Required Items

3789. Minimum Cost to Acquire Required Items

You are given five integers cost1, cost2, costBoth, need1, and need2.

There are three types of items available:

  • An item of type 1 costs cost1 and contributes 1 unit to the type 1 requirement only.
  • An item of type 2 costs cost2 and contributes 1 unit to the type 2 requirement only.
  • An item of type 3 costs costBoth and contributes 1 unit to both type 1 and type 2 requirements.

You must collect enough items so that the total contribution toward type 1 is at least need1 and the total contribution toward type 2 is at least need2.

Return an integer representing the minimum possible total cost to achieve these requirements.

Read more
[LeetCode] Number of Balanced Integers in a Range

3791. Number of Balanced Integers in a Range

You are given two integers low and high.

An integer is called balanced if it satisfies both of the following conditions:

  • It contains at least two digits.
  • The sum of digits at even positions is equal to the sum of digits at odd positions (the leftmost digit has position 1).

Return an integer representing the number of balanced integers in the range [low, high] (both inclusive).

Read more
[LeetCode] Reverse String Prefix

3794. Reverse String Prefix

You are given a string s and an integer k.

Reverse the first k characters of s and return the resulting string.

Read more
[LeetCode] Smallest All-Ones Multiple

3790. Smallest All-Ones Multiple

You are given a positive integer k.

Find the smallest integer n divisible by k that consists of only the digit 1 in its decimal representation (e.g., 1, 11, 111, …).

Return an integer denoting the number of digits in the decimal representation of n. If no such n exists, return -1.

Read more