Skip to content

Solve 1267. Count Servers that Communicate #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
- name: Set up Go 1.20
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.20
id: go

- name: Check out code into the Go module directory
Expand Down
64 changes: 64 additions & 0 deletions 1267-count_servers_that_communicate/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package countserversthatcommunicate

func countServers(grid [][]int) int {
rows := len(grid)
cols := len(grid[0])

connected := make(map[server]bool, rows*cols)

for y := 0; y <= rows-1; y++ {
temp := make([]server, 0, cols)

for x := 0; x <= cols-1; x++ {
pos := grid[y][x]

if pos == 0 {
continue
}

s := server{
x: x,
y: y,
}

temp = append(temp, s)
}

if len(temp) > 1 {
for _, s := range temp {
connected[s] = true
}
}
}

for x := 0; x <= cols-1; x++ {
temp := make([]server, 0, rows)

for y := 0; y <= rows-1; y++ {
pos := grid[y][x]

if pos == 0 {
continue
}

s := server{
x: x,
y: y,
}

temp = append(temp, s)
}

if len(temp) > 1 {
for _, s := range temp {
connected[s] = true
}
}
}

return len(connected)
}

type server struct {
x, y int
}
71 changes: 71 additions & 0 deletions 1267-count_servers_that_communicate/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package countserversthatcommunicate

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_countServers(t *testing.T) {
type args struct {
grid [][]int
}

tests := []struct {
name string
args args
want int
}{
{
name: "[[1,0],[0,1]] => 0",
args: args{
grid: [][]int{
{1, 0},
{0, 1},
},
},
want: 0,
},
{
name: "[[1,0],[1,1]] => 3",
args: args{
grid: [][]int{
{1, 0},
{1, 1},
},
},
want: 3,
},
{
name: "[[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] => 4",
args: args{
grid: [][]int{
{1, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
},
},
want: 4,
},
{
name: "[[1,0,0,1,0],[0,0,0,0,0],[0,0,0,1,0]] => 3",
args: args{
grid: [][]int{
{1, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
},
},
want: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := countServers(tt.args.grid)

assert.Equal(t, tt.want, got)
})
}
}
46 changes: 46 additions & 0 deletions 1267-count_servers_that_communicate/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 1267. Count Servers that Communicate

You are given a map of a server center, represented as an `m * n` integer matrix `grid`, where 1 means that on that cell
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
on the same column.

Return the number of servers that communicate with any other server.

## Examples

### Example 1

```text
Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.
```


### Example 2

```text
Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.
```


### Example 3

```text
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other.
The two servers in the third column can communicate with each other.
The server at right bottom corner can't communicate with any other server.
```


## Constraints:

- `m == grid.length`
- `n == grid[i].length`
- `1 <= m <= 250`
- `1 <= n <= 250`
- `grid[i][j] == 0 or 1`
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/oleg-balunenko/leetcode_problems

go 1.13
go 1.20

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.8.2

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading