Skip to content

Commit 35bedb1

Browse files
authored
Solve 1267. Count Servers that Communicate (#2)
* chore: Bump go version to 1.20 * feat: Solve `1267. Count Servers that Communicate`
1 parent 9306af1 commit 35bedb1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+19611
-9
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99

10-
- name: Set up Go 1.13
10+
- name: Set up Go 1.20
1111
uses: actions/setup-go@v1
1212
with:
13-
go-version: 1.13
13+
go-version: 1.20
1414
id: go
1515

1616
- name: Check out code into the Go module directory
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package countserversthatcommunicate
2+
3+
func countServers(grid [][]int) int {
4+
rows := len(grid)
5+
cols := len(grid[0])
6+
7+
connected := make(map[server]bool, rows*cols)
8+
9+
for y := 0; y <= rows-1; y++ {
10+
temp := make([]server, 0, cols)
11+
12+
for x := 0; x <= cols-1; x++ {
13+
pos := grid[y][x]
14+
15+
if pos == 0 {
16+
continue
17+
}
18+
19+
s := server{
20+
x: x,
21+
y: y,
22+
}
23+
24+
temp = append(temp, s)
25+
}
26+
27+
if len(temp) > 1 {
28+
for _, s := range temp {
29+
connected[s] = true
30+
}
31+
}
32+
}
33+
34+
for x := 0; x <= cols-1; x++ {
35+
temp := make([]server, 0, rows)
36+
37+
for y := 0; y <= rows-1; y++ {
38+
pos := grid[y][x]
39+
40+
if pos == 0 {
41+
continue
42+
}
43+
44+
s := server{
45+
x: x,
46+
y: y,
47+
}
48+
49+
temp = append(temp, s)
50+
}
51+
52+
if len(temp) > 1 {
53+
for _, s := range temp {
54+
connected[s] = true
55+
}
56+
}
57+
}
58+
59+
return len(connected)
60+
}
61+
62+
type server struct {
63+
x, y int
64+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package countserversthatcommunicate
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_countServers(t *testing.T) {
10+
type args struct {
11+
grid [][]int
12+
}
13+
14+
tests := []struct {
15+
name string
16+
args args
17+
want int
18+
}{
19+
{
20+
name: "[[1,0],[0,1]] => 0",
21+
args: args{
22+
grid: [][]int{
23+
{1, 0},
24+
{0, 1},
25+
},
26+
},
27+
want: 0,
28+
},
29+
{
30+
name: "[[1,0],[1,1]] => 3",
31+
args: args{
32+
grid: [][]int{
33+
{1, 0},
34+
{1, 1},
35+
},
36+
},
37+
want: 3,
38+
},
39+
{
40+
name: "[[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] => 4",
41+
args: args{
42+
grid: [][]int{
43+
{1, 1, 0, 0},
44+
{0, 0, 1, 0},
45+
{0, 0, 1, 0},
46+
{0, 0, 0, 1},
47+
},
48+
},
49+
want: 4,
50+
},
51+
{
52+
name: "[[1,0,0,1,0],[0,0,0,0,0],[0,0,0,1,0]] => 3",
53+
args: args{
54+
grid: [][]int{
55+
{1, 0, 0, 1, 0},
56+
{0, 0, 0, 0, 0},
57+
{0, 0, 0, 1, 0},
58+
},
59+
},
60+
want: 3,
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
got := countServers(tt.args.grid)
67+
68+
assert.Equal(t, tt.want, got)
69+
})
70+
}
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 1267. Count Servers that Communicate
2+
3+
You are given a map of a server center, represented as an `m * n` integer matrix `grid`, where 1 means that on that cell
4+
there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or
5+
on the same column.
6+
7+
Return the number of servers that communicate with any other server.
8+
9+
## Examples
10+
11+
### Example 1
12+
13+
```text
14+
Input: grid = [[1,0],[0,1]]
15+
Output: 0
16+
Explanation: No servers can communicate with others.
17+
```
18+
19+
20+
### Example 2
21+
22+
```text
23+
Input: grid = [[1,0],[1,1]]
24+
Output: 3
25+
Explanation: All three servers can communicate with at least one other server.
26+
```
27+
28+
29+
### Example 3
30+
31+
```text
32+
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
33+
Output: 4
34+
Explanation: The two servers in the first row can communicate with each other.
35+
The two servers in the third column can communicate with each other.
36+
The server at right bottom corner can't communicate with any other server.
37+
```
38+
39+
40+
## Constraints:
41+
42+
- `m == grid.length`
43+
- `n == grid[i].length`
44+
- `1 <= m <= 250`
45+
- `1 <= n <= 250`
46+
- `grid[i][j] == 0 or 1`

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module github.com/oleg-balunenko/leetcode_problems
22

3-
go 1.13
3+
go 1.20
44

5-
require github.com/stretchr/testify v1.4.0
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
21
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
45
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
56
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
7-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
813
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
914
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
11-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)