Robot Room Cleaner
Function Name: cleanRoom
Description
Imagine a robot sitting on the upper left corner of a grid with rows and columns. The grid is filled with dirt (represented by a '1'), and clean tiles are represented by a '0'. The robot can move up, down, left, or right, and it can clean the tile it's on. The function 'cleanRoom' should simulate the cleaning process. It takes a 2D array representing the room's condition, a start position for the robot, and returns the number of tiles cleaned by the robot. It cleans each dirty tile only once, regardless of the path taken.
Requirements
- Your function shall take a 2D array 'room', and an array 'startPos' indicating the starting position [row, col] of the robot within the room.
- The robot is only allowed to move horizontally or vertically and cannot move off the edge of the grid.
- The robot should be able to navigate to every reachable dirty tile and clean it, without cleaning the same spot more than once.
- Return the total number of spots cleaned by the robot.
- Consider any position within the grid as reachable if it is dirty ('1').
- The 2D array will only contain integers '0' or '1', where '0' represents a clean spot, and '1' represents dirt.
- The function should be efficient in terms of space and time complexity.
Examples
cleanRoom([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [0, 0]) returns 3cleanRoom([[1, 1, 0], [0, 0, 1], [1, 0, 0]], [1, 2]) returns 4
Links
LinkedList Cycle Detector
Tue Oct 22 2024
Write a function `hasCycle` in a language of your choice that takes a linked list as an input and returns a boolean indicating whether the linked list contains a cycle.A linked list is said to contain a cycle if any node is visited more than once while traversing the list.To solve this problem, you should detect if the linked list has a cycle without using extra space (e.g., an additional data structure like a hash table).Assume that the linked list is a singly linked list provided with the next pointer.Your function should be able to return false if the input is an empty list or a single-node list without a cycle, and true if a cycle is detected.
Array Quadruplet
Thu Oct 24 2024
Write a function called 'findQuadruplet' that finds a quadruplet of numbers in a given array that sum up to a given target sum. A quadruplet is a group of four numbers that are part of the array. The function should return an array of these four numbers in any order. If there is no such quadruplet present in the array, the function should return an empty array.