Given two positive integers
mandn``board, a pair of positive integers(r, c)which is the starting position of the knight on the board.Your task is to find an order of movements for the knight, in a manner that every cell of the
board
boardin which the cells’ values show the order of visiting the cell starting from 0 (the initial place of the knight).
(r1, c1)to cell(r2, c2)if0 <= r2 <= m - 1and0 <= c2 <= n - 1andmin(abs(r1 - r2), abs(c1 - c2)) = 1andmax(abs(r1 - r2), abs(c1 - c2)) = 2.
2673. Make Costs of Paths Equal in a Binary Tree
You are given an integer
nrepresenting the number of nodes in a perfect binary tree consisting of nodes numbered from1ton. The root of the tree is node1and each nodeiin the tree has two children where the left child is the node2 * iand the right child is2 * i + 1.Each node in the tree also has a cost represented by a given 0-indexed integer array
costof sizenwherecost[i]is the cost of nodei + 1. You are allowed to increment the cost of any node by1any number of times.Return the minimum number of increments you need to make the cost of paths from the root to each leaf node equal.
Note:
- A perfect binary tree is a tree where each node, except the leaf nodes, has exactly 2 children.
- The cost of a path is the sum of costs of nodes in the path.
2672. Number of Adjacent Elements With the Same Color
There is a 0-indexed array
numsof lengthn. Initially, all elements are uncolored (has a value of0).You are given a 2D integer array
querieswherequeries[i] = [indexi, colori].For each query, you color the index
indexiwith the colorcoloriin the arraynums.Return an array
answerof the same length asquerieswhereanswer[i]is the number of adjacent elements with the same color after theithquery.More formally,
answer[i]is the number of indicesj, such that0 <= j < n - 1andnums[j] == nums[j + 1]andnums[j] != 0after theithquery.
Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.
Implement the
FrequencyTrackerclass.
FrequencyTracker(): Initializes theFrequencyTrackerobject with an empty array initially.void add(int number): Addsnumberto the data structure.void deleteOne(int number): Deletes one occurence ofnumberfrom the data structure. The data structure may not containnumber, and in this case nothing is deleted.bool hasFrequency(int frequency): Returnstrueif there is a number in the data structure that occursfrequencynumber of times, otherwise, it returnsfalse.
2670. Find the Distinct Difference Array
You are given a 0-indexed array
numsof lengthn.The distinct difference array of
numsis an arraydiffof lengthnsuch thatdiff[i]is equal to the number of distinct elements in the suffixnums[i + 1, ..., n - 1]subtracted from the number of distinct elements in the prefixnums[0, ..., i].Return the distinct difference array of
nums.Note that
nums[i, ..., j]denotes the subarray ofnumsstarting at indexiand ending at indexjinclusive. Particularly, ifi > jthennums[i, ..., j]denotes an empty subarray.