[LeetCode] Maximum Good Subarray Sum

3026. Maximum Good Subarray Sum

You are given an array nums of length n and a positive integer k.

A subarray of nums is called good if the absolute difference between its first and last element is exactly k, in other words, the subarray nums[i..j] is good if |nums[i] - nums[j]| == k.

Return the maximum sum of a good subarray of nums. If there are no good subarrays**, return 0.

Read more
[LeetCode] Find the Number of Ways to Place People II

3027. Find the Number of Ways to Place People II

You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis (decreasing y-coordinate)

You have to place n people, including Chisato and Takina, at these points such that there is exactly one person at every point. Chisato wants to be alone with Takina, so Chisato will build a rectangular fence with Chisato’s position as the upper left corner and Takina’s position as the lower right corner of the fence (Note that the fence might not enclose any area, i.e. it can be a line). If any person other than Chisato and Takina is either inside the fence or on the fence, Chisato will be sad.

Return the number of pairs of points where you can place Chisato and Takina, such that Chisato does not become sad on building the fence.

Note that Chisato can only build a fence with Chisato’s position as the upper left corner, and Takina’s position as the lower right corner. For example, Chisato cannot build either of the fences in the picture below with four corners (1, 1), (1, 3), (3, 1), and (3, 3), because:

  • With Chisato at (3, 3) and Takina at (1, 1), Chisato’s position is not the upper left corner and Takina’s position is not the lower right corner of the fence.
  • With Chisato at (1, 3) and Takina at (1, 1), Takina’s position is not the lower right corner of the fence.
Read more
[LeetCode] Find the Number of Ways to Place People I

3025. Find the Number of Ways to Place People I

You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis (decreasing y-coordinate)

You have to place n people, including Chisato and Takina, at these points such that there is exactly one person at every point. Chisato wants to be alone with Takina, so Chisato will build a rectangular fence with Chisato’s position as the upper left corner and Takina’s position as the lower right corner of the fence (Note that the fence might not enclose any area, i.e. it can be a line). If any person other than Chisato and Takina is either inside the fence or on the fence, Chisato will be sad.

Return the number of pairs of points where you can place Chisato and Takina, such that Chisato does not become sad on building the fence.

Note that Chisato can only build a fence with Chisato’s position as the upper left corner, and Takina’s position as the lower right corner. For example, Chisato cannot build either of the fences in the picture below with four corners (1, 1), (1, 3), (3, 1), and (3, 3), because:

  • With Chisato at (3, 3) and Takina at (1, 1), Chisato’s position is not the upper left corner and Takina’s position is not the lower right corner of the fence.
  • With Chisato at (1, 3) and Takina at (1, 1), Takina’s position is not the lower right corner of the fence.
Read more
[LeetCode] Type of Triangle II

3024. Type of Triangle II

You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.

  • A triangle is called equilateral if it has all sides of equal length.
  • A triangle is called isosceles if it has exactly two sides of equal length.
  • A triangle is called scalene if all its sides are of different lengths.

Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.

Read more
[LeetCode] Max Consecutive Ones

485. Max Consecutive Ones

Given a binary array nums, return the maximum number of consecutive 1‘s in the array.

Read more
[LeetCode] Find Pattern in Infinite Stream I

3023. Find Pattern in Infinite Stream I

You are given a binary array pattern and an object stream of class InfiniteStream representing a 0-indexed infinite stream of bits.

The class InfiniteStream contains the following function:

  • int next(): Reads a single bit (which is either 0 or 1) from the stream and returns it.

Return the first starting index where the pattern matches the bits read from the stream. For example, if the pattern is [1, 0], the first match is the highlighted part in the stream [0, **1, 0**, 1, ...].

Read more
[LeetCode] Maximum Subtree of the Same Color

3004. Maximum Subtree of the Same Color

You are given a 2D integer array edges representing a tree with n nodes, numbered from 0 to n - 1, rooted at node 0, where edges[i] = [ui, vi] means there is an edge between the nodes vi and ui.

You are also given a 0-indexed integer array colors of size n, where colors[i] is the color assigned to node i.

We want to find a node v such that every node in the subtree of v has the same color.

Return the size of such subtree with the maximum number of nodes possible.

Read more
[LeetCode] Number of Same-End Substrings

2955. Number of Same-End Substrings

You are given a 0-indexed string s, and a 2D array of integers queries, where queries[i] = [li, ri] indicates a substring of s starting from the index li and ending at the index ri (both inclusive), i.e. s[li..ri].

Return an array ans where ans[i] is the number of same-end substrings of queries[i].

A 0-indexed string t of length n is called same-end if it has the same character at both of its ends, i.e., t[0] == t[n - 1].

A substring is a contiguous non-empty sequence of characters within a string.

Read more
[LeetCode] Find the Closest Palindrome

564. Find the Closest Palindrome

Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.

The closest is defined as the absolute difference minimized between two integers.

Read more
[Codeforces] Round 646 (Div. 2) D. Guess The MaximumsRead more