본문 바로가기
코딩테스트

C++ ] leetCode 2120 - Execution of All Suffix Instructions Staying in a Grid

by eteo 2024. 3. 12.

 

 

리트코드 2120 문제

 

There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol).

You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).

The robot can begin executing from any ith instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met:

 

The next instruction will move the robot off the grid.

There are no more instructions left to execute.

 

Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the ith instruction in s.

 

 

Example 1: 

  • Input: n = 3, startPos = [0,1], s = "RRDDLU" 
  • Output: [1,5,4,3,1,0] 
  • Explanation: Starting from startPos and beginning execution from the ith instruction:

- 0th: "RRDDLU". Only one instruction "R" can be executed before it moves off the grid.

- 1st:  "RDDLU". All five instructions can be executed while it stays in the grid and ends at (1, 1).

- 2nd:   "DDLU". All four instructions can be executed while it stays in the grid and ends at (1, 0).

- 3rd:    "DLU". All three instructions can be executed while it stays in the grid and ends at (0, 0).

- 4th:     "LU". Only one instruction "L" can be executed before it moves off the grid.

- 5th:      "U". If moving up, it would move off the grid.

 

 

Example 2:

  • Input: n = 2, startPos = [1,1], s = "LURD"
  • Output: [4,1,0,0]
  • Explanation: - 0th: "LURD".

- 1st:  "URD".

- 2nd:   "RD".

- 3rd:    "D".

 

 

Example 3:

  • Input: n = 1, startPos = [0,0], s = "LRUD"
  • Output: [0,0,0,0]
  • Explanation: No matter which instruction the robot begins execution from, it would move off the grid.

 

 

Constraints:

  • m == s.length
  • 1 <= n, m <= 500
  • startPos.length == 2
  • 0 <= startrow, startcol < n
  • s consists of 'L', 'R', 'U', and 'D'.

 

 

먼저 s.length() 사이즈의 리턴할 벡터를 만들고, s.length()만큼 반복하면서 리턴 벡터의 i번째 요소를 업데이트한다.

 

i번째 검사에서는 s[i]부터 s[s.length() - 1]까지의 instruction을 수행하면서 grid를 벗어나지 않고 수행 가능한 횟수를 count하면 되는데 count의 최소값은 0이고 최대값은 s.lengh() - i가 된다.

 

startPos의 값을 변경시키지 않기 위해 임시 변수에 대입해 놓고 s에서 등장하는 instruction에 따라 행 또는 열을 증가 또는 감소한 뒤 행열의 위치값이 0보다 작거나 n보다 크거나 같으면 grid를 벗어난다고 판단할 수 있다. 

 

 

class Solution {
public:
    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
        vector<int> ret(s.length(), 0);

        for(int i = 0; i < s.length(); i++) {
            int cnt = 0;
            int row = startPos[0];
            int col = startPos[1];
            for(int j = i; j < s.length(); j++) {
                switch(s[j]) {
                    case 'U':
                    row--;
                    break;
                    case 'D':
                    row++;
                    break;
                    case 'L':
                    col--;
                    break;
                    case 'R':
                    col++;
                    break;
                }
                if(row < 0 || row >= n || col < 0 || col >= n) break;
                cnt++;
            }
            ret[i] = cnt;            
        }
        

        return ret;
    }
};