2023-05-09 Daily Challenge

Today I have done leetcode's May LeetCoding Challenge with cpp.

May LeetCoding Challenge 9

Description

Spiral Matrix

Given an m x n matrix, return all elements of the matrix in spiral order.

 

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

 

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Solution

class Solution {
  int moves[4];
  vector<bool> visit;
  int cols;
  int total;
  
  void init() {
    moves[0] = 1;
    moves[1] = cols;
    moves[2] = -1;
    moves[3] = -cols;
  }
  
  bool check(int pos, int direction) {
    return (direction == 0 && !((pos+1) % cols)) || \
            (pos + moves[direction] >= total)    || \
            (direction == 2 && !(pos % cols))    || \
            (pos + moves[direction] < 0)         || \
            visit[pos+moves[direction]]; 
  }
public:
  vector<int> spiralOrder(vector<vector<int>>& matrix) {
    cols = matrix.front().size();
    total = cols * matrix.size();
    init();
    visit.resize(total);
    vector<int> answer;
    int direction = 0, pos = 0;
    while(answer.size() < total) {
      answer.push_back(matrix[pos/cols][pos%cols]);
      visit[pos] = true;
      int turns = 0;
      while(turns < 4 && check(pos, direction)) {
          turns += 1;
          direction = (direction + 1) % 4;
      }
      pos += moves[direction];
    }
    return move(answer);
  }
};

// Accepted
// 23/23 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 72.32 % of cpp submissions (6.8 MB)