diff --git a/HamSungJun/HashTable/Solution_594.ts b/HamSungJun/HashTable/Solution_594.ts new file mode 100644 index 0000000..afaf267 --- /dev/null +++ b/HamSungJun/HashTable/Solution_594.ts @@ -0,0 +1,23 @@ +function findLHS (nums: number[]): number { + const countMap = new Map() + nums.forEach(num => { + if (countMap.has(num)) { + countMap.set(num, countMap.get(num) + 1) + } else { + countMap.set(num, 1) + } + }) + const mapToArray = [...countMap.entries()] + mapToArray.sort((a, b) => { + return Number(a[0] > b[0]) - Number(a[0] < b[0]) + }) + let maxLHS = Number.MIN_SAFE_INTEGER + for (let i = 0; i < mapToArray.length - 1; i++) { + if (mapToArray[i + 1][0] - mapToArray[i][0] === 1) { + maxLHS = Math.max(maxLHS, mapToArray[i + 1][1] + mapToArray[i][1]) + } + } + return maxLHS +}; + +findLHS([1, 3, 2, 2, 5, 2, 3, 7]) diff --git a/HamSungJun/HashTable/Solution_645.ts b/HamSungJun/HashTable/Solution_645.ts new file mode 100644 index 0000000..3c36704 --- /dev/null +++ b/HamSungJun/HashTable/Solution_645.ts @@ -0,0 +1,14 @@ +function findErrorNums (nums: number[]): number[] { + const countArray = new Array(nums.length + 1).fill(0) + const answer = [] + nums.forEach(num => { + countArray[num] += 1 + if (countArray[num] === 2) { + answer.push(num) + } + }) + answer.push(countArray.findIndex((v, i) => v === 0 && i !== 0)) + return answer +}; + +console.log(findErrorNums([1, 1])) diff --git a/HamSungJun/HashTable/Solution_720.ts b/HamSungJun/HashTable/Solution_720.ts new file mode 100644 index 0000000..5c0b929 --- /dev/null +++ b/HamSungJun/HashTable/Solution_720.ts @@ -0,0 +1,44 @@ +function longestWord (words: string[]): string { + const wordSet = new Set() + const lengthMap:Map = new Map() + let possibleAnswers = [] + let longestAnswers = [] + for (let i = 0; i < words.length; i++) { + const nextWord = words[i] + const wordLen = nextWord.length + if (!wordSet.has(nextWord)) { + wordSet.add(nextWord) + if (lengthMap.has(wordLen)) { + (lengthMap.get(wordLen) as string[]).push(nextWord) + } else { + lengthMap.set(wordLen, [nextWord]) + } + } + } + if (!lengthMap.has(1)) { + return '' + } + possibleAnswers = [...(lengthMap.get(1) as string[])] + longestAnswers = possibleAnswers.slice() + while (possibleAnswers.length !== 0) { + const nextItem = possibleAnswers.shift() as string + if (lengthMap.has(nextItem.length + 1)) { + const reachableList = lengthMap.get(nextItem.length + 1)?.filter(str => str.startsWith(nextItem)) as string[] + possibleAnswers.push(...reachableList) + longestAnswers.push(...reachableList) + } + } + const maxLongestWord = longestAnswers[longestAnswers.length - 1].length + possibleAnswers = [] + for (let i = longestAnswers.length - 1; i >= 0; i--) { + if (longestAnswers[i].length === maxLongestWord) { + possibleAnswers.push(longestAnswers[i]) + } else { + break + } + } + possibleAnswers.sort() + return possibleAnswers[0] +}; + +console.log(longestWord(['a', 'banana', 'app', 'appl', 'ap', 'apply', 'apple']))