GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Other/river_sizes.py at master · Pranshu-Raj/coding-problems

Expand file tree

/

river_sizes.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
94 lines (72 loc) · 2.07 KB
/

river_sizes.py

Copy path

File metadata and controls

94 lines (72 loc) · 2.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
River Sizes
You are given a two-dimensional array (matrix) of potentially unequal height and width containing only 0s and 1s.
Each 0 represents land, and each 1 represents part of a river. A river consists of any number of 1s that are
either horizontally or vertically adjacent (but not diagonally adjacent).
The number of adjacent 1s forming a river determine its size.
Write a function that returns an array of the sizes of all rivers represented in the input matrix.
Note that these sizes do not need to be in any particular order.
Input:
[
[1, 0, 0, 1],
[1, 0, 1, 0],
[0, 0, 1, 0],
[1, 0, 1, 0]
]
Output: [2, 1, 3, 1]
=========================================
This problem can be solved using DFS or BFS.
If 1 is found, find all horizontal or vertical neighbours (1s), and mark them as 0.
Time Complexity: O(N*M)
Space Complexity: O(N*M) , because of recursion calls stack
'''
############
# Solution #
############
def river_sizes(matrix):
n = len(matrix)
m = len(matrix[0])
results = []
for i in range(n):
for j in range(m):
if matrix[i][j] != 0:
# find the river size
size = dfs((i, j), matrix)
# save the river size
results.append(size)
return results
def dfs(coord, matrix):
(i, j) = coord
if i < 0 or j < 0:
# invalid position
return 0
n = len(matrix)
m = len(matrix[0])
if i == n or j == m:
# invalid position
return 0
if matrix[i][j] == 0:
# not a river
return 0
# update the matrix, the matrix is passed by reference
matrix[i][j] = 0
# this position is part of river
size = 1
# directions: down, left, up, right
dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
# check all 4 directions
for d in dirs:
size += dfs((i + d[0], j + d[1]), matrix)
return size
###########
# Testing #
###########
# Test 1
# Correct result => [2, 1, 3, 1]
matrix = [
[1, 0, 0, 1],
[1, 0, 1, 0],
[0, 0, 1, 0],
[1, 0, 1, 0]
]
print(river_sizes(matrix))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.