-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add (programmers 코딩테스트 입문 Day 21): js 풀이
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function solution(sides) { | ||
const maxSide = Math.max(...sides); | ||
const minSide = Math.min(...sides); | ||
|
||
let count = 0; | ||
for (let i = 1; i <= maxSide; i++) { | ||
if (i + minSide > maxSide) { | ||
count = count + 1; | ||
} | ||
} | ||
|
||
const sum = sides.reduce((acc, curr) => acc + curr, 0); | ||
for (let j = maxSide + 1; j < sum; j++) { | ||
count = count + 1; | ||
} | ||
|
||
return count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function solution(my_string) { | ||
const onlyNumber = my_string | ||
.replace(/[^0-9]+/g, ',') | ||
.split(',') | ||
.filter(val => val !== ''); | ||
|
||
return onlyNumber.reduce((acc, curr) => acc + Number(curr), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
function solution(board) { | ||
const len = board.length; | ||
const tempBoard = board.map(arr => { | ||
return arr.slice(); | ||
}); | ||
|
||
for (let i = 0; i < board.length; i++) { | ||
for (let j = 0; j < board.length; j++) { | ||
if (board[i][j] === 1) { | ||
// 상단 | ||
if (j - 1 >= 0) { | ||
tempBoard[i][j - 1] = 1; | ||
} | ||
|
||
// 우측 상단 | ||
if (i < len - 1 && j - 1 >= 0) { | ||
tempBoard[i + 1][j - 1] = 1; | ||
} | ||
|
||
// 우측 | ||
if (i < len - 1) { | ||
tempBoard[i + 1][j] = 1; | ||
} | ||
|
||
// 우측 하단 | ||
if (i < len - 1 && j < len - 1) { | ||
tempBoard[i + 1][j + 1] = 1; | ||
} | ||
|
||
// 하단 | ||
if (j < len - 1) { | ||
tempBoard[i][j + 1] = 1; | ||
} | ||
|
||
// 좌측 하단 | ||
if (i - 1 >= 0 && j < len - 1) { | ||
tempBoard[i - 1][j + 1] = 1; | ||
} | ||
|
||
// 좌측 | ||
if (i - 1 >= 0) { | ||
tempBoard[i - 1][j] = 1; | ||
} | ||
|
||
// 좌측 상단 | ||
if (i - 1 >= 0 && j - 1 >= 0) { | ||
tempBoard[i - 1][j - 1] = 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return tempBoard.reduce( | ||
(acc, curr) => acc + curr.filter(val => val === 0).length, | ||
0 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function solution(spell, dic) { | ||
const result = dic.map(word => { | ||
let newWord = word; | ||
|
||
spell.forEach(char => { | ||
const wordArr = newWord.split(''); | ||
const idx = wordArr.findIndex(value => value === char); | ||
if (idx !== -1) { | ||
wordArr[idx] = ''; | ||
} else { | ||
wordArr.push(','); | ||
} | ||
|
||
newWord = wordArr.join(''); | ||
}); | ||
|
||
return newWord; | ||
}); | ||
|
||
if (result.filter(val => val === '').length > 0) { | ||
return 1; | ||
} else { | ||
return 2; | ||
} | ||
} |