[LeetCode] Sum of Remoteness of All Cells

2852. Sum of Remoteness of All Cells

You are given a 0-indexed matrix grid of order n * n. Each cell in this matrix has a value grid[i][j], which is either a positive integer or -1 representing a blocked cell.

You can move from a non-blocked cell to any non-blocked cell that shares an edge.

For any cell (i, j), we represent its remoteness as R[i][j] which is defined as the following:

  • If the cell (i, j) is a non-blocked cell, R[i][j] is the sum of the values grid[x][y] such that there is no path from the non-blocked cell (x, y) to the cell (i, j).
  • For blocked cells, R[i][j] == 0.

Return the sum of R[i][j] over all cells.

Read more
[LeetCode] Minimizing Array After Replacing Pairs With Their Product

2892. Minimizing Array After Replacing Pairs With Their Product

Given an integer array nums and an integer k, you can perform the following operation on the array any number of times:

  • Select two adjacent elements of the array like x and y, such that x * y <= k, and replace both of them with a single element with value x * y (e.g. in one operation the array [1, 2, 2, 3] with k = 5 can become [1, 4, 3] or [2, 2, 3], but can’t become [1, 2, 6]).

Return the minimum possible length of nums after any number of operations.

Read more
[LeetCode] The Wording Game

2868. The Wording Game

Alice and Bob each have a lexicographically sorted array of strings named a and b respectively.

They are playing a wording game with the following rules:

  • On each turn, the current player should play a word from their list such that the new word is closely greater than the last played word; then it’s the other player’s turn.
  • If a player can’t play a word on their turn, they lose.

Alice starts the game by playing her lexicographically smallest word.

Given a and b, return true if Alice can win knowing that both players play their best, and false otherwise.

A word w is closely greater than a word z if the following conditions are met:

  • w is lexicographically greater than z.
  • If w1 is the first letter of w and z1 is the first letter of z, w1 should either be equal to z1 or be the letter after z1 in the alphabet.
  • For example, the word "care" is closely greater than "book" and "car", but is not closely greater than "ant" or "cook".

A string s is lexicographically greater than a string t if in the first position where s and t differ, string s has a letter that appears later in the alphabet than the corresponding letter in t. If the first min(s.length, t.length) characters do not differ, then the longer string is the lexicographically greater one.

Read more
[Codeforces] Round 734 (Div. 3) E. Fixed PointsRead more
[Codeforces] Round 740 (Div. 1, based on VK Cup 2021 - Final (Engine)) C. Bottom-Tier ReversalsRead more
[Codeforces] Global Round 16 E. Buds Re-hangingRead more
[Codeforces] Round 742 (Div. 2) D. Expression Evaluation ErrorRead more
[Codeforces] Educational Round 114 (Rated for Div. 2) D. The Strongest BuildRead more
[LeetCode] Apply Operations on Array to Maximize Sum of Squares

2897. Apply Operations on Array to Maximize Sum of Squares

You are given a 0-indexed integer array nums and a positive integer k.

You can do the following operation on the array any number of times:

  • Choose any two distinct indices i and j and simultaneously update the values of nums[i] to (nums[i] AND nums[j]) and nums[j] to (nums[i] OR nums[j]). Here, OR denotes the bitwise OR operation, and AND denotes the bitwise AND operation.

You have to choose k elements from the final array and calculate the sum of their squares.

Return the maximum sum of squares you can achieve.

Since the answer can be very large, return it modulo 109 + 7.

Read more
[LeetCode] Apply Operations to Make Two Strings Equal

2896. Apply Operations to Make Two Strings Equal

You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.

You can perform any of the following operations on the string s1 any number of times:

  • Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.
  • Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.

Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.

Note that flipping a character means changing it from 0 to 1 or vice-versa.

Read more