[LeetCode] Palindromic Path Queries in a Tree

3841. Palindromic Path Queries in a Tree

You are given an undirected tree with n nodes labeled 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 a string s of length n consisting of lowercase English letters, where s[i] represents the character assigned to node i.

You are also given a string array queries, where each queries[i] is either:

  • "update u_i c": Change the character at node u_i to c. Formally, update s[u_i] = c.
  • "query u_i v_i": Determine whether the string formed by the characters on the unique path from u_i to v_i (inclusive) can be rearranged into a palindrome.

Return a boolean array answer, where answer[j] is true if the j^th query of type "query u_i v_i"​​​​​​​ can be rearranged into a palindrome, and false otherwise.

Read more
[LeetCode] Toggle Light Bulbs

3842. Toggle Light Bulbs

You are given an array bulbs of integers between 1 and 100.

There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially.

For each element bulbs[i] in the array bulbs:

  • If the bulbs[i]^th light bulb is currently off, switch it on.
  • Otherwise, switch it off.

Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.

Read more
[LeetCode] Count Dominant Indices

3833. Count Dominant Indices

You are given an integer array nums of length n.

An element at index i is called dominant if: nums[i] > average(nums[i + 1], nums[i + 2], ..., nums[n - 1])

Your task is to count the number of indices i that are dominant.

The average of a set of numbers is the value obtained by adding all the numbers together and dividing the sum by the total number of numbers.

Note: The rightmost element of any array is not dominant.

Read more
[LeetCode] Design Ride Sharing System

3829. Design Ride Sharing System

A ride sharing system manages ride requests from riders and availability from drivers. Riders request rides, and drivers become available over time. The system should match riders and drivers in the order they arrive.

Implement the RideSharingSystem class:

  • RideSharingSystem() Initializes the system.
  • void addRider(int riderId) Adds a new rider with the given riderId.
  • void addDriver(int driverId) Adds a new driver with the given driverId.
  • int[] matchDriverWithRider() Matches the earliest available driver with the earliest waiting rider and removes both of them from the system. Returns an integer array of size 2 where result = [driverId, riderId] if a match is made. If no match is available, returns [-1, -1].
  • void cancelRider(int riderId) Cancels the ride request of the rider with the given riderId if the rider exists and has not yet been matched.
Read more
[LeetCode] Final Element After Subarray Deletions

3828. Final Element After Subarray Deletions

You are given an integer array nums.

Two players, Alice and Bob, play a game in turns, with Alice playing first.

  • In each turn, the current player chooses any subarray nums[l..r] such that r - l + 1 < m, where m is the current length of the array.
  • The selected subarray is removed, and the remaining elements are concatenated to form the new array.
  • The game continues until only one element remains.

Alice aims to maximize the final element, while Bob aims to minimize it. Assuming both play optimally, return the value of the final remaining element.

Read more
[LeetCode] Longest Alternating Subarray After Removing At Most One Element

3830. Longest Alternating Subarray After Removing At Most One Element

You are given an integer array nums.

A subarray nums[l..r] is alternating if one of the following holds:

  • nums[l] < nums[l + 1] > nums[l + 2] < nums[l + 3] > ...
  • nums[l] > nums[l + 1] < nums[l + 2] > nums[l + 3] < ...

In other words, if we compare adjacent elements in the subarray, then the comparisons alternate between strictly greater and strictly smaller.

You can remove at most one element from nums. Then, you select an alternating subarray from nums.

Return an integer denoting the maximum length of the alternating subarray you can select.

A subarray of length 1 is considered alternating.

Read more
[LeetCode] Find the Score Difference in a Game

3847. Find the Score Difference in a Game

You are given an integer array nums, where nums[i] represents the points scored in the i^th game.

There are exactlytwo players. Initially, the first player is active and the second player is inactive.

The following rules apply sequentially for each game i:

  • If nums[i] is odd, the active and inactive players swap roles.
  • In every 6th game (that is, game indices 5, 11, 17, ...), the active and inactive players swap roles.
  • The active player plays the i^th game and gains nums[i] points.

Return the score difference, defined as the first player’s total score minus the second player’s total score.

Read more
[LeetCode] Find the Smallest Balanced Index

3862. Find the Smallest Balanced Index

You are given an integer array nums.

An index i is balanced if the sum of elements strictly to the left of i equals the product of elements strictly to the right of i.

If there are no elements to the left, the sum is considered as 0. Similarly, if there are no elements to the right, the product is considered as 1.

Return an integer denoting the smallest balanced index. If no balanced index exists, return -1.

Read more
[LeetCode] First Element with Unique Frequency

3843. First Element with Unique Frequency

You are given an integer array nums.

Return an integer denoting the first element (scanning from left to right) in nums whose frequency is unique. That is, no other integer appears the same number of times in nums. If there is no such element, return -1.

Read more
[LeetCode] Longest Almost-Palindromic Substring

3844. Longest Almost-Palindromic Substring

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

A substring is almost-palindromic if it becomes a palindrome after removing exactly one character from it.

Return an integer denoting the length of the longest almost-palindromic substring in s.

Read more