Skip to content

Commit 18059ff

Browse files
committed
problem: 0228. Summary Ranges
1 parent ccac5db commit 18059ff

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 0200. [Number of Islands](https://leetcode.com/problems/number-of-islands/)
2+
3+
Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
4+
5+
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
6+
7+
#### Example 1:
8+
9+
<pre><code><strong>Input:</strong> grid = [
10+
["1","1","1","1","0"],
11+
["1","1","0","1","0"],
12+
["1","1","0","0","0"],
13+
["0","0","0","0","0"]
14+
]
15+
<strong>Output:</strong> 1</code></pre>
16+
17+
#### Example 2:
18+
19+
<pre><code><strong>Input:</strong> grid = [
20+
["1","1","0","0","0"],
21+
["1","1","0","0","0"],
22+
["0","0","1","0","0"],
23+
["0","0","0","1","1"]
24+
]
25+
<strong>Output:</strong> 3</code></pre>
26+
27+
#### Constraints:
28+
29+
- `m == grid.length`
30+
- `n == grid[i].length`
31+
- `1 <= m, n <= 300`
32+
- `grid[i][j]` is `'0'` or `'1'`.

solutions/solution_0228/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 0228. [Number of Islands](https://leetcode.com/problems/number-of-islands/)
2+
3+
Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
4+
5+
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
6+
7+
#### Example 1:
8+
9+
<pre><code><strong>Input:</strong> nums = [0,1,2,4,5,7]
10+
<strong>Output:</strong> ["0->2","4->5","7"]
11+
<strong>Explanation:</strong>The ranges are:
12+
[0,2] --> "0->2"
13+
[4,5] --> "4->5"
14+
[7,7] --> "7"</code></pre>
15+
16+
#### Example 2:
17+
18+
<pre><code><strong>Input:</strong> nums = [0,2,3,4,6,8,9]
19+
<strong>Output:</strong> ["0","2->4","6","8->9"]
20+
<strong>Explanation:</strong> The ranges are:
21+
[0,0] --> "0"
22+
[2,4] --> "2->4"
23+
[6,6] --> "6"
24+
[8,9] --> "8->9"</code></pre>
25+
26+
#### Constraints:
27+
28+
- `0 <= nums.length <= 20`
29+
- `-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1`
30+
- All the values of `nums` are **unique**.
31+
- `nums` is sorted in ascending order.

solutions/solution_0228/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def summaryRanges(self, nums: list[int]) -> list[str]:
3+
pass
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import TypedDict
2+
3+
from . import Solution
4+
5+
6+
class CaseInput(TypedDict):
7+
nums: list[int]
8+
9+
10+
class CaseDict(TypedDict):
11+
input: CaseInput
12+
expected: list[str]
13+
14+
15+
test_cases: list[CaseDict] = [
16+
{
17+
'input': {'nums': [0, 1, 2, 4, 5, 7]},
18+
'expected': ['0->2', '4->5', '7'],
19+
},
20+
{
21+
'input': {'nums': [0, 2, 3, 4, 6, 8, 9]},
22+
'expected': ['0', '2->4', '6', '8->9'],
23+
},
24+
]
25+
26+
27+
def test_solution():
28+
for test_case in test_cases:
29+
assert Solution().summaryRanges(**test_case['input']) == test_case['expected']

0 commit comments

Comments
 (0)