Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 924983d

Browse files
[풀이 추가] 22.03.31 1문제, 단어 퍼즐
1 parent 72a1fb4 commit 924983d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎level-4/단어-퍼즐.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
//코드 참고자료: https://velog.io/@longroadhome/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-LV.4-%EB%8B%A8%EC%96%B4-%ED%8D%BC%EC%A6%90
5+
function solution(strs, t) {
6+
const tLength = t.length; //자주 쓰는 값 미리 계산
7+
//Infinity 로 선언을 해야 조합이 불가능한 영역의 값을 무한으로 두고, 그 영역에 하나를 더해도 불가능하다는 것을 Infinity로 표현할 수 있게 된다.
8+
const minCountToIndex = new Array(tLength).fill(Infinity);
9+
for (let currentIndex = 0; currentIndex < tLength; currentIndex++) {
10+
//내가 검사할 부분은 t의 0~currentIndex 영역
11+
const currentSlice = t.slice(0, currentIndex + 1);
12+
for (const str of strs) {
13+
//현재 영역이 strs에 있는 조각들 중 하나로 끝난다면
14+
if (currentSlice.endsWith(str)) {
15+
//frontLength 는 str 조각을 제외한 앞 쪽의 남은 조각의 길이
16+
const frontLength = currentIndex - str.length + 1;
17+
if (frontLength === 0) {
18+
//앞쪽에 남은 것이 없다면, 현재 검사중인 영역 = strs에 있는 조각
19+
minCountToIndex[currentIndex] = 1;
20+
} else {
21+
//앞쪽에 남은 것이 있다면, 현재 검사중이 영역까지 필요한 조각 수는, 지금까지 구한 최소 값과 지금 구한 값 중 최소값
22+
minCountToIndex[currentIndex] = Math.min(
23+
minCountToIndex[currentIndex],
24+
minCountToIndex[frontLength - 1] + 1
25+
);
26+
}
27+
}
28+
}
29+
}
30+
//마지막 영역이 Infinity 이면 만들기 불가능한 단어, 아니라면 마지막 영역의 값을 리턴
31+
return minCountToIndex[tLength - 1] === Infinity
32+
? -1
33+
: minCountToIndex[tLength - 1];
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /