[LeetCode] Smallest Stable Index II

3904. Smallest Stable Index II

You are given an integer array nums of length n and an integer k.

For each index i, define its instability score as max(nums[0..i]) - min(nums[i..n - 1]).

In other words:

  • max(nums[0..i]) is the largest value among the elements from index 0 to index i.
  • min(nums[i..n - 1]) is the smallest value among the elements from index i to index n - 1.

An index i is called stable if its instability score is less than or equal to k.

Return the smallest stable index. If no such index exists, return -1.

Read more
[LeetCode] Valid Digit Number

3908. Valid Digit Number

You are given an integer n and a digit x.

A number is considered valid if:

  • It contains at least one occurrence of digit x, and
  • It does not start with digit x.

Return true if n is valid, otherwise return false.

Read more
[LeetCode] Compare Sums of Bitonic Parts

3909. Compare Sums of Bitonic Parts

You are given a bitonic array nums of length n.

Split the array into two parts:

  • Ascending part: from index 0 to the peak element (inclusive).
  • Descending part: from the peak element to index n - 1 (inclusive).

The peak element belongs to both parts.

Return:

  • 0 if the sum of the ascending part is greater.
  • 1 if the sum of the descending part is greater.
  • -1 if both sums are equal.

Notes:

  • A bitonic array is an array that is strictly increasing up to a single peak element and then strictly decreasing.
  • An array is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
  • An array is said to be strictly decreasing if each element is strictly smaller than its previous one (if exists).
Read more
[LeetCode] Count Connected Subgraphs with Even Node Sum

3910. Count Connected Subgraphs with Even Node Sum

You are given an undirected graph with n nodes labeled from 0 to n - 1. Node i has a value of nums[i], which is either 0 or 1. The edges of the graph are given by a 2D array edges where edges[i] = [u_i, v_i] represents an edge between node u_i and node v_i.

For a non-empty subset s of nodes in the graph, we consider the induced subgraph of s generated as follows:

  • We keep only the nodes in s.
  • We keep only the edges whose two endpoints are both in s.

Return an integer representing the number of non-empty subsets s of nodes in the graph such that:

  • The induced subgraph of s is connected.
  • The sum of node values in s is even.
Read more
[LeetCode] K-th Smallest Remaining Even Integer in Subarray Queries

3911. K-th Smallest Remaining Even Integer in Subarray Queries

You are given an integer array nums where nums is strictly increasing.

You are also given a 2D integer array queries, where queries[i] = [l_i, r_i, k_i].

For each query [l_i, r_i, k_i]:

  • Consider the subarray nums[l_i..r_i]
  • From the infinite sequence of all positive even integers: 2, 4, 6, 8, 10, 12, 14, ...
  • Remove all elements that appear in the subarray nums[l_i..r_i].
  • Find the k_i^th smallest integer remaining in the sequence after the removals.

Return an integer array ans, where ans[i] is the result for the i^th query.

Read more
[LeetCode] Valid Elements in an Array

3912. Valid Elements in an Array

You are given an integer array nums.

An element nums[i] is considered valid if it satisfies at least one of the following conditions:

  • It is strictly greater than every element to its left.
  • It is strictly greater than every element to its right.

The first and last elements are always valid.

Return an array of all valid elements in the same order as they appear in nums.

Read more
[LeetCode] Count Indices With Opposite Parity

3917. Count Indices With Opposite Parity

You are given an integer array nums of length n.

The score of an index i is defined as the number of indices j such that:

  • i < j < n, and
  • nums[i] and nums[j] have different parity (one is even and the other is odd).

Return an integer array answer of length n, where answer[i] is the score of index i.

Read more
[LeetCode] Maximum Sum of Alternating Subsequence With Distance at Least K

3915. Maximum Sum of Alternating Subsequence With Distance at Least K

You are given an integer array nums of length n and an integer k.

Pick a subsequence with indices 0 <= i_1 < i_2 < ... < i_m < n such that:

  • For every 1 <= t < m, i_t+1 - i_t >= k.
  • The selected values form a strictly alternating sequence. In other words, either:
    • nums[i_1] < nums[i_2] > nums[i_3] < ..., or
    • nums[i_1] > nums[i_2] < nums[i_3] > ...

A subsequence of length 1 is also considered strictly alternating. The score of a valid subsequence is the sum of its selected values.

Return an integer denoting the maximum possible score of a valid subsequence.

Read more
[LeetCode] Minimum Operations to Make Array Non Decreasing

3914. Minimum Operations to Make Array Non Decreasing

You are given an integer array nums of length n.

In one operation, you may choose any subarray nums[l..r] and increase each element in that subarray by x, where x is any positive integer.

Return the minimum possible sum of the values of x across all operations required to make the array non-decreasing.

An array is non-decreasing if nums[i] <= nums[i + 1] for all 0 <= i < n - 1.

Read more
[LeetCode] Sort Vowels by Frequency

3913. Sort Vowels by Frequency

You are given a string s consisting of lowercase English characters.

Rearrange only the vowels in the string so that they appear in non-increasing order of their frequency.

If multiple vowels have the same frequency, order them by the position of their first occurrence in s.

Return the modified string.

Vowels are 'a', 'e', 'i', 'o', and 'u'.

The frequency of a letter is the number of times it occurs in the string.

Read more