Skip to content

Commit b07c8a7

Browse files
authored
Merge pull request #53 from Do-ho/Do-ho
Week14 문제 풀이
2 parents 6226df1 + 0e3848d commit b07c8a7

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const getOddPalindromeLength = (s, idx) => { return loopPalindrome(s, idx-1, idx+1, 1); }
2+
const getEvenPalindromeLength = (s, idx) => {
3+
if(s[idx] != s[idx+1]) return 0;
4+
return loopPalindrome(s, idx-1, idx+2, 2);
5+
}
6+
7+
const loopPalindrome = (s, left, right, palindromeCount) => {
8+
while(left >= 0 && right < s.length) {
9+
if(s[left--] !== s[right++]) break;
10+
palindromeCount += 2
11+
}
12+
return palindromeCount;
13+
}
14+
15+
const solution = s => {
16+
let answer = 0;
17+
for (let i=0; i<s.length; i++) {
18+
answer = Math.max(answer, getOddPalindromeLength(s, i), getEvenPalindromeLength(s, i));
19+
}
20+
return answer;
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class CraneStack {
2+
constructor() {
3+
this.store = [];
4+
this.size = 0;
5+
}
6+
top() {
7+
if(this.size==0) return 0;
8+
return this.store[this.size-1];
9+
}
10+
pushAndGetResult(item) {
11+
if(this.top() === item) {
12+
this.pop();
13+
this.size--;
14+
return 2;
15+
}
16+
17+
this.store.push(item);
18+
this.size++;
19+
return 0;
20+
}
21+
pop() { return this.store.pop(); }
22+
}
23+
24+
const pickDoll = (board, x) => {
25+
for(let i=0; i<board.length; i++) {
26+
if (board[i][x] != 0) {
27+
const result = board[i][x];
28+
board[i][x] = 0;
29+
return result;
30+
}
31+
}
32+
return 0;
33+
}
34+
35+
const solution = (board, moves) => {
36+
let answer = 0;
37+
let basket = new CraneStack();
38+
39+
moves.forEach((item) => {
40+
const doll = pickDoll(board, item-1);
41+
if(doll!==0) answer += basket.pushAndGetResult(doll);
42+
})
43+
44+
return answer;
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const removeBracket = s => {
2+
const re = /[0-9]+(,[0-9]+)*/g;
3+
return s.match(re);
4+
}
5+
6+
const compareFunction = (prev, curr) => prev.length - curr.length;
7+
8+
const parseStr = s => {
9+
const strWithOutBracket = removeBracket(s);
10+
const parsed = strWithOutBracket.reduce((acc, cur) => {
11+
acc.push(cur.split(','));
12+
return acc;
13+
}, [])
14+
return parsed.sort(compareFunction);
15+
}
16+
17+
const solution = s => {
18+
const parsedArr = parseStr(s);
19+
const answer = parsedArr.reduce((acc, cur) => {
20+
const filterItem = cur.filter((item) => !acc.includes(item));
21+
acc.push(filterItem[0]);
22+
return acc;
23+
}, []).map(item => parseInt(item))
24+
return answer;
25+
}

0 commit comments

Comments
 (0)