From db2be7bff5161c70cd81df356b75716db2e72543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=A8=EC=84=B1=EC=A4=80?= Date: Sat, 3 Jul 2021 17:31:37 +0900 Subject: [PATCH] HamSungJun-1688,1725,1844 --- .eslintrc.json | 4 +++- HamSungJun/Solution_1688.ts | 21 +++++++++++++++++++++ HamSungJun/Solution_1725.ts | 21 +++++++++++++++++++++ HamSungJun/Solution_1844.ts | 31 +++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 HamSungJun/Solution_1688.ts create mode 100644 HamSungJun/Solution_1725.ts create mode 100644 HamSungJun/Solution_1844.ts diff --git a/.eslintrc.json b/.eslintrc.json index 3a443a3..1228d6e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,6 +16,8 @@ "@typescript-eslint" ], "rules": { - "max-len": 0 + "max-len": 0, + "indent": "off", + "@typescript-eslint/indent": ["error"] } } diff --git a/HamSungJun/Solution_1688.ts b/HamSungJun/Solution_1688.ts new file mode 100644 index 0000000..42f02a7 --- /dev/null +++ b/HamSungJun/Solution_1688.ts @@ -0,0 +1,21 @@ +function numberOfMatches (n: number): number { + /** + * 남은 토너먼트 팀이 짝수일때와 홀수일때 + * 필요한 연산을 진행하여 매치 카운트를 증가 + * 이후 승자팀이 결정되었을 때 카운트를 반환. + */ + let numTeams = n + let matchCount = 0 + while (numTeams > 1) { + if (numTeams % 2 === 0) { + numTeams /= 2 + matchCount += numTeams + } else { + numTeams = (numTeams - 1) / 2 + 1 + matchCount += numTeams - 1 + } + } + return matchCount +}; + +numberOfMatches(7) diff --git a/HamSungJun/Solution_1725.ts b/HamSungJun/Solution_1725.ts new file mode 100644 index 0000000..144513d --- /dev/null +++ b/HamSungJun/Solution_1725.ts @@ -0,0 +1,21 @@ +function countGoodRectangles (rectangles: number[][]): number { + /** + * 1. 두 사이드의 길이중 작은 값을 선택하면서 등장한 작은 값중 최대값을 기억 + * 2. 작은 값으로 선택된 리스트를 순회하면서 원소별 등장 갯수를 확인 + * 3. 기억해둔 최대값을 Map에 조회하여 답안을 반환 + */ + let maxLen = Number.MIN_SAFE_INTEGER + const cuts = rectangles.map(rect => { + const maxSideLength = Math.min(rect[0], rect[1]) + maxLen = Math.max(maxLen, maxSideLength) + return maxSideLength + }).reduce((acc, curr) => { + if (acc.has(curr)) { + acc.set(curr, acc.get(curr) + 1) + } else { + acc.set(curr, 1) + } + return acc + }, new Map()) + return cuts.get(maxLen) +}; diff --git a/HamSungJun/Solution_1844.ts b/HamSungJun/Solution_1844.ts new file mode 100644 index 0000000..dea0338 --- /dev/null +++ b/HamSungJun/Solution_1844.ts @@ -0,0 +1,31 @@ +function replaceDigits (s: string): string { + /** + * 1. 클로저 내부 캐쉬용도로 사용할 Map 자료구조 배치. + * 2. shift 연산이 많아질 수록 캐쉬에 등록해둔 결과 값이 많아지므로 + * 3. 이후 인풋의 크기가 커질 수록 성능상에서 유리해질 수 있을것이라 생각함. + */ + const cachedConverter = cachedFn() + let out = '' + for (let i = 0; i < s.length; i++) { + const nextCharCode = s[i].charCodeAt(0) + if (nextCharCode >= 48 && nextCharCode <= 57) { + out += cachedConverter(s[i - 1], +s[i]) + } else { + out += s[i] + } + } + return out +}; + +function cachedFn () { + const cacheMap: Map = new Map() + return (alpha: string, shiftDiff: number) => { + const cacheKey = alpha + shiftDiff + if (!cacheMap.has(cacheKey)) { + cacheMap.set(cacheKey, String.fromCharCode(alpha.charCodeAt(0) + shiftDiff)) + } + return cacheMap.get(cacheKey) + } +} + +console.log(replaceDigits('a1b2c3d4e'))