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 ab3491c

Browse files
Merge pull request #21 from codeisneverodd/codeisneverodd
[풀이 추가] 2022年04月06日 1문제 추가
2 parents f2520e9 + 924983d commit ab3491c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

‎level-3/N-Queen.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n) {
5+
/*
6+
1. 0번째 행에 0번째 queen을 놓는다.
7+
2. 그 다음 행의 퀸은 이전 퀸들의 범위와 겹치지 않는 곳에 놓는다. 퀸은 한 행에 반드시 하나 두어야한다.
8+
3. 마지막 열까지 도달하면 성공으로 간주하고 answer에 1을 더한다.
9+
4. 0번째 queen의 위치를 바꿔가며 모두 시도한다.
10+
4. 단, 체스판은 일차원 배열로 선언하고 index = 행, 값 = 열 로 생각한다.
11+
*/
12+
let answer = 0;
13+
const canBePlacedOn = (chess, currentRow) => {
14+
//해당 행에 둔 queen이 유효한지
15+
for (let prevRow = 0; prevRow < currentRow; prevRow++) {
16+
const onDiagonal = currentRow - prevRow === Math.abs(chess[currentRow] - chess[prevRow])
17+
const onStraight = chess[prevRow] === chess[currentRow]
18+
if (onDiagonal || onStraight) return false
19+
}
20+
return true
21+
}
22+
const placeQueen = (chess, currentRow) => {
23+
//queen을 배치하다가 끝 행에 도착하면 1을 리턴, 도착하지 못하면 0을 리턴하여, 재귀적으로 모든 경우를 합하여 리턴
24+
let count = 0
25+
if (currentRow === chess.length) return 1
26+
for (let currentQueen = 0; currentQueen < n; currentQueen++) {
27+
//queen을 우선 배치한 후 가능한지 살펴본다.
28+
chess[currentRow] = currentQueen
29+
if (canBePlacedOn(chess, currentRow)) count += placeQueen(chess, currentRow + 1)
30+
}
31+
return count
32+
}
33+
for (let firstQueen = 0; firstQueen < n; firstQueen++) {
34+
const chess = new Array(n).fill(-1)
35+
chess[0] = firstQueen
36+
answer += placeQueen(chess, 1)
37+
}
38+
return answer;
39+
}

‎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 によって変換されたページ (->オリジナル) /