Easily understandable code || Java || Beats 96.96 || Greedy approach
Approach\n    Greedy\n# Complexity\n- Time complexity:\n    O(RowsCols)\n\n- Space complexity:\n    O(RowsCols)\n\n# Code\n\nclass Solution {\n    public int[][
# Approach\n    Greedy\n# Complexity\n- Time complexity:\n    O(Rows*Cols)\n\n- Space complexity:\n    O(Rows*Cols)\n\n# Code\n```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int count=0,i=rStart,j=cStart;\n        int[][] res=new int[cols*rows][2];\n        int r=0,c=0;\n        int steps=1;\n        if(i<rows && i>=0 && j<cols && j>=0){\n            res[r][c]=i;\n            c++;\n            res[r][c]=j;\n            r++;\n            count++;\n            c=0;\n        }\n        while(count<(rows*cols)){\n            for(int k=0;k<steps;k++){\n                j++;\n                if(i<rows && i>=0 && j<cols && j>=0){\n                    res[r][c]=i;\n                    c++;\n                    res[r][c]=j;\n                    r++;\n                    c=0;\n                    count++;\n                }\n            }\n            for(int k=0;k<steps;k++){\n                i++;\n                if(i<rows && i>=0 && j<cols && j>=0){\n                    res[r][c]=i;\n                    c++;\n                    res[r][c]=j;\n                    r++;\n                    c=0;\n                    count++;\n                }\n            }\n            steps++;\n            for(int k=0;k<steps;k++){\n                j--;\n                if(i<rows && i>=0 && j<cols && j>=0){\n                    res[r][c]=i;\n                    c++;\n                    res[r][c]=j;\n                    r++;\n                    c=0;\n                    count++;\n                }\n            }            \n            for(int k=0;k<steps;k++){\n                i--;\n                if(i<rows && i>=0 && j<cols && j>=0){\n                    res[r][c]=i;\n                    c++;\n                    res[r][c]=j;\n                    r++;\n                    c=0;\n                    count++;\n                }\n            }\n            steps++;\n        }\n        return res;\n    }\n}\n```\n\n\n\n\n
['Java']