Page Contents

**Grid Connections Amazon OA Solution**

A supply chain manager at Amazon Logistics wants to determine the number of connections between warehouses, represented as nodes on a grid. A grid with m rows and n columns is used to form a cluster of nodes. If a point in the grid has a value of 1, then it represents a node.

Each node in the cluster has a level associated with it. A node located in the ith row of the grid is a level i node.

Here are the rules for creating a cluster:

- Every node at a level connects to the next level that contains at least 1 node (i.e., every node at level i connects to all the nodes at level k where k > i and k is the first level after level i than contains at least one note).
- When i reaches the last level in the grid, no more connections are possible.

Given such a grid, please help the supply chain manager by finding the number of connections present in the cluster.

**Input**

- grid: the nodes grid

**Output**

the total number of connections

Examples

Example 1:

**Input**: 1grid = [[1, 1, 1], [0, 1, 0], [0, 0, 0], [1, 1, 0]]

**Output**: 5

**Explanation**:

There are a total of 3+2=5 connections.

**SOLUTION**

**Program:** ** Grid Connections Solution** in Python

```
def gridOfNodes(self, intervals: list[list[int]]) -> int:
connections = 0
prevNodes = 1
# print(len(intervals[0]))
for i in range(len(intervals)):
currNodeCount = 0
for j in range(len(intervals[0])):
if(intervals[i][j] == 1):
currNodeCount+= 1
if i == 0:
prevNodes = currNodeCount
continue
elif currNodeCount >= 1:
connections += currNodeCount * prevNodes
prevNodes = currNodeCount
return connections
```

**Program:** ** Grid Connections Amazon OA Solution** in Python

```
import collections
class Solution:
def gridOfNodes(self, intervals: list[list[int]]) -> int:
connections = 0
prevNodes = intervals[0].count(1)
for interval in intervals[1:]:
currNodeCount = interval.count(1)
if currNodeCount >= 1:
connections += currNodeCount * prevNodes
prevNodes = currNodeCount
return connections
```

*Related: Amazon OA Online Assessment 2021 Questions and Answers*