표편집

  1. 접근방법
    • 이동 구현: “현재 위치 2에서 삭제되지 않은 행 2개 위로 가기”
    • 삭제 구현: 현재 행을 삭제하고, 다음 선택할 행 찾기
    • 복구 구현: 가장 최근 삭제된 행을 기억해뒀다가 복구
    • 결과 구현:
  2. 어려웠던 점
    • 현재 선택된 행의 위치를 어떻게 추적할까?
    • 삭제가 일어나면 행 번호가 바뀌는데, 이를 어떻게 처리할지
    • 삭제된 행을 카운트 하지 않기
      • 먼저 아래쪽에서 삭제되지 않은 행을 찾기
      • 없으면 위쪽에서 삭제되지 않은 행을 찾기
        • “Z” 명령어(복구)를 위해 뭘 저장해둬야 할까?
    • 가장 최근에 삭제된 행부터 복구해야 하니까…
    • “가장 최근에 삭제된 것부터 복구” = 나중에 들어온 것부터 나간다 (LIFO)

      [!NOTE] 핵심 개념: 스택(Stack) 구조💡 삭제 순서: 3 → 5 → 1 deletedRows = [3, 5, 1]

      첫 번째 Z: deletedRows.pop() → 1번 행 복구 → deletedRows = [3, 5]
      두 번째 Z: deletedRows.pop() → 5번 행 복구 → deletedRows = [3]
      세 번째 Z: deletedRows.pop() → 3번 행 복구 → deletedRows = []
      - **최종 결과 문자열을 만들 때 뭘 기준으로 "O"와 "X"를 결정할까?**
      - **문제 분석이 제일 먼저**
      
    • 어처구니 없게도 새로운 행이 추가 되었을 때는 어떻게 하지? 하고 시간을 많이 소모함(문제에는 그런 조건이 없었음)
    • “표의 범위(0행 ~ 마지막 행)를 벗어날 수 없습니다” “정답은 표의 0행부터 n - 1행까지에 해당되는 O, X를 순서대로…” 이 조건도 간과함. ``` 문제상 행 번호: 0행 1행 2행 3행 4행 5행 6행 7행 배열 인덱스: [0] [1] [2] [3] [4] [5] [6] [7]
    • ```
  3. 새롭게 알게된 점

    ```jsx

let count = 0; let current = k;

while (count < X){ current –; if(rows[current]=== “0”){ count++; } }

k = current;


#### 🔄 **이동 구현 (U X, D X)**
```jsx
// U X: 위로 X칸 이동 
if(command[0] === "U") { 
	let X = parseInt(command.split(" ")[1]); 
	let count = 0; let current = k; 
	while(count < X) { 
		current--; if(rows[current] === "O") { 
		// 삭제되지 않은 행만 카운트 
		count++; } 
	} 
	k = current; 
} 
	
// D X: 아래로 X칸 이동 
if(command[0] === "D") { 
	let X = parseInt(command.split(" ")[1]); 
	let count = 0; 
	let current = k; 
	while(count < X) { 
		current++; if(rows[current] === "O") { 
		// 삭제되지 않은 행만 카운트 
		count++; 
		} 
	} 
	k = current; 
}

삭제 구현 (C)

// C: 현재 행 삭제

if(command === "C") {

	// 1. 현재 행을 삭제 표시
	rows[k] = "X";
	// 2. 복구를 위해 삭제된 행 번호 저장 (나중에 Z에서 사용)
	deletedRows.push(k);
	
	// 3. 다음 선택할 행 찾기
	let newPosition = k + 1;
	
	// 3-1: 아래쪽에서 삭제되지 않은 행 찾기
	while(newPosition < n && rows[newPosition] === "X") {
		
		newPosition++;
	}

	// 3-2: 아래쪽에 없으면 위쪽에서 찾기
	if(newPosition < n) {
		k = newPosition; // 아래 행으로 이동
	} else {
	
		newPosition = k - 1;
		
		while(newPosition >= 0 && rows[newPosition] === "X") {
			newPosition--;
		}
	k = newPosition; // 위쪽 행으로 이동
	}

}

↩️ 복구 구현 (Z)

if(command === "Z") { 
	// 1. 가장 최근에 삭제된 행 번호 가져오기 
	let restoredRow = deletedRows.pop(); // 마지막 원소 꺼내기 
	// 2. 해당 행을 복구 
	rows[restoredRow] = "O"; 
	// 3. 현재 선택된 행(k)은 바뀌지 않음! 
}

핵심 개념: 스택(Stack) 구조

삭제 순서: 3 → 5 → 1
deletedRows = [3, 5, 1]

첫 번째 Z: deletedRows.pop() → 1번 행 복구 → deletedRows = [3, 5]
두 번째 Z: deletedRows.pop() → 5번 행 복구 → deletedRows = [3]
세 번째 Z: deletedRows.pop() → 3번 행 복구 → deletedRows = []

최종구현

function solution(n, k, cmd) { 

	let rows = new Array(n).fill("O"); 
	let deletedRows = []; 

	for(let i = 0; i < cmd.length; i++) {
		let command = cmd[i]; 
		if(command[0] === "U") { 
			let X = parseInt(command.split(" ")[1]); 
			let count = 0; let current = k; 
			while(count < X) { 
				current--; if(rows[current] === "O") { 
				// 삭제되지 않은 행만 카운트 
				count++; } 
			} 
			k = current;  
		} else if(command[0] === "D") { 
			let X = parseInt(command.split(" ")[1]); 
			let count = 0; 
			let current = k; 
			while(count < X) { 
				current++; if(rows[current] === "O") { 
				// 삭제되지 않은 행만 카운트 
				count++; 
				} 
			} 
			k = current; 
		} else if(command === "C") { 

			rows[k] = "X";
			deletedRows.push(k);

			let newPosition = k + 1;
			while(newPosition < n && rows[newPosition] === "X") {
				
				newPosition++;
			}

			if(newPosition < n) {
			
				k = newPosition; // 아래 행으로 이동
				
			} else {
				newPosition = k - 1;
				while(newPosition >= 0 && rows[newPosition] === "X") {
					newPosition--;
				}
				k = newPosition; // 위쪽 행으로 이동
			}
		} else if(command === "Z") { 
			let restoredRow = deletedRows.pop(); // 마지막 원소 꺼내기 
			rows[restoredRow] = "O";  
		} 
	} 
	// 3. 결과 문자열 생성 
	let result = ""; 
	for(let i = 0; i < n; i++) { 
		result += rows[i]; 
		// "O" 또는 "X" 추가 
		} 
	return result; 
}

This line appears after every note.

Notes mentioning this note

There are no notes linking to this note.


Here are all the notes in this garden, along with their links, visualized as a graph.