2647. Color the Triangle Red
Description
You are given an integer n
. Consider an equilateral triangle of side length n
, broken up into n2
unit equilateral triangles. The triangle has n
1-indexed rows where the ith
row has 2i - 1
unit equilateral triangles.
The triangles in the ith
row are also 1-indexed with coordinates from (i, 1)
to (i, 2i - 1)
. The following image shows a triangle of side length 4
with the indexing of its triangle.
![](https://spcdn.pages.dev/leetcode/problems/2647.Color%20the%20Triangle%20Red/images/triangle4.jpg)
Two triangles are neighbors if they share a side. For example:
- Triangles
(1,1)
and(2,2)
are neighbors - Triangles
(3,2)
and(3,3)
are neighbors. - Triangles
(2,2)
and(3,3)
are not neighbors because they do not share any side.
Initially, all the unit triangles are white. You want to choose k
triangles and color them red. We will then run the following algorithm:
- Choose a white triangle that has at least two red neighbors.
<ul> <li>If there is no such triangle, stop the algorithm.</li> </ul> </li> <li>Color that triangle <strong>red</strong>.</li> <li>Go to step 1.</li>
Choose the minimum k
possible and set k
triangles red before running this algorithm such that after the algorithm stops, all unit triangles are colored red.
Return a 2D list of the coordinates of the triangles that you will color red initially. The answer has to be of the smallest size possible. If there are multiple valid solutions, return any.
Example 1:
![](https://spcdn.pages.dev/leetcode/problems/2647.Color%20the%20Triangle%20Red/images/example1.jpg)
Input: n = 3 Output: [[1,1],[2,1],[2,3],[3,1],[3,5]] Explanation: Initially, we choose the shown 5 triangles to be red. Then, we run the algorithm: - Choose (2,2) that has three red neighbors and color it red. - Choose (3,2) that has two red neighbors and color it red. - Choose (3,4) that has three red neighbors and color it red. - Choose (3,3) that has three red neighbors and color it red. It can be shown that choosing any 4 triangles and running the algorithm will not make all triangles red.
Example 2:
![](https://spcdn.pages.dev/leetcode/problems/2647.Color%20the%20Triangle%20Red/images/example2.jpg)
Input: n = 2 Output: [[1,1],[2,1],[2,3]] Explanation: Initially, we choose the shown 3 triangles to be red. Then, we run the algorithm: - Choose (2,2) that has three red neighbors and color it red. It can be shown that choosing any 2 triangles and running the algorithm will not make all triangles red.
Constraints:
1 <= n <= 1000
Solutions
Solution 1: Find the Pattern
We draw a graph to observe, and we can find that the first row only has one triangle and must be colored, and from the last row to the second row, the coloring scheme of every four rows is the same:
- The last row is colored at $(n, 1)$, $(n, 3)$, …, $(n, 2n - 1)$.
- The $n - 1$ row is colored at $(n - 1, 2)$.
- The $n - 2$ row is colored at $(n - 2, 3)$, $(n - 2, 5)$, …, $(n - 2, 2n - 5)$.
- The $n - 3$ row is colored at $(n - 3, 1)$.
![](https://spcdn.pages.dev/leetcode/problems/2647.Color%20the%20Triangle%20Red/images/demo3.png)
Therefore, we can color the first row according to the above rules, and then start from the last row, and color every four rows once until the second row ends.
![](https://spcdn.pages.dev/leetcode/problems/2647.Color%20the%20Triangle%20Red/images/demo2.png)
The time complexity is $(n^2)$, where $n$ is the parameter given in the problem. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
|
|
|
|
|
|
|
|
|
|