Skip to content

Commit 2e7bf81

Browse files
authored
[Bona1122] 25.01.09 (#17)
* change folder name: Array -> [week1]Array * Claw machine / 중급 * Valid brackets / 중급 * Rotating brackets / 심화 * Parcel box / 심화 * Stock price / 심화
1 parent 3b38aa3 commit 2e7bf81

File tree

10 files changed

+124
-0
lines changed

10 files changed

+124
-0
lines changed

bona1122/Array/Making_stars_at_intersection_points.js renamed to bona1122/[week1]Array/Making_stars_at_intersection_points.js

File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const solution = (boards, moves) => {
2+
let stack = [];
3+
let removedCnt = 0;
4+
for (let i = 0; i < moves.length; i++) {
5+
let pickedToy = -1;
6+
let col = moves[i] - 1; // 격자의 열 선택(크레인 위치)
7+
for (let row = 0; row < boards.length; row++) {
8+
// 아래로 크레인 내리기
9+
if (boards[row][col] !== 0) {
10+
pickedToy = boards[row][col];
11+
boards[row][col] = 0;
12+
break;
13+
}
14+
}
15+
if (pickedToy === -1) continue; // 인형이 집히지 않은 경우, 아래 스킵
16+
if (pickedToy === stack[stack.length - 1]) {
17+
stack.pop();
18+
removedCnt += 2;
19+
} else {
20+
stack.push(pickedToy);
21+
}
22+
}
23+
return removedCnt;
24+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function solution(order) {
2+
const stack = [];
3+
let idx = 0; // 처리해야되는 order의 인덱스
4+
let cnt = 0;
5+
6+
for (let currentBox = 1; currentBox <= order.length; currentBox++) {
7+
if (currentBox === order[idx]) {
8+
// 처리가능하면 처리하고, 스택도 비울만큼 비우기
9+
cnt++;
10+
idx++;
11+
while (stack.length > 0 && stack.at(-1) === order[idx]) {
12+
stack.pop();
13+
cnt++;
14+
idx++;
15+
}
16+
} else {
17+
// 처리못하면 스택에 보관
18+
stack.push(currentBox);
19+
}
20+
}
21+
22+
while (stack.length > 0 && stack.at(-1) === order[idx]) {
23+
// 스택에 남은 것들 처리하기
24+
stack.pop();
25+
cnt++;
26+
idx++;
27+
}
28+
29+
return cnt;
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(s) {
2+
let result = 0;
3+
const map = { ")": "(", "}": "{", "]": "[" };
4+
const arr = [...s];
5+
6+
const isValid = (arr) => {
7+
const stack = [];
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
const item = arr[i];
11+
const mappingItem = map[item];
12+
if (mappingItem) {
13+
if (mappingItem === stack.at(-1)) stack.pop();
14+
else return false;
15+
} else {
16+
stack.push(item);
17+
}
18+
}
19+
return stack.length === 0;
20+
};
21+
22+
for (let i = 0; i < s.length; i++) {
23+
if (isValid(arr)) result++;
24+
arr.unshift(arr.pop());
25+
}
26+
27+
return result;
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 1. 연속성을 체크하는 문제이면 스택 의심하기.
2+
// 가격이 떨어지지 않은 연속기간을 찾는 문제!, 연속된 값들의 관계를 추적할 때 스택이 용이하다.
3+
// 2. 이전 상태를 기억해야하는지
4+
// 현재 가격을 이전의 가격들과 계속 비교해야함.
5+
// 3. 나중에 들어온 데이터가 이전 데이터에 영향을 주는지
6+
// 현재 시점의 가격이 이전시점들의 '가격 유지 기간'에 영향을 준다 -> 스택으로 이전데이터 보관하기
7+
// => 연속성, 이전상태 기억, 나중데이터가 이전데이터에 영향을 주는 것 => 3가지를 체크하자 => 스택/큐
8+
function solution(prices) {
9+
const stack = []; // {price: 가격, time: 시점} 객체를 저장할 스택
10+
const totalSeconds = prices.length;
11+
const maintainedSeconds = Array(totalSeconds).fill(0); // 각 시점별 가격이 유지된 기간
12+
13+
for (let currentTime = 0; currentTime < totalSeconds; currentTime++) {
14+
const currentPrice = prices[currentTime];
15+
16+
while (stack.length && stack.at(-1).price > currentPrice) {
17+
const lastPrice = stack.pop();
18+
maintainedSeconds[lastPrice.time] = currentTime - lastPrice.time;
19+
}
20+
21+
stack.push({ price: currentPrice, time: currentTime });
22+
}
23+
24+
// 끝까지 가격이 떨어지지 않은 주식들 처리
25+
while (stack.length) {
26+
const remainingPrice = stack.pop();
27+
maintainedSeconds[remainingPrice.time] =
28+
totalSeconds - 1 - remainingPrice.time;
29+
}
30+
31+
return maintainedSeconds;
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const solution = (s) => {
2+
let cnt = 0;
3+
4+
for (let i = 0; i < s.length; i++) {
5+
cnt += s[i] === "(" ? +1 : -1;
6+
if (cnt < 0) return false;
7+
}
8+
9+
return cnt === 0;
10+
};

0 commit comments

Comments
 (0)