Description
Solutions
Count Idle Warehouse Robots
πŸ‘©β€πŸŽ“ NEW GRAD

The Banana Company employs small, autonomous transport units, known as "Bananaer", to efficiently carry large stacks of products within its warehouses. These Bananaer navigate along predefined paths in a warehouse, which can be represented as a Cartesian plane. Each Bananaer is stationed at distinct integral coordinate points of the form (x, y).

When a product needs to be delivered to a specific location (i, j), the system selects the nearest available Bananaer to complete the task. However, some Bananaers may seldom be chosen due to being surrounded by other units.

A Bananaer is considered idle if it has another Bananaer positioned directly above, below, to the left, and to the right of it. This means it is entirely enclosed and less likely to be selected for work. It is guaranteed that no two Bananaers share the same coordinates.

Given the locations of n Bananaers on the Cartesian plane, determine how many Bananers are idle based on the above criteria.

Example 1:

Input:  x = [0, 0, 0, 0, 0, 1, 1, 1, 2, -1, -1, -2, -1], y = [-1, 0, 1, 2, -2, 0, 1, -1, 0, 1, -1, 0, 0]
Output: 5
Explanation:
The cartesian plane with idle robots marked red can be represented as: The robots at locations (0, 0), (1, 0), (-1, -0), (0, 1), and (0, -1) are idle as they are surrounded by robots on all 4 sides.

Example 2:

Input:  x = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3], y = [1, 2, 3, 1, 2, 3, 5, 1, 2, 3]
Output: 2
Explanation:
The 10 robots are arranged as follows:
  • The robot at (2, 2) is idle because it has robots at (1, 2), (3, 2), (2, 3), and (2, 1) directly to the left, right, up, and down respectively.
  • The robot at (2, 3) is idle because it has robots at (1, 3), (3, 3), (2, 5), and (2, 2) directly to the left, right, up, and down respectively.
  • There are 2 idle robots in this arrangement.
    Constraints:
      • 1 ≀ n ≀ 105
      • -109 ≀ x[i], y[i] ≀ 109
    Thumbnail 0
    Thumbnail 1
    Testcase

    Result
    Case 1

    input:

    output: