All Posts

Leetcode 2606: Find the Substring With Maximum Cost

You are given a string s, a string chars of distinct characters, and an integer array vals of the same length as chars. The value of each character is determined by either its position in the alphabet or a corresponding value in the vals array if it is present in chars. The cost of a substring is the sum of the values of each character in that substring. Your goal is to find the maximum cost of any substring of s.

Leetcode 2610: Convert an Array Into a 2D Array With Conditions

Given an integer array, your task is to create a 2D array from the input array satisfying the following conditions:

  1. The 2D array should contain all the elements of the input array.
  2. Each row of the 2D array should contain distinct integers.
  3. The number of rows in the 2D array should be minimal. Return the resulting array. If there are multiple valid answers, return any of them.

Leetcode 2615: Sum of Distances

You are given a 0-indexed integer array nums. For each element nums[i], compute the sum of absolute differences |i - j| for all indices j where nums[j] == nums[i] and j != i. If there are no such indices, set the corresponding value in the result array to 0. Return the resulting array.

Leetcode 2641: Cousins in Binary Tree II

Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins’ values. Two nodes are cousins if they have the same depth but different parents. The depth of a node is the number of edges from the root to the node.

Leetcode 2653: Sliding Subarray Beauty

Given an integer array ’nums’ containing ’n’ integers, you are tasked with calculating the beauty of each subarray of size ‘k’. The beauty of a subarray is defined as the xth smallest negative integer in the subarray if it exists, or 0 if there are fewer than ‘x’ negative integers.

Leetcode 2657: Find the Prefix Common Array of Two Arrays

You are given two integer arrays A and B, each of length n, which are permutations of numbers from 1 to n. You need to find the prefix common array of A and B. The prefix common array is defined as an array C where each C[i] represents the number of integers that appear at or before index i in both A and B.