Skip to content

Wrong testcase Rotting Oranges #19429

@danyduran

Description

@danyduran

LeetCode Username

danielisidrocustodioduran

Problem number, title, and link

  1. Rotting Oranges https://leetcode.com/problems/rotting-oranges/description/

Category of the bug

  • Problem description
  • Solved examples
  • Problem constraints
  • Problem hints
  • Incorrect or missing "Related Topics"
  • Incorrect test Case (Output of test case is incorrect as per the problem statement)
  • Missing test Case (Incorrect/Inefficient Code getting accepted because of missing test cases)
  • Editorial
  • Programming language support

Description of the bug

There's a wrong case when the input is [[2,1,1],[0,1,1],[1,0,1]], should be result of 4 instead of the result -1

Language used for code

Python3

Code used for Submit/Run operation

// Paste your code here. Don't remove ``` given
// above and below the code.
from collections import deque


class Solution(object):
    def orangesRotting(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        minutes = -1
        queue = deque()
        visited = set()
        fresh_oranges = set()

        for row in range(len(grid)):
            for col in range(len(grid[row])):
                if grid[row][col] == 2:
                    queue.append((row, col))
                if grid[row][col] == 1:
                    fresh_oranges.add((row, col))
        
        while queue:
            minutes += 1
            for _ in range(len(queue)):
                row, col = queue.popleft()
        
                if (row-1, col,) in fresh_oranges and (row-1, col,) not in visited:
                    queue.append((row-1, col,))
                    visited.add((row-1, col,))
                if (row+1, col,) in fresh_oranges and (row+1, col,) not in visited:
                    queue.append((row+1, col,))
                    visited.add((row+1, col,))
                if (row, col-1) in fresh_oranges and (row, col-1,) not in visited:
                    queue.append((row, col-1,))
                    visited.add((row, col-1,))
                if (row, col+1) in fresh_oranges and (row, col+1,) not in visited:
                    queue.append((row, col+1,))
                    visited.add((row, col+1,))
        return minutes
                

Expected behavior

This problem stands that we need to determine how many minutes are needed for all the fresh oranges get rotten,
for the first step is check the already rotten oranges are inside in the grid,
in this case, we start at the indexes(row, col) 0,0 that are equals to 2(rotten orange), we add up one minute
too we need to check if there is any fresh orange in the side of the rotten orange(0,0) we see there is a fresh orange at the index (0, 1), this is the next rotten orange, so we need to add up one minute more, at the index (0,1) there are two rotten oranges at the indexes (0, 2) and (1, 1) we add up another minute more, the next fresh orange is at the index (1, 2) we need to add up one minute more, the last index is (2, 2) so we need to add up more minute, finally we should get 4 minutes and the current test case says that is -1, that is wrong.

Screenshots

image

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions