Skip to content

Commit 474d0cb

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (48.12%)
1 parent 0177a2d commit 474d0cb

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer <code>n</code>, indicating that you must do the following routine for <code>n</code> minutes:</p>
2+
3+
<ul>
4+
<li>At the first minute, color <strong>any</strong> arbitrary unit cell blue.</li>
5+
<li>Every minute thereafter, color blue <strong>every</strong> uncolored cell that touches a blue cell.</li>
6+
</ul>
7+
8+
<p>Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.</p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;" />
10+
<p>Return <em>the number of <strong>colored cells</strong> at the end of </em><code>n</code> <em>minutes</em>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> n = 1
17+
<strong>Output:</strong> 1
18+
<strong>Explanation:</strong> After 1 minute, there is only 1 blue cell, so we return 1.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> n = 2
25+
<strong>Output:</strong> 5
26+
<strong>Explanation:</strong> After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
34+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Approach 2: Math
2+
3+
# Time: O(1)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def coloredCells(self, n: int) -> int:
8+
return 1 + 4 * (n - 1) * n // 2
9+

0 commit comments

Comments
 (0)