๐โโ๏ธJSON ๊ตฌ์กฐ๊ฐ ์ฌ๊ท ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ Tree ๊ตฌ์กฐ์์ ์ดํดํ ์ ์๋ค. (stringifyJSON)
๐โโ๏ธJSON.stringify ์ JSON.parse ๊ฐ seriealize, deserialize๋ผ๋ ๊ฒ์ ์ดํดํ ์ ์๋ค.
JSON.stringify
: Object type์ JSON์ผ๋ก ๋ณํ (serialized,์ง๋ ฌํ)
JSON.parse
: JSON์ Object type์ผ๋ก ๋ณํ (deserialized,์ญ์ง๋ ฌํ)
๐โโ๏ธJSON.stringify ์ JSON.parse ๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๊ณผ JSON์ ๋๋๋ค ์ ์๋ค.
๐โโ๏ธJSON์ ์ฌ๊ท ํธ์ถ์ ์ฌ์ฉํ ๋, ์ด๋์ ์ฌ์ฉํด์ผ ํ ์ง ์ดํดํ ์ ์๋ค.
null ํ์ ์ ๊ฐ์ฒด์ด๋ค. ๋ฒ๊ทธ๋ผ๋ ์๋ฆฌ๋ ์๋๋ฐ, ์ด๋ ์ฌ์ค์ด ์๋๋ผ๊ณ ํ๋ค. ๋ณดํต ๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ค.
๊ฐ์ฒด์ value ๊ฐ์ด undefined
๋๋ function(){}
์ผ ๋, JSON.springify
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด {}
์ด ์ถ๋ ฅ๋๋ค. ์ด๋ฅผ ๋ค์ JSON.parse
ํด๋ ์๋ ๊ฐ์ผ๋ก ๋์๊ฐ์ง ๋ชปํ๊ณ {}
์ด ์ถ๋ ฅ๋๋ค.
`[${[result]}]`
์ ๊ฐ์ ์(`[${result}]`
์ ๊ฐ๋ค)์ ํ์ฉํด์ ๋ฐฐ์ด์ JSON ํํ๋ก ๋ง๋ค ์ ์์ง๋ง ์ถ์ฒ๋์ง ์๋ ๋ฐฉ์์ด๋ค. ๋ฌธ์์ด์ ์ด์ฉํ๋ ๊ฒ์ด ์ ์์ด๋ผ๊ณ ํ๋ค.
`${[1, 2]}`
์ ์
๋ ฅํ๋ฉด ๋ฐฐ์ด์ด ํ๋ ค โ1,2โ๊ฐ ์ถ๋ ฅ๋๋ค.
๋ฌธ์์ด์ ์กฐ๊ธ ๋ ์์ ๋กญ๊ฒ ํ์ฉํ๊ธฐ ์ํด์๋ ์ด์ค์ผ์ดํ ์ํ์ค๋ฅผ ์ฐพ์๋ณผ ์ ์๋ค. \n
\'
, \"
DOM์ for๋ฌธ์ ์ ์ฉํ๋ฉด, ์๋ฆฌ๋จผํธ ์์ฑ ๋ถ๋ถ์ด ์ฌ์ ์ธ ๋๋ ๊ฒ์ฒ๋ผ ๋๊ปด์ง์ง๋ง ๋ฐ๋ณต๋ฌธ ์ข ๋ฃ์ ํจ๊ป ์ค์ฝํ๊ฐ ์ข ๋ฃ๋๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ ์ธ๋๋ ๊ฒ์ด ์๋๋ค.
Array.fill()
๋ฉ์๋๋ ๋ฐฐ์ด์ ์์ ์ธ๋ฑ์ค๋ถํฐ ๋ ์ธ๋ฑ์ค์ ์ด์ ๊น์ง ์ ์ ์ธ ๊ฐ ํ๋๋ก ์ฑ์ด๋ค.
function orderOfPresentation(N, K) {
// ์กฐ์ ๊ฐ์ N, ๋ฐํ ์์ K
const factorial = (n) => {
if (n <= 1) return 1;
return n * factorial(n - 1);
};
let order = 0; // ๋ฐํ ์์๋ฅผ ๋ด๋ ๋ณ์
// N๊ฐ์ ์กฐ ์ค์, ์ด๋ ํ ์กฐ๊ฐ ์ด๋ฏธ ํฌํจ๋์๋์ง ํ์ธํ๊ธฐ ์ํด ๋ฐฐ์ด์ ๋ง๋ ๋ค.
// ๋ง์ฝ N์ด 3์ด๋ผ๋ฉด [false, false, false, false]
// ์ ์ผ ์ฒซ ๋ฒ์งธ๋ ๋๋ฏธ ๋ฐ์ดํฐ(์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ์ง๋ง ์กฐ๋ 1๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ)
const isUsed = Array(N + 1).fill(false);
for (let i = 0; i < K.length; i++) {
const num = K[i];
// ์ฌ์ฉํ๋์ง ํ๋ณํ๊ธฐ ์ํด isUsed์ ์ฒดํฌํฉ๋๋ค. (์ค๋ณต์ด ์๋๊ธฐ ๋๋ฌธ์)
isUsed[num] = true;
// num๋ณด๋ค ์์ ์ฌ ์ ์๋ ์๋ค์ ๋ฐฐ์ด์ ๋ณต์ ํด์,
const candidates = isUsed.slice(1, num);
// ์ด ์ค์์ ์์ง ์ฌ์ฉ๋์ง ์์ ์์ ๊ฐ์๋ฅผ ๊ตฌํฉ๋๋ค.
const validCnt = candidates.filter((el) => el === false).length;
// ์์ง ์ฌ์ฉ๋์ง ์์ ์, ๊ทธ ์ ๊น์ง์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์นด์ดํธํฉ๋๋ค.
const formerCnt = validCnt * factorial(N - i - 1);
// order์ ์ถ๊ฐํฉ๋๋ค.
order = order + formerCnt;
}
return order;
}
function orderOfPresentation(N,K) {
//N = 5 , K = [3,5,4,2,1]
const fatorial = (n) => {
if(n <= 1) return 1;
return n * factorial(n - 1);
}
let order = 0;
const isUsed = Array(N + 1).fill(false);
// [false, false, false, false, false, false]
for (let i = 0; i < K.length; i++) {
const num = K[i]; //i = 0 , num = 3
isUsed[num] = true; // [false, false, false, true, false, false]
const candidates = isUsed.slice(1, num); //[false,false]
const validCnt = candidates.filter((el) => el === false).length; // 2
const formerCnt = validCnt * factorial(N - i - 1); //2 * 4!(=24) = 48
order = order + formerCnt; //48
}
const num = K[i]; //i = 1 , num = 5
isUsed[num] = true; // [false, false, false, true, false, true]
const candidates = isUsed.slice(1, num); //[false,false,true,false]
const validCnt = candidates.filter((el) => el === false).length; // 3
const formerCnt = validCnt * factorial(N - i - 1); //3 * 3!(=6) = 18
order = order + formerCnt; //48 + 18 = 66
}
const num = K[i]; //i = 2, num = 4
isUsed[num] = true; // [false, false, false, true, true, true]
const candidates = isUsed.slice(1, num); //[false,false,true]
const validCnt = candidates.filter((el) => el === false).length; // 2
const formerCnt = validCnt * factorial(N - i - 1); //2 * 2!(=2) = 4
order = order + formerCnt; //66 + 4 = 70
}
const num = K[i]; //i = 3, num = 2
isUsed[num] = true; // [false, false, true, true, true, true]
const candidates = isUsed.slice(1, num); //[false]
const validCnt = candidates.filter((el) => el === false).length; // 1
const formerCnt = validCnt * factorial(N - i - 1); //1 * 1! = 1
order = order + formerCnt; // 70 + 1 = 71
}
const num = K[i]; //i = 4, num = 1
isUsed[num] = true; // [false, true, true, true, true, true]
const candidates = isUsed.slice(1, num); //[]
const validCnt = candidates.filter((el) => el === false).length; // 0
const formerCnt = validCnt * factorial(N - i - 1); // 0 * 0 = 0
order = order + formerCnt; // 71 + 0 = 0
}
return order; // 71
}
์์ฆ ๋๋ฌด ์๋์ฝ๋๋ฅผ ๋์ถฉ ์ฐ๋๊ฒ ๊ฐ์๋ฐ ๋ค์ ์๋์ฝ๋๋ฅผ ๊ผผ๊ผผํ๊ฒ ์์ฑํด์ผํ ๊ฒ ๊ฐ๋ค.
์ฌ๊ทํจ์๋ฅผ ์๊ฐํด๋ด๊ณ ์์ฑํ๋๊ฒ ๋๋ฌด ์ด๋ ต๋ค. ํ ์ด๋ ์ฌ๊ท๋ ๋ง์์ฒ๋ผ ์๋์ ๋ต๋ตํ๋ค. ์ฒด๋ ฅ์ด ๋จ์ด์ ธ๊ฐ๋๋ฐ ์ด๋๋ ํ๊ณ ์ต๋ํ ์ฌ๋ ์๊ฐ์ ์ค์ฌ์ผํ ๊ฒ๊ฐ๋ค.
๋ฐ๋ณต๋ง์ด ์ด ๊ธธ์ด๋ค.