Treasure Hunt Grid
Function Name: findTreasure
Description
Write a function named 'findTreasure' that navigates through a grid to find a treasure. The grid is represented as a two-dimensional array, where each cell contains a clue to the next cell's position. Each clue is a string in the format 'x,y', indicating the row and column indices of the next cell to check. The treasure is found when a cell points to itself. The function should return the path taken as a list of coordinates (each coordinate being a pair of integers), starting from the top-left cell (0,0). If the treasure cannot be found after visiting all cells, the function should return an empty list.
Requirements
- The function 'findTreasure' must take one parameter: a two-dimensional array (grid).
- Each cell in the grid will contain a string with two numbers separated by a comma 'row,column'.
- If the treasure is found, return a list of coordinate pairs; otherwise, return an empty list.
- Do not use a cell as the next step in the path if it has already been visited to avoid infinite loops.
- Assume the grid is non-empty and has at least one row and one column.
Examples
Let grid = [['1,0', '2,2'], ['2,1', '2,2'], ['2,3', '2,2']];findTreasure(grid); // Should return [[0,0], [1,0], [2,1], [2,2]] since the treasure is in cell [2,2] and it points to itself.
Links
Path Sum in Binary Tree
Wed Nov 20 2024
In this challenge, you are given the root of a binary tree and an integer target sum. Write a function that checks if the tree has a root-to-leaf path such that adding up all the values along the path equals the target sum.A leaf is a node with no children.Your function should return true if such a path is found, or false otherwise.
Matrix Spiral Copy
Fri Nov 22 2024
Write a function that receives a two-dimensional array (matrix) of integers and returns a list of the matrix's elements in spiral order. Spiral order should start from the top-left corner and progress clockwise until all elements have been visited.