/*
This is an implementation that demonstrates
how to efficiently spiral traverse a two
dimensional array. In particular, the program
acts on an M*N 2-D array, returning
its elements in spiral order.
Let M be the number of rows of 2-D array and
N be its number of columns.
Time complexity: O(M*N)
Space complexity: O(1)
*/
import java.util.List;
import java.util.ArrayList;
public class LargestRectangleArea {
public static void main(String[] args) {
int[][] twoDArray = {
{ 1, 2, 3, 4 },
{ 12, 13, 14, 5 },
{ 11, 16, 15, 6 },
{ 10, 9, 8, 7 }
};
// Below prints:
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
System.out.println(spiralOrder(twoDArray));
}
private static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int rows = matrix.length;
int columns = matrix[0].length;
int up = 0;
int left = 0;
int right = columns - 1;
int down = rows - 1;
while (result.size() < rows * columns) {
// Left to right direction.
for (int col = left; col <= right; col++) {
result.add(matrix[up][col]);
}
// Top down direction
for (int row = up + 1; row <= down; row++) {
result.add(matrix[row][right]);
}
// Make sure we are on a different row
if (up != down) {
// Right to left direction
for (int col = right - 1; col >= left; col--) {
result.add(matrix[down][col]);
}
}
// Make sure we are on a different column
if (left != right) {
// Bottom up direction
for (int row = down - 1; row > up; row--) {
result.add(matrix[row][left]);
}
}
left++;
right--;
up++;
down--;
}
return result;
}
}
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
cout<<n<<" "<<m<<endl;
vector<int> v(m, -1);
vector<vector<int>> visitedmatrix(n, v);
vector<int> result;
int pos = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << matrix[i][j] << " ";
}
cout << "
";
}
cout << "--------------------"
<< "
";
int top = 0, right = m - 1, left = 0, down = n - 1;
int count = n * m;
while (count > 0)
{
// move right
for (int i = top; i <= right; i++)
{
cout<<matrix[top][i]<<" "<<top<<i<<" "<<right<<endl;
if (visitedmatrix[top][i] == -1)
{
// cout << matrix[top][i] << " ";
result.insert(result.begin() + pos ,matrix[top][i]);
visitedmatrix[top][i] = 0;
count--;pos++;
}
}
// move down
for (int i = top; i <= down; i++)
{
if (visitedmatrix[i][right] == -1)
{
cout << matrix[i][right] << " ";
result.insert(result.begin() + pos,matrix[i][right]);
visitedmatrix[i][right] = 0;
count--;pos++;
}
}
// move left
for (int i = right; i >= left; i--)
{
if (visitedmatrix[down][i] == -1)
{
cout << matrix[down][i] << " ";
result.insert(result.begin() + pos,matrix[down][i]);
visitedmatrix[down][i] = 0;
count--;pos++;
}
}
// move up
for (int i = down; i >= top; i--)
{
if (visitedmatrix[i][top] == -1)
{
cout << matrix[i][top] << " ";
result.insert(result.begin() + pos,matrix[i][top] );
visitedmatrix[i][top] = 0;
count--;pos++;
}
}
top++;
right--;
down--;
left++;
}
cout << "--------------------------"
<< "
";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << visitedmatrix[i][j] << " ";
}
cout << "
";
}
return result;
}
};