[LeetCode] Limit Occurrences in Sorted Array

3940. Limit Occurrences in Sorted Array

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

Return an array such that each distinct element appears at most k times, while preserving the relative order of the elements in nums.

Note: If a distinct element appears at least k times, then it must appear exactly k times in the resulting array.

Read more
[LeetCode] Minimum Operations to Sort a Permutation

3942. Minimum Operations to Sort a Permutation

You are given an integer array nums of length n, where nums is a permutation of the integers from 0 to n - 1.

You may perform only the following operations:

  • Reverse the entire array.
  • Rotate Left by One: Move the first element to the end of the array, and rest elements to left by one position.

Return an integer denoting the minimum number of operations required to sort the array in increasing order. If it is not possible to sort the array using only the given operations, return -1.

Read more
[LeetCode] Number of Pairs After Increment

3943. Number of Pairs After Increment

You are given two integer arrays nums1 and nums2, and a 2D integer array queries.

Each queries[i] is one of the following types:

  • [1, x, y, val]Add val to every element in nums2[x..y].
  • [2, tot]Compute the number of pairs (j, k) such that nums1[j] + nums2[k] == tot.

Return an integer array answer, where answer[j] is the number of pairs for the j^th query of type 2.

Read more
[LeetCode] Password Strength

3941. Password Strength

You are given a string password.

The strength of the password is calculated based on the following rules:

  • 1 point for each distinct lowercase letter ('a' to 'z').
  • 2 points for each distinct uppercase letter ('A' to 'Z').
  • 3 points for each distinct digit ('0' to '9').
  • 5 points for each distinct special character from the set "!@#$".

Each character contributes at most once, even if it appears multiple times.

Return an integer denoting the strength of the password.

Read more
[LeetCode] Digit Frequency Score

3945. Digit Frequency Score

You are given an integer n.

The score of n is defined as the sum of d * freq(d) over all distinct digits d, where freq(d) denotes the number of times the digit d appears in n.

Return an integer denoting the score of n.

Read more
[LeetCode] Exactly One Consecutive Set Bits Pair

3950. Exactly One Consecutive Set Bits Pair

You are given an integer n.

Return true if its binary representation contains exactly one adjacent pair of set bits, and false otherwise.

Read more
[LeetCode] Lexicographically Maximum MEX Array

3948. Lexicographically Maximum MEX Array

You are given an integer array nums.

You want to construct an array result by repeatedly performing the following operation until nums becomes empty:

  • Choose an integer k such that 1 <= k <= len(nums).
  • Compute the MEX of the first k elements of nums.
  • Append this MEX to result.
  • Remove the first k elements from nums.

Return the lexicographically maximum array result that can be obtained after performing the operations.

The MEX of an array is the smallest non-negative integer not present in the array.

An array a is lexicographically greater than an array b if in the first position where a and b differ, array a has an element that is greater than the corresponding element in b. If the first min(a.length, b.length) elements do not differ, then the longer array is the lexicographically greater one.

Read more
[LeetCode] Subtree Inversion Sum II

3949. Subtree Inversion Sum II

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

You are also given an integer array nums of length n, where nums[i] represents the value at node i, and an integer k.

You may perform inversion operations on a subset of nodes subject to the following rules:

  • Subtree Inversion Operation:
    • When you invert a node, every value in the subtree rooted at that node is multiplied by -1.
  • Distance Constraint on Inversions:
    • You may only invert a node if it is “sufficiently far” from any other inverted node.
    • If you invert two nodes a and b, the distance (the number of edges on the unique path between them) must be at least k.

Return the maximum possible sum of the tree’s node values after applying inversion operations.

Read more
[LeetCode] Maximum Score with Co-Prime Element

3953. Maximum Score with Co-Prime Element

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

You may change any element in nums to any positive integer less than or equal to maxVal. Each such change costs 1.

Two integers are co-prime if their greatest common divisor (GCD) is 1.

After all modifications, you must choose an index i such that, nums[i] is co-prime with every other element nums[j].

Let:

  • selectedValue be the final value of nums[i] after modifications.
  • modificationCost be the total number of elements changed.

The score is defined as score = selectedValue - modificationCost.

Return the maximum possible score.

Read more
[LeetCode] Maximum Total Value of Covered Indices

3952. Maximum Total Value of Covered Indices

You are given an integer array nums of length n and a binary string s of length n, where s[i] == '1' means index i initially contains a token and s[i] == '0' means it does not.

You may perform the following operation any number of times:

  • Choose a token currently located at index i, where i > 0, such that this token has not been moved before.
  • Move this token from index i to index i - 1.

An index is considered covered if it contains a token after all moves.

Return an integer denoting the maximum total value of nums at the covered indices after optimally performing the operations.

Read more