Educational Codeforces Round 159 (Rated for Div. 2) E. Collapsing Strings
2053. Kth Distinct String in an Array
A distinct string is a string that is present only once in an array.
Given an array of strings
arr
, and an integerk
, return thekth
*distinct string present inarr
. If there are fewer thank
distinct strings, return an empty string*""
.Note that the strings are considered in the order in which they appear in the array.
3244. Shortest Distance After Road Addition Queries II
You are given an integer
n
and a 2D integer arrayqueries
.There are
n
cities numbered from0
ton - 1
. Initially, there is a unidirectional road from cityi
to cityi + 1
for all0 <= i < n - 1
.
queries[i] = [ui, vi]
represents the addition of a new unidirectional road from cityui
to cityvi
. After each query, you need to find the length of the shortest path from city0
to cityn - 1
.There are no two queries such that
queries[i][0] < queries[j][0] < queries[i][1] < queries[j][1]
.Return an array
answer
where for eachi
in the range[0, queries.length - 1]
,answer[i]
is the length of the shortest path from city0
to cityn - 1
after processing the firsti + 1
queries.
3243. Shortest Distance After Road Addition Queries I
You are given an integer
n
and a 2D integer arrayqueries
.There are
n
cities numbered from0
ton - 1
. Initially, there is a unidirectional road from cityi
to cityi + 1
for all0 <= i < n - 1
.
queries[i] = [ui, vi]
represents the addition of a new unidirectional road from cityui
to cityvi
. After each query, you need to find the length of the shortest path from city0
to cityn - 1
.Return an array
answer
where for eachi
in the range[0, queries.length - 1]
,answer[i]
is the length of the shortest path from city0
to cityn - 1
after processing the firsti + 1
queries.
3242. Design Neighbor Sum Service
You are given a
n x n
2D arraygrid
containing distinct elements in the range[0, n2 - 1]
.Implement the
neighborSum
class:
neighborSum(int [][]grid)
initializes the object.int adjacentSum(int value)
returns the sum of elements which are adjacent neighbors ofvalue
, that is either to the top, left, right, or bottom ofvalue
ingrid
.int diagonalSum(int value)
returns the sum of elements which are diagonal neighbors ofvalue
, that is either to the top-left, top-right, bottom-left, or bottom-right ofvalue
ingrid
.
There are some red and blue tiles arranged circularly. You are given an array of integers
colors
and a 2D integers arrayqueries
.The color of tile
i
is represented bycolors[i]
:
colors[i] == 0
means that tilei
is red.colors[i] == 1
means that tilei
is blue.An alternating group is a contiguous subset of tiles in the circle with alternating colors (each tile in the group except the first and last one has a different color from its adjacent tiles in the group).
You have to process queries of two types:
queries[i] = [1, sizei]
, determine the count of alternating groups with sizesizei
.queries[i] = [2, indexi, colori]
, changecolors[indexi]
tocolori
.Return an array
answer
containing the results of the queries of the first type in order.Note that since
colors
represents a circle, the first and the last tiles are considered to be next to each other.
3241. Time Taken to Mark All Nodes
There exists an undirected tree with
n
nodes numbered0
ton - 1
. You are given a 2D integer arrayedges
of lengthn - 1
, whereedges[i] = [ui, vi]
indicates that there is an edge between nodesui
andvi
in the tree.Initially, all nodes are unmarked. For each node
i
:
- If
i
is odd, the node will get marked at timex
if there is at least one node adjacent to it which was marked at timex - 1
.- If
i
is even, the node will get marked at timex
if there is at least one node adjacent to it which was marked at timex - 2
.Return an array
times
wheretimes[i]
is the time when all nodes get marked in the tree, if you mark nodei
at timet = 0
.Note that the answer for each
times[i]
is independent, i.e. when you mark nodei
all other nodes are unmarked.
3240. Minimum Number of Flips to Make Binary Grid Palindromic II
You are given an
m x n
binary matrixgrid
.A row or column is considered palindromic if its values read the same forward and backward.
You can flip any number of cells in
grid
from0
to1
, or from1
to0
.Return the minimum number of cells that need to be flipped to make all rows and columns palindromic, and the total number of
1
‘s ingrid
divisible by4
.