[LeetCode] Minimum Cost to Partition a Binary String

3864. Minimum Cost to Partition a Binary String

You are given a binary string s and two integers encCost and flatCost.

For each index i, s[i] = '1' indicates that the i^th element is sensitive, and s[i] = '0' indicates that it is not.

The string must be partitioned into segments. Initially, the entire string forms a single segment.

For a segment of length L containing X sensitive elements:

  • If X = 0, the cost is flatCost.
  • If X > 0, the cost is L * X * encCost.

If a segment has even length, you may split it into two contiguous segments of equal length and the cost of this split is the sum of costs of the resulting segments.

Return an integer denoting the minimum possible total cost over all valid partitions.

Read more
[LeetCode] Sum of GCD of Formed Pairs

3867. Sum of GCD of Formed Pairs

You are given an integer array nums of length n.

Construct an array prefixGcd where for each index i:

  • Let mx_i = max(nums[0], nums[1], ..., nums[i]).
  • prefixGcd[i] = gcd(nums[i], mx_i).

After constructing prefixGcd:

  • Sort prefixGcd in non-decreasing order.
  • Form pairs by taking the smallest unpaired element and the largest unpaired element.
  • Repeat this process until no more pairs can be formed.
  • For each formed pair, compute the gcd of the two elements.
  • If n is odd, the middle element in the prefixGcd array remains unpaired and should be ignored.

Return an integer denoting the sum of the GCD values of all formed pairs.

gcd(a, b)

greatest common divisor

a

b

Read more
[LeetCode] Merge Close Characters

3853. Merge Close Characters

You are given a string s consisting of lowercase English letters and an integer k.

Two equal characters in the current string s are considered close if the distance between their indices is at most k.

When two characters are close, the right one merges into the left. Merges happen one at a time, and after each merge, the string updates until no more merges are possible.

Return the resulting string after performing all possible merges.

Note: If multiple merges are possible, always merge the pair with the smallest left index. If multiple pairs share the smallest left index, choose the pair with the smallest right index.

Read more
[LeetCode] Minimum Operations to Make Array Parity Alternating

3854. Minimum Operations to Make Array Parity Alternating

You are given an integer array nums.

An array is called parity alternating if for every index i where 0 <= i < n - 1, nums[i] and nums[i + 1] have different parity (one is even and the other is odd).

In one operation, you may choose any index i and either increase nums[i] by 1 or decrease nums[i] by 1.

Return an integer array answer of length 2 where:

  • answer[0] is the minimum number of operations required to make the array parity alternating.
  • answer[1] is the minimum possible value of max(nums) - min(nums) taken over all arrays that are parity alternating and can be obtained by performing exactly answer[0] operations.

An array of length 1 is considered parity alternating.

Read more
[LeetCode] Sum of K-Digit Numbers in a Range

3855. Sum of K-Digit Numbers in a Range

You are given three integers l, r, and k.

Consider all possible integers consisting of exactly k digits, where each digit is chosen independently from the integer range [l, r] (inclusive). If 0 is included in the range, leading zeros are allowed.

Return an integer representing the sum of all such numbers.​​​​​​​ Since the answer may be very large, return it modulo 10^9 + 7.

Read more
[LeetCode] Trim Trailing Vowels

3856. Trim Trailing Vowels

You are given a string s that consists of lowercase English letters.

Return the string obtained by removing all trailing vowels from s.

The vowels consist of the characters 'a', 'e', 'i', 'o', and 'u'.

Read more
[LeetCode] Count Subarrays With K Distinct Integers

3859. Count Subarrays With K Distinct Integers

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

Return an integer denoting the count of subarrays of nums such that:

  • The subarray contains exactly k distinct integers.
  • Within the subarray, each distinct integer appears at least m times.
Read more
[LeetCode] Minimum Bitwise OR From Grid

3858. Minimum Bitwise OR From Grid

You are given a 2D integer array grid of size m x n.

You must select exactly one integer from each row of the grid.

Return an integer denoting the minimum possible bitwise OR of the selected integers from each row.

Read more
[LeetCode] Minimum Capacity Box

3861. Minimum Capacity Box

You are given an integer array capacity, where capacity[i] represents the capacity of the i^th box, and an integer itemSize representing the size of an item.

The i^th box can store the item if capacity[i] >= itemSize.

Return an integer denoting the index of the box with the minimum capacity that can store the item. If multiple such boxes exist, return the smallest index.

If no box can store the item, return -1.

Read more
[LeetCode] Minimum Cost to Split into Ones

3857. Minimum Cost to Split into Ones

You are given an integer n.

In one operation, you may split an integer x into two positive integers a and b such that a + b = x.

The cost of this operation is a * b.

Return an integer denoting the minimum total cost required to split the integer n into n ones.

Read more