[LeetCode] Minimize Array Sum Using Divisible Replacements

3927. Minimize Array Sum Using Divisible Replacements

You are given an integer array nums.

You can perform the following operation any number of times:

  • Choose two indices a and b such that nums[a] % nums[b] == 0.
  • Replace nums[a] with nums[b].

Return the minimum possible sum of the array after performing any number of operations.

Read more
[LeetCode] Minimum Cost to Buy Apples II

3928. Minimum Cost to Buy Apples II

You are given an integer n and an integer array prices of length n, where prices[i] is the price of apples at shop i.

You are also given a 2D integer array roads, where roads[i] = [u_i, v_i, cost_i, tax_i] represents a bidirectional road:

  • u_i and v_i are the shops connected by the road.
  • cost_i is the cost to travel the road without carrying apples.
  • tax_i is the multiplier applied to cost_i when traveling with apples.

For each shop i, you can either:

  • Buy apples locally at shop i for prices[i].
  • Travel empty to any shop j using any number of roads, buy apples for prices[j], and return to shop i while carrying apples, paying cost * tax on each road used for the return trip.

The forward path, where you travel empty, and the return path may be different.

Return an integer array ans of length n, where ans[i] is the minimum total cost to buy apples starting from shop i.

Read more
[LeetCode] Count K-th Roots in a Range

3932. Count K-th Roots in a Range

You are given three integers l, r, and k.

An integer y is said to be a perfect k^th power if there exists an integer x such that y = x^k.

Return the number of integers y in the range [l, r] (inclusive) that are perfect k^th powers.

Read more
[LeetCode] Largest Local Values in a Matrix II

3933. Largest Local Values in a Matrix II

You are given an n x m integer matrix matrix containing non-negative integers.

A non-zerocell (row, col) checks the cells near it as follows:

  • Let x = matrix[row][col].
  • Consider every cell within x rows and x columns of (row, col).
  • Ignore cells that are outside the matrix.
  • Ignore the cells where both the row distance and column distance are exactly x.

The cell (row, col) is a local maximum if it is non-zero and no considered cell has a value greater than x.

Return an integer denoting the number of local maximums in matrix.

​​​​​​​Example 1:

Input: matrix = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,2,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
Output: 1
​​​​​​​​​​​​​​​​​​​​​
Explanation:

For the non-zero cell (3, 3), x = matrix[3][3] = 2.
The highlighted cells are the considered cells within x rows and x columns of (3, 3).
The four cells with both row and column distances equal to x = 2 are ignored.
No considered cell has a value greater than 2, so (3, 3) is a local maximum.
There are no other non-zero cells, so the answer is 1.

Read more
[LeetCode] Power Update After K-th Largest Insertion I

3935. Power Update After K-th Largest Insertion I

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

You are also given a 2D integer array queries, where each queries[i] = [val_i, k_i] and the difference between consecutive k_i values is always less than 10.

For each query:

  • Insert val_i into nums.
  • Let x be the k_i^th largest element in the current nums.
  • Update p to p^x % (10^9 + 7).

Return an array ans where the ans[i] represents the value of p after processing the i^th query.

Read more
[LeetCode] Smallest Unique Subarray

3934. Smallest Unique Subarray

You are given an integer array nums.

Find the minimumlength of a subarray that is not identical to any other subarray in nums.

Return an integer denoting the minimum possible length of such a subarray.

Two subarrays are considered identical if they have the same length and the same elements in corresponding positions.

Read more
[LeetCode] Maximum Path Intersection Sum in a Grid

3938. Maximum Path Intersection Sum in a Grid

You are given an m x n integer matrix grid.

Two players move across the grid:

  • Player 1 starts at the top-left cell (0, 0) and can move only right or down. Their destination is the bottom-right cell (m - 1, n - 1).
  • Player 2 starts at the bottom-left cell (m - 1, 0) and can move only right or up. Their destination is the top-right cell (0, n - 1).

Each player must choose a valid path from their respective starting cell to their destination.

A cell is called shared if it belongs to both chosen paths.

Return an integer denoting the maximum possible sum of values of all shared cells.

Read more
[LeetCode] Minimum Operations to Make Array Modulo Alternating I

3937. Minimum Operations to Make Array Modulo Alternating I

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

In one operation, you can increase or decrease any element of nums by 1.

An array is called modulo alternating if there exist two distinct integers x and y (0 <= x, y < k) such that:

  • For every even index i, nums[i] % k == x
  • For every odd index i, nums[i] % k == y

Return the minimum number of operations required to make nums modulo alternating.

Read more
[LeetCode] Minimum Swaps to Move Zeros to End

3936. Minimum Swaps to Move Zeros to End

You are given an integer array nums.

In one operation, you can choose any two distinct indices i and j and swap nums[i] and nums[j].

Return an integer denoting the minimum number of operations required to move all 0s to the end of the array.

Read more
[LeetCode] Count Non Adjacent Subsets in a Rooted Tree

3939. Count Non Adjacent Subsets in a Rooted Tree

You are given a rooted tree with n nodes labeled from 0 to n - 1, represented by an integer array parent of length n, where:

  • parent[0] = -1 (node 0 is the root).
  • For each 1 <= i < n, parent[i] is the parent of node i (0 <= parent[i] < i).

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

A non-empty subset of nodes is called valid if:

  • The sum of the values of the selected nodes is divisible by k.
  • No two selected nodes are adjacent in the tree (no node and its direct parent are both included in the subset).

Return the number of valid subsets modulo 10^9 + 7.

Read more