[LeetCode] Nearest Available Drone

4024. Nearest Available Drone

You are given a 2D integer array drones, where drones[i] = [x_i, y_i, range_i] represents the x-coordinate, y-coordinate, and travel range of the i^th drone.

You are also given an integer array target = [tx, ty], representing the coordinates of the target.

A drone drones[i] can reach the target if the Manhattan distance between its coordinates and the target coordinates is less than or equal to its range_i.

Return the index of the reachable drone with the minimum Manhattan distance to the target. If there is a tie, return the smallest index. If no drone can reach the target, return -1.

Read more
[LeetCode] Find All Numbers Disappeared in an Array II

4031. Find All Numbers Disappeared in an Array II

You are given an integer array nums and two integers lower and upper.

A missing integer is an integer in the inclusive range [lower, upper] that does not appear in nums.

Return a 2D integer array where each element is of the form [start, end], representing a contiguous range of missing integers. Return the ranges in increasing order. If there are no missing integers, return an empty array.

Note: Consecutive missing integers should be grouped into a single range.

Read more
[LeetCode] Longest Subarray With at Most K Distinct Prime Factors

4032. Longest Subarray With at Most K Distinct Prime Factors

You are given an integer array nums consisting of positive integers and an integer k.

The prime factor set of a subarray is the union of the distinct prime factors of all its elements.

Return the length of the longest subarray whose prime factor set contains at most k distinct prime factors. If no such subarray exists, return 0.

Read more
[LeetCode] Valid K-Unique Subarrays I

4033. Valid K-Unique Subarrays I

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

You are also given a 2D integer array queries, where queries[i] = [l_i, r_i] represents the subarray nums[l_i..r_i].

For each query, the subarray nums[l_i..r_i] is considered valid if:

  • It contains exactly k distinct numbers, and
  • The frequency of every number in the subarray is even.

Return a boolean array ans, where ans[i] is true if nums[l_i..r_i] is valid, and false otherwise.

Read more
[LeetCode] Lexicographically Largest String After Pair Transformations

4036. Lexicographically Largest String After Pair Transformations

You are given an integer array nums.

For each integer x in nums, start with a string consisting of exactly x lowercase 'a' characters.

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

  • Choose two adjacent equal letters and replace them with the next letter in the alphabet.

For example, "aa" can be replaced with "b", and "bb" can be replaced with "c". The pair "zz" cannot be replaced.

For each x, determine the lexicographically largest string that can be obtained.

Return an array of strings where the i^th string is the answer for nums[i].

A string a is lexicographically larger than a string b if, at the first position where they differ, a contains a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters are equal, the longer string is lexicographically larger.

Read more
[LeetCode] Minimum Bishop Moves to Reach Target

4034. Minimum Bishop Moves to Reach Target

There is an 8 x 8 empty chessboard with 1-indexed rows and columns.

You are given an array source = [sr, sc] representing the starting position of a bishop, and an array target = [tr, tc] representing the target position.

In one move, the bishop travels one or more squares along a single diagonal direction, staying within the board.

Return the minimum number of moves for the bishop to land exactly on target. If it can never reach target, return -1.

Read more
[LeetCode] Minimum Operations to Form Subset Sum I

4040. Minimum Operations to Form Subset Sum I

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

In one operation, choose an element with current value x and replace it with either 2 * x or floor(x / 2).

For each element, all multiplication operations performed on it must occur before any division operations performed on it.

Return the minimum number of operations needed so that some subset of the resulting array has a sum exactly equal to sum. If it is impossible, return -1.

The floor() function returns the integer part of the division.

Read more
[LeetCode] Merge Close Characters II

4019. Merge Close Characters II

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

Two equal characters s[i] and s[j], where 0 <= i < j < s.length, are considered close if j - i <= k. All indices refer to the current string.

Repeatedly perform the following operation until no close pair remains:

  • Among all close pairs (i, j), choose the pair with the smallest i. If multiple pairs have the same i, choose the one with the smallest j.
  • Merge the right character into the left character by removing s[j] from s. The character s[i] remains unchanged, and the remaining characters are reindexed.

Return the resulting string after performing all possible merges.

Read more
[LeetCode] Maximum Valid Split Positions I

4035. Maximum Valid Split Positions I

You are given an integer array nums.

You may remove at most one element from nums. Let arr be the array of remaining elements in their original order, and let m be its length.

A split position i of arr is valid if:

  • 0 <= i < m - 1, and
  • gcd(arr[0..i]) == gcd(arr[i + 1..m - 1]).

An array of length 1 has no valid split positions.

The score of arr is the number of valid split positions in it.

Return the maximum possible score of arr.

Here, gcd(a) denotes the greatest common divisor of all elements in the array a.

Read more
[LeetCode] Minimum Operations to Form Subset Sum II

4041. Minimum Operations to Form Subset Sum II

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

In one operation, choose an element with current value x and replace it with either 2 * x or floor(x / 2).

For each element, multiplication and division operations may be performed in any order.

Return the minimum number of operations needed so that some subset of the resulting array has a sum exactly equal to sum. If it is impossible, return -1.

The floor() function returns the integer part of the division.

Read more