Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add (programmers - level2 3문제, level 3, 4 각 1문제): js 풀이
- Loading branch information
Showing
5 changed files
with
175 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,16 @@ | ||
function solution(n, t, m, p) { | ||
let answer = ''; | ||
const arr = []; | ||
|
||
for (let i = 0; i < t * m; i++) { | ||
arr.push(...i.toString(n).split('')); | ||
} | ||
|
||
for (let i = 0; i < t * m; i++) { | ||
if (i % m === 0) { | ||
answer += arr[i + p - 1] | ||
} | ||
} | ||
|
||
return answer.toUpperCase(); | ||
} |
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(record) { | ||
const log = []; | ||
const users = {}; | ||
|
||
record.forEach(r => { | ||
const [action, uid, nickname] = r.split(" "); | ||
|
||
if (action === "Enter") { | ||
users[uid] = nickname; | ||
log.push({ uid, action }); | ||
} else if (action === "Leave") { | ||
log.push({ uid, action }); | ||
} else if (action === "Change") { | ||
users[uid] = nickname; | ||
} | ||
}); | ||
|
||
const answer = log.map(({ uid, action }) => { | ||
return `${users[uid]}님이 ${ | ||
action === "Enter" ? "들어왔습니다." : "나갔습니다." | ||
}`; | ||
}); | ||
|
||
return answer; | ||
} |
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,67 @@ | ||
function solution(fees, records) { | ||
let answer = []; | ||
|
||
const IN_LIST = {}; | ||
const OUT_LIST = {}; | ||
|
||
records.forEach(record => { | ||
const [time, number, action] = record.split(" "); | ||
|
||
if (action === "IN") { | ||
if (IN_LIST[number]) { | ||
IN_LIST[number].push(time); | ||
} else { | ||
IN_LIST[number] = [time]; | ||
} | ||
} else if (action === "OUT") { | ||
if (OUT_LIST[number]) { | ||
OUT_LIST[number].push(time); | ||
} else { | ||
OUT_LIST[number] = [time]; | ||
} | ||
} | ||
}); | ||
|
||
const [base, baseCharge, unitTime, unitCharge] = fees; | ||
|
||
for (const item of Object.entries(IN_LIST).sort( | ||
([a], [b]) => Number(a) - Number(b) | ||
)) { | ||
const [number, inList] = item; | ||
const outList = OUT_LIST[number]; | ||
|
||
let fee = 0; | ||
let totalMinutes = 0; | ||
inList.forEach((inTime, idx) => { | ||
const [inHour, inMinutes] = inTime.split(":").map(Number); | ||
const [outHour, outMinutes] = | ||
outList && outList.length > idx | ||
? outList[idx].split(":").map(Number) | ||
: [23, 59]; | ||
|
||
let calcHour = outHour; | ||
let calcMinutes = outMinutes; | ||
|
||
if (calcMinutes < inMinutes) { | ||
calcMinutes = calcMinutes + 60 - inMinutes; | ||
calcHour = calcHour - 1; | ||
} else { | ||
calcMinutes = calcMinutes - inMinutes; | ||
} | ||
|
||
calcHour = calcHour - inHour; | ||
totalMinutes += calcHour * 60 + calcMinutes; | ||
}); | ||
|
||
if (base < totalMinutes) { | ||
fee = | ||
baseCharge + Math.ceil((totalMinutes - base) / unitTime) * unitCharge; | ||
} else { | ||
fee = baseCharge; | ||
} | ||
|
||
answer.push(fee); | ||
} | ||
|
||
return answer; | ||
} |
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,24 @@ | ||
function solution(N, number) { | ||
const dp = Array.from({ length: 9 }, (v, i) => new Set()); | ||
|
||
for (let i = 1; i < 9; i++) { | ||
dp[i].add("1".repeat(i) * N); | ||
for (let j = 1; j < i; j++) { | ||
for (const num1 of dp[j]) { | ||
for (const num2 of dp[i - j]) { | ||
dp[i].add(num1 + num2); | ||
dp[i].add(num1 - num2); | ||
dp[i].add(num1 * num2); | ||
dp[i].add(num1 / num2); | ||
} | ||
} | ||
} | ||
|
||
if (dp[i].has(number)) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
solution(2, 11); |
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,43 @@ | ||
function solution(arr) { | ||
const len = Math.ceil(arr.length / 2); | ||
const MAX_DP = Array.from({ length: len }, (v, i) => | ||
Array.from({ length: len }, (v, i) => -Infinity) | ||
); | ||
const MIN_DP = Array.from({ length: len }, (v, i) => | ||
Array.from({ length: len }, (v, i) => Infinity) | ||
); | ||
|
||
for (let i = 0; i < len; i++) { | ||
MAX_DP[i][i] = Number(arr[i * 2]); | ||
MIN_DP[i][i] = Number(arr[i * 2]); | ||
} | ||
|
||
for (let cnt = 1; cnt < len; cnt++) { | ||
for (let i = 0; i < len - cnt; i++) { | ||
const j = i + cnt; | ||
for (let k = i; k < j; k++) { | ||
if (arr[k * 2 + 1] === "+") { | ||
MAX_DP[i][j] = Math.max( | ||
MAX_DP[i][j], | ||
MAX_DP[i][k] + MAX_DP[k + 1][j] | ||
); | ||
MIN_DP[i][j] = Math.min( | ||
MIN_DP[i][j], | ||
MIN_DP[i][k] + MIN_DP[k + 1][j] | ||
); | ||
} else { | ||
MAX_DP[i][j] = Math.max( | ||
MAX_DP[i][j], | ||
MAX_DP[i][k] - MIN_DP[k + 1][j] | ||
); | ||
MIN_DP[i][j] = Math.min( | ||
MIN_DP[i][j], | ||
MIN_DP[i][k] - MAX_DP[k + 1][j] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return MAX_DP[0][len - 1]; | ||
} |