diff --git a/HamSungJun/Solution_1441.ts b/HamSungJun/Solution_1441.ts new file mode 100644 index 0000000..024cd25 --- /dev/null +++ b/HamSungJun/Solution_1441.ts @@ -0,0 +1,16 @@ +function buildArray (target: number[], n: number): string[] { + let cursor = 0 + const out = [] + for (let i = 1; i <= n; i++) { + if (target[cursor] === i) { + out.push('Push') + cursor += 1 + if (cursor === target.length) { + break + } + } else { + out.push('Push', 'Pop') + } + } + return out +}; diff --git a/HamSungJun/Solution_496.ts b/HamSungJun/Solution_496.ts new file mode 100644 index 0000000..ad46891 --- /dev/null +++ b/HamSungJun/Solution_496.ts @@ -0,0 +1,16 @@ +function nextGreaterElement (nums1: number[], nums2: number[]): number[] { + const numToIndexMap: Map = new Map() + for (let i = 0; i < nums2.length; i++) { + numToIndexMap.set(nums2[i], i) + } + return nums1.map(n => { + for (let i = numToIndexMap.get(n) as number + 1; i < nums2.length; i++) { + if (nums2[i] > n) { + return nums2[i] + } + } + return -1 + }) +}; + +console.log(nextGreaterElement([4, 1, 2], [1, 3, 4, 2])) diff --git a/HamSungJun/Solution_821.ts b/HamSungJun/Solution_821.ts new file mode 100644 index 0000000..6f90694 --- /dev/null +++ b/HamSungJun/Solution_821.ts @@ -0,0 +1,40 @@ +function shortestToChar (s: string, c: string): number[] { + const charToIndexSet: Set = new Set() + const remains = [] + for (let i = 0; i < s.length; i++) { + if (s[i] === c) { + charToIndexSet.add(i) + remains.push({ + v: 0, + isC: true + }) + } else { + remains.push({ + v: i, + isC: false + }) + } + } + return remains.map(({ v, isC }) => { + if (!isC) { + return getShortest(v, charToIndexSet) + } + return 0 + }) +}; + +const getShortest = (n: number, indexSet: Set): number => { + let left = n - 1 + let right = n + 1 + while (true) { + if (indexSet.has(left)) { + return n - left + } else if (indexSet.has(right)) { + return right - n + } + left -= 1 + right += 1 + } +} + +console.log(shortestToChar('loveleetcode', 'e'))