[Codeforces] Round 428 (Div. 2) B. Game of the RowsRead more
[Codeforces] 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred) I. Noise LevelRead more
[Codeforces] Round 431 (Div. 1) B. Rooter`s SongRead more
[Codeforces] Educational Codeforces Round 28 D. MonitorRead more
[LeetCode] is Array a Preorder of Some ‌Binary Tree

2764. is Array a Preorder of Some ‌Binary Tree

Given a 0-indexed integer 2D array nodes, your task is to determine if the given array represents the preorder traversal of some binary tree.

For each index i, nodes[i] = [id, parentId], where id is the id of the node at the index i and parentId is the id of its parent in the tree (if the node has no parent, then parentId = -1).

Return true if the given array represents the preorder traversal of some tree, and false otherwise.

Note: the preorder traversal of a tree is a recursive way to traverse a tree in which we first visit the current node, then we do the preorder traversal for the left child, and finally, we do it for the right child.

Read more
[LeetCode] Height of Special Binary Tree

2773. Height of Special Binary Tree

You are given a root``n nodes. The nodes of the special binary tree are numbered from 1 to n. Suppose the tree has k leaves in the following order: b1 < b2 < ... < bk.

bi, the following conditions hold:

  • The right child of bi is bi + 1 if i < k, and b1 otherwise.
  • The left child of bi is bi - 1 if i > 1, and bk otherwise.

Return the height of the given tree.

Note: The height of a binary tree is the length of the longest path from the root to any other node.

Read more
[LeetCode] Count Nodes That Are Great Enough

2792. Count Nodes That Are Great Enough

You are given a root to a binary tree and an integer k. A node of this tree is called great enough if the followings hold:

  • Its subtree has at least k nodes.
  • Its value is greater than the value of at least k nodes in its subtree.

Return the number of nodes in this tree that are great enough.

The node u is in the subtree of the node v, if u == v or v is an ancestor of u.

Read more
[LeetCode] Number of Unique Categories

2782. Number of Unique Categories

You are given an integer n and an object categoryHandler of class CategoryHandler.

There are nelements, numbered from 0 to n - 1. Each element has a category, and your task is to find the number of unique categories.

The class CategoryHandler contains the following function, which may help you:

  • boolean haveSameCategory(integer a, integer b): Returns true if a and b are in the same category and false otherwise. Also, if either a or b is not a valid number (i.e. it’s greater than or equal to nor less than 0), it returns false.

Return the number of unique categories.

Read more
[LeetCode] Minimum Time Takes to Reach Destination Without Drowning

2814. Minimum Time Takes to Reach Destination Without Drowning

You are given an n * m 0-indexed grid of string land. Right now, you are standing at the cell that contains "S", and you want to get to the cell containing "D". There are three other types of cells in this land:

  • ".": These cells are empty.
  • "X": These cells are stone.
  • "*": These cells are flooded.

At each second, you can move to a cell that shares a side with your current cell (if it exists). Also, at each second, every empty cell that shares a side with a flooded cell becomes flooded as well.
There are two problems ahead of your journey:

  • You can’t step on stone cells.
  • You can’t step on flooded cells since you will drown (also, you can’t step on a cell that will be flooded at the same time as you step on it).

Return the minimum time it takes you to reach the destination in seconds, or -1 if it is impossible.

Note that the destination will never be flooded.

Read more
[Codeforces] Round 435 (Div. 2) C. Mahmoud and Ehab and the xorRead more