Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ksg2388 : [19week] #116

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Programmers/[68646] 풍선 터트리기/ksg2388/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# [68646] 풍선 터트리기

## :pushpin: **Algorithm**

구현, 메모이제이션
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메모이제이션이란 말 첨 들어봐용

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP 공부 다시 하세요.


## :round_pushpin: **Logic**

```javascript
for (let i = 1; i < a.length; i++) {
leftMin[i] = Math.min(leftMin[i - 1], a[i]);
rightMin[a.length - i - 1] = Math.min(
rightMin[a.length - i],
a[a.length - i]
);
}

a.forEach((balloon, index) => {
if (balloon <= leftMin[index] || balloon <= rightMin[index]) answer++;
});
```

- 현재 인덱스에서 왼쪽에서의 가장 최소값과 오른쪽에서의 가장 최소값을 저장하는 배열을 만들어 저장해둔다.
- 모든 배열을 돌면서 왼쪽과 오른쪽 중 하나라도 자신보다 큰 값이 있는 경우는 `answer`을 1증가 시킨다.

## :black_nib: **Review**

- 방법은 쉽게 떠올렸지만 메모이제이션을 사용하는 법이 바로 떠오르지 않아 시간초과가 났었다.
- 간단한 코드 몇줄로 시간이 엄청나게 줄어든다는건 신기하다...
17 changes: 17 additions & 0 deletions Programmers/[68646] 풍선 터트리기/ksg2388/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function solution(a) {
let answer = 0;
let leftMin = Array.from({ length: a.length }, () => a[0]);
let rightMin = Array.from({ length: a.length }, () => a[a.length - 1]);

for (let i = 1; i < a.length; i++) {
leftMin[i] = Math.min(leftMin[i - 1], a[i]);
rightMin[a.length - i - 1] = Math.min(
rightMin[a.length - i],
a[a.length - i]
);
}
a.forEach((balloon, index) => {
if (balloon <= leftMin[index] || balloon <= rightMin[index]) answer++;
});
return answer;
}