Skip to content

Commit 44d371c

Browse files
authored
Merge pull request boost-algorithm#51 from kyu9341/master
week14 문제 풀이
2 parents 9c7df53 + 32e3d77 commit 44d371c

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

  • src/kyu9341
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const getLongestPalindrome = (left, right, arrFromStr) => {
2+
let maxLength = 0;
3+
const isInRange = (left, right) => left >= 0 && right < arrFromStr.length;
4+
5+
while (isInRange(left, right)) {
6+
if (arrFromStr[left] !== arrFromStr[right]) return maxLength;
7+
8+
maxLength = right - left + 1;
9+
left -= 1;
10+
right += 1;
11+
}
12+
13+
return maxLength;
14+
};
15+
16+
const solution = s => {
17+
let maxLength = 0;
18+
const arrFromStr = [...s];
19+
20+
const checkLength = (_, index) => {
21+
const odd = getLongestPalindrome(index, index, arrFromStr);
22+
const even = getLongestPalindrome(index, index + 1, arrFromStr);
23+
maxLength = Math.max(maxLength, Math.max(odd, even));
24+
};
25+
26+
arrFromStr.forEach(checkLength);
27+
28+
return maxLength;
29+
};
30+
31+
(() => {
32+
const inputs = ['abcdcba', 'abacde'];
33+
34+
inputs.forEach(input => console.log(solution(input)));
35+
})();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const range = (start, end) =>
2+
start < end ? Array.from({ length: end - start + 1 }, (_, idx) => idx + start) : [];
3+
4+
const solution = (board, moves) => {
5+
let count = 0;
6+
const stack = [];
7+
8+
const pickDoll = col => {
9+
for (const row of range(0, board.length - 1)) {
10+
const currentDoll = board[row][col - 1];
11+
board[row][col - 1] = 0;
12+
13+
const isExist = currentDoll !== 0;
14+
const isEqualToPrevDoll = stack[stack.length - 1] === currentDoll;
15+
16+
if (isExist) {
17+
if (isEqualToPrevDoll) {
18+
stack.pop();
19+
count += 2;
20+
break;
21+
}
22+
stack.push(currentDoll);
23+
break;
24+
}
25+
}
26+
};
27+
28+
moves.forEach(pickDoll);
29+
return count;
30+
}
31+
32+
(() => {
33+
const board = [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]];
34+
const moves = [1,5,3,5,1,2,1,4];
35+
36+
console.log(solution(board, moves));
37+
})();
38+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const parseStringToArray = str => str.substring(2, str.length - 2)
2+
.split("},{")
3+
.map(el => el.split(',').map(str => Number(str)));
4+
5+
const solution = s => {
6+
const set = new Set();
7+
const arr = parseStringToArray(s);
8+
9+
const compareLength = (prev, cur) => prev.length - cur.length;
10+
arr.sort(compareLength).flat().forEach(el => set.add(el));;
11+
12+
return [...set];
13+
}
14+
15+
(() => {
16+
const inputs = [
17+
"{{2},{2,1},{2,1,3},{2,1,3,4}}",
18+
"{{1,2,3},{2,1},{1,2,4,3},{2}}",
19+
"{{20,111},{111}}",
20+
"{{123}}",
21+
"{{4,2,3},{3},{2,3,4,1},{2,3}}",
22+
];
23+
24+
inputs.forEach(input => console.log(solution(input)));
25+
})();
26+

0 commit comments

Comments
 (0)