Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions HamSungJun/HashTable/Solution_594.ts
Original file line number Diff line number Diff line change
@@ -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])
14 changes: 14 additions & 0 deletions HamSungJun/HashTable/Solution_645.ts
Original file line number Diff line number Diff line change
@@ -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]))
44 changes: 44 additions & 0 deletions HamSungJun/HashTable/Solution_720.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function longestWord (words: string[]): string {
const wordSet = new Set()
const lengthMap:Map<number, string[]> = 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']))