[LeetCode] Minimum Possible Maximum Waiting Time

4009. Minimum Possible Maximum Waiting Time

You are given an integer array demand, where demand[i] is the amount of fuel required by the i^th car.

You are also given an integer array fuel of length 2. There are exactly two fuel dispensers, numbered 0 and 1, where fuel[j] is the initial amount of fuel available in dispenser j.

Cars are allowed to start refueling in increasing index order. Car 0 becomes allowed at time 0, and for each i > 0, car i becomes allowed exactly when car i - 1 starts refueling.

The refueling process follows these rules:

  • Each dispenser can serve at most one car at a time.
  • When a car becomes allowed, you must choose a dispenser with at least demand[i] fuel remaining. If both dispensers have enough fuel remaining, you may choose either of them, regardless of when they become free.
  • The car waits until the chosen dispenser becomes free and starts refueling immediately. It cannot switch dispensers or intentionally wait after the chosen dispenser becomes free.
  • When a car starts refueling, the remaining fuel in the chosen dispenser decreases by demand[i], and the dispenser remains occupied for demand[i] seconds.
  • Once started, refueling cannot be interrupted.
  • If neither dispenser has at least demand[i] fuel remaining when car i becomes allowed, the process terminates and no further cars can be served.

The waiting time of a car is the time between when it becomes allowed to start refueling and when it actually starts.

Return the minimum possible value of the maximum waiting time among all served cars over all assignments that maximize the number of served cars. If no car can be served, return -1.

Read more
[LeetCode] Widest Possible Fence

4007. Widest Possible Fence

You are given an integer array planks, where planks[i] represents the height of the i^th wooden plank. Each plank has a width of 1 unit.

You want to build a fence consisting of planks that all have the same height.

You may either use a plank as is, or combine exactly two distinct original planks into a single plank whose height equals the sum of their heights. Each original plank can be used at most once, and not all original planks need to be used.

Return the maximum possible width of the fence that can be built.

Read more
[LeetCode] Count of Unfinished Tasks After Each Shift

4012. Count of Unfinished Tasks After Each Shift

You are given two integer arrays tasks and shifts.

  • tasks[i] represents the time required to complete the i_th task.
  • shifts[j] represents the amount of time available during the j^th shift.

The tasks must be processed in order from left to right.

  • Carry-over: If a task is not completed during a shift, processing continues from the same point in that task during the next shift.
  • Restart: If all tasks are completed during a shift, the shift ends immediately. Any unused time in that shift is discarded, and the next shift begins again from task 0.

A task is unfinished if it has not been fully completed. This includes a task that is currently in progress.

Return an integer array ans where ans[j] is the number of unfinished tasks immediately after the j^th shift.

Read more
[LeetCode] Count Subarrays With Even Odd Ratio I

4011. Count Subarrays With Even Odd Ratio I

You are given an integer array nums and two integers a and b.

For a subarray, let:

  • x be the number of even elements.
  • y be the number of odd elements.

The ratio of even to odd elements in a subarray is defined as x / y, where ratios are compared by their exact rational values.

A subarray is considered valid if:

  • y > 0, and
  • x / y <= a / b.

Return the number of valid subarrays in nums.

Read more
[LeetCode] Count Subarrays With Even Odd Ratio II

4013. Count Subarrays With Even Odd Ratio II

You are given an integer array nums and two integers a and b.

For a subarray, let:

  • x be the number of even elements.
  • y be the number of odd elements.

The ratio of even to odd elements in a subarray is defined as x / y, where ratios are compared by their exact rational values.

A subarray is considered valid if:

  • y > 0, and
  • x / y <= a / b.

Return the number of valid subarrays in nums.

Read more
[LeetCode] Maximize Pair Strength Using GCD

4010. Maximize Pair Strength Using GCD

You are given an integer array nums.

Choose exactly one pair of distinct indices i and j. The strength of the pair is defined as (nums[i] * nums[j]) / gcd(nums[i], nums[j])^2.

Return the maximum strength over all possible pairs.

Read more
[LeetCode] Minimum Total Price After Applying Discounts

4014. Minimum Total Price After Applying Discounts

You are given two integer arrays prices and discounts.

The value prices[i] represents the price of the i^th item, and discounts[j] represents a discount percentage.

You may apply discounts subject to the following rules:

  • Each discount can be applied to at most one item.
  • Each item can receive at most one discount.
  • An item may also receive no discount.

If a discount of d percent is applied to an item with price p, its final price becomes (p * (100 - d)) / 100. The final price is not rounded.

Return the minimum possible sum of final prices after assigning discounts optimally. Answers within 10^-5 of the actual answer will be accepted.

Read more
[LeetCode] Total Sum of Interaction Cost in Tree Groups II

4018. Total Sum of Interaction Cost in Tree Groups II

You are given an integer n and 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 undirected edge between nodes u_i and v_i.

You are also given an integer array group of length n, where group[i] denotes the group label assigned to node i.

  • Two nodes u and v belong to the same group if and only if group[u] == group[v].
  • The interaction cost between two nodes is the shortest distance between them in the tree.

Return the sum of interaction costs over all pairs of node indices (u, v) such that 0 <= u < v < n and group[u] == group[v].

The shortest distance between two nodes is the number of edges on the unique path connecting them in the tree.

Read more
[LeetCode] Maximum Valid Split Positions II

4037. Maximum Valid Split Positions II

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] Minimize the Maximum Waiting Time at Synchronized Traffic Lights

4025. Minimize the Maximum Waiting Time at Synchronized Traffic Lights

You are given an integer period and an integer array lights, where lights[i] is the duration, in seconds, of the green phase of the i^th traffic light.

At time 0, every traffic light starts at the beginning of its green phase. Their cycles are synchronized: every traffic light starts a new cycle at the same time, and every cycle lasts exactly period seconds. Therefore, the red phase of the i_th traffic light lasts for period - lights[i] seconds.

You are also given an integer array arrivalTime, where arrivalTime[j] is the arrival time, in seconds, of the j^th car.

Each car must be assigned to exactly one traffic light. Multiple cars may be assigned to the same traffic light. Any number of cars may cross the same traffic light simultaneously while it is green. Cars do not block or delay one another.

For a car j assigned to the i^th traffic light, let r = arrivalTime[j] % period. If r < lights[i], its waiting time is 0. Otherwise, its waiting time is period - r.

The penalty of an assignment is the maximum waiting time among all cars.

Return an integer denoting the minimum possible penalty.

Read more