[LeetCode] Count Residue Prefixes

3803. Count Residue Prefixes

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

A prefix of s is called a residue if the number of distinct characters in the prefix is equal to len(prefix) % 3.

Return the count of residue prefixes in s.

prefix

non-empty substring

Read more
[LeetCode] Minimum Cost to Merge Sorted Lists

3801. Minimum Cost to Merge Sorted Lists

You are given a 2D integer array lists, where each lists[i] is a non-empty array of integers sorted in non-decreasing order.

You may repeatedly choose two lists a = lists[i] and b = lists[j], where i != j, and merge them. The cost to merge a and b is:

len(a) + len(b) + abs(median(a) - median(b)), where len and median denote the list length and median, respectively.

After merging a and b, remove both a and b from lists and insert the new merged sorted list in any position. Repeat merges until only one list remains.

Return an integer denoting the minimum total cost required to merge all lists into one single sorted list.

The median of an array is the middle element after sorting it in non-decreasing order. If the array has an even number of elements, the median is the left middle element.

Read more
[LeetCode] Minimum Cost to Make Two Binary Strings Equal

3800. Minimum Cost to Make Two Binary Strings Equal

You are given two binary strings s and t, both of length n, and three positive integers flipCost, swapCost, and crossCost.

You are allowed to apply the following operations any number of times (in any order) to the strings s and t:

  • Choose any index i and flip s[i] or t[i] (change '0' to '1' or '1' to '0'). The cost of this operation is flipCost.
  • Choose two distinct indices i and j, and swap either s[i] and s[j] or t[i] and t[j]. The cost of this operation is swapCost.
  • Choose an index i and swap s[i] with t[i]. The cost of this operation is crossCost.

Return an integer denoting the minimum total cost needed to make the strings s and t equal.

Read more
[LeetCode] Word Squares II

3799. Word Squares II

You are given a string array words, consisting of distinct 4-letter strings, each containing lowercase English letters.

A word square consists of 4 distinct words: top, left, right and bottom, arranged as follows:

  • top forms the top row.
  • bottom forms the bottom row.
  • left forms the left column (top to bottom).
  • right forms the right column (top to bottom).

It must satisfy:

  • top[0] == left[0], top[3] == right[0]
  • bottom[0] == left[3], bottom[3] == right[3]

Return all valid distinct word squares, sorted in ascending lexicographic order by the 4-tuple (top, left, right, bottom)​​​​​​​.

Read more
[LeetCode] Best Reachable Tower

3809. Best Reachable Tower

You are given a 2D integer array towers, where towers[i] = [x_i, y_i, q_i] represents the coordinates (x_i, y_i) and quality factor q_i of the i^th tower.

You are also given an integer array center = [cx, cy​​​​​​​] representing your location, and an integer radius.

A tower is reachable if its Manhattan distance from center is less than or equal to radius.

Among all reachable towers:

  • Return the coordinates of the tower with the maximum quality factor.
  • If there is a tie, return the tower with the lexicographically smallest coordinate. If no tower is reachable, return [-1, -1].

Manhattan Distance

(x_i, y_i)

(x_j, y_j)

|x_i - x_j| + |y_i - y_j|

A coordinate [x_i, y_i] is lexicographically smaller than [x_j, y_j] if x_i < x_j, or x_i == x_j and y_i < y_j.

|x| denotes the absolute value of x.

Read more
[LeetCode] Count Caesar Cipher Pairs

3805. Count Caesar Cipher Pairs

You are given an array words of n strings. Each string has length m and contains only lowercase English letters.

Two strings s and t are similar if we can apply the following operation any number of times (possibly zero times) so that s and t become equal.

  • Choose either s or t.
  • Replace every letter in the chosen string with the next letter in the alphabet cyclically. The next letter after 'z' is 'a'.

Count the number of pairs of indices (i, j) such that:

  • i < j
  • words[i] and words[j] are similar.

Return an integer denoting the number of such pairs.

Read more
[LeetCode] Maximum Bitwise AND After Increment Operations

3806. Maximum Bitwise AND After Increment Operations

You are given an integer array nums and two integers k and m.

You may perform at most k operations. In one operation, you may choose any index i and increase nums[i] by 1.

Return an integer denoting the maximum possible bitwise AND of any subset of size m after performing up to k operations optimally.

Read more
[LeetCode] Number of Centered Subarrays

3804. Number of Centered Subarrays

You are given an integer array nums.

A subarray of nums is called centered if the sum of its elements is equal to at least one element within that same subarray.

Return the number of centered subarrays of nums.

Read more
[LeetCode] Minimum Edge Toggles on a Tree

3812. Minimum Edge Toggles on a Tree

You are given an undirected tree with n nodes, numbered from 0 to n - 1. It is represented by a 2D integer array edges​​​​​​​ of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.

You are also given two binary strings start and target of length n. For each node x, start[x] is its initial color and target[x] is its desired color.

In one operation, you may pick an edge with index i and toggleboth of its endpoints. That is, if the edge is [u, v], then the colors of nodes u and v each flip from '0' to '1' or from '1' to '0'.

Return an array of edge indices whose operations transform start into target. Among all valid sequences with minimum possible length, return the edge indices in increasing​​​​​​​ order.

If it is impossible to transform start into target, return an array containing a single element equal to -1.

Read more
[LeetCode] Minimum Operations to Reach Target Array

3810. Minimum Operations to Reach Target Array

You are given two integer arrays nums and target, each of length n, where nums[i] is the current value at index i and target[i] is the desired value at index i.

You may perform the following operation any number of times (including zero):

  • Choose an integer value x
  • Find all maximal contiguous segments where nums[i] == x (a segment is maximal if it cannot be extended to the left or right while keeping all values equal to x)
  • For each such segment [l, r], update simultaneously:
    • nums[l] = target[l], nums[l + 1] = target[l + 1], ..., nums[r] = target[r]

Return the minimum number of operations required to make nums equal to target.

Read more