선입 후출 (LIFO)
마지막에 들어온 데이터가 먼저 나가는 자료구조
데이터 추가: push
데이터 삭제: pop
class Node {
constructor(data) {
this.data = data;
this.prev = null;
}
}
class Stack {
constructor() {
this.top = null;
this.length = 0;
}
push(data) {
const newNode = new Node(data);
if (this.length === 0) {
this.top = newNode;
} else {
const preNode = this.top;
this.top = newNode;
this.top.prev = preNode;
}
this.length++;
}
peek() {
this.length && console.log(this.top.data);
}
pop() {
if (this.length === 0) {
console.error("stack is empty");
} else {
this.top = this.top.prev;
}
this.length--;
}
}
const stack = new Stack();
stack.push(1);
stack.peek();
stack.push(2);
stack.peek();
stack.push(3);
stack.peek();
stack.pop();
stack.pop();
stack.pop();
stack.peek();
프로세스
실행중인 프로그램
스레드
프로세스의 실행 단위
프로세스의 메모리 영역은 4부분으로 나뉜다.
프로세스 내부의 스레드는 code, data, heap 영역을 공유하고 stack 영역을 독립적으로 갖는다.
stack을 통해 각 스레드가 독립적인 실행 흐름을 가능하게 한다.
스레드간의 텍스트 영역 공유를 통해 함수 호출을 가능하게 한다.
스레드간의 데이터, 힙 영역 공유를 통해 메로리 공간을 공유하며 스레드간 통신을 가능하게 만든다.
선입 후출 (FIFO)
처음으로 들어온 데이터가 먼저 나가는 자료구조
데이터 추가: push
데이터 삭제: shift
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.top = null;
this.bottom = null;
this.length = 0;
}
push(data) {
const newNode = new Node(data);
if (this.length === 0) {
this.top = newNode;
this.bottom = newNode;
} else {
const preNode = this.top;
this.top = newNode;
preNode.next = newNode;
}
this.length++;
}
peek() {
this.length > 0 && console.log(this.top.data);
}
shift() {
if (this.length === 0) {
console.error("stack is empty");
} else {
this.bottom = this.bottom.next;
}
this.length--;
}
}
const queue = new Queue();
queue.push(1);
queue.peek();
queue.push(2);
queue.peek();
queue.push(3);
queue.peek();
queue.shift();
queue.shift();
queue.shift();
queue.shift();
queue.peek();
CPU는 프로세스를 관리하며, CPU를 각 프로세스에게 적절히 할당하는 임무를 가지고 있다.
한정된 메모리를 여러 프로세스가 효울적으로 사용할 수 있도록 다음 실행 시간에 실행할 수 있는 프로세스를 선택할 필요가 있는데 이는 스케줄링 기법을 통해 선택한다.
https://programmers.co.kr/learn/courses/30/lessons/77485
직사각형 테두리에 있는 숫자들을 따로 저장시킨 뒤, 맨 앞의 수를 뒤로 보낸 후, 다시 배열에 넣어주면 될 것 같다.
const arr = Array.from(Array(rows), (_,i) => Array.from(Array(columns),(_,j)=>i*columns+j+1))
const stack = [];
for(let i=y1;i<y2;i++) stack.push(arr[x1][i]);
for(let i=x1;i<x2;i++) stack.push(arr[i][y2]);
for(let i=y2;i>y1;i--) stack.push(arr[x2][i]);
for(let i=x2;i>x1;i--) stack.push(arr[i][y1]);
answer.push(Math.min(...stack))
stack.unshift(stack.pop())
for(let i=y1;i<y2;i++) arr[x1][i] = stack.shift();
for(let i=x1;i<x2;i++) arr[i][y2] = stack.shift();
for(let i=y2;i>y1;i--) arr[x2][i] = stack.shift();
for(let i=x2;i>x1;i--) arr[i][y1] = stack.shift();
function solution(rows, columns, queries) {
const answer = []
const arr = Array.from(Array(rows), (_,i) => Array.from(Array(columns),(_,j)=>i*columns+j+1))
console.log(arr)
queries.map(query=>{
const [x1,y1,x2,y2] = [query[0]-1,query[1]-1,query[2]-1,query[3]-1]
const stack = [];
for(let i=y1;i<y2;i++) stack.push(arr[x1][i]);
for(let i=x1;i<x2;i++) stack.push(arr[i][y2]);
for(let i=y2;i>y1;i--) stack.push(arr[x2][i]);
for(let i=x2;i>x1;i--) stack.push(arr[i][y1]);
answer.push(Math.min(...stack))
stack.unshift(stack.pop())
for(let i=y1;i<y2;i++) arr[x1][i] = stack.shift();
for(let i=x1;i<x2;i++) arr[i][y2] = stack.shift();
for(let i=y2;i>y1;i--) arr[x2][i] = stack.shift();
for(let i=x2;i>x1;i--) arr[i][y1] = stack.shift();
})
return answer;
}