diff --git a/HamSungJun/DynamicProgramming/Solution_1137.ts b/HamSungJun/DynamicProgramming/Solution_1137.ts new file mode 100644 index 0000000..ab7945e --- /dev/null +++ b/HamSungJun/DynamicProgramming/Solution_1137.ts @@ -0,0 +1,15 @@ +function tribonacci (n: number): number { + if (n <= 1) return n + if (n === 2) return 1 + let t0 = 0 + let t1 = 1 + let t2 = 1 + let acc = 0 + for (let i = 3; i <= n; i++) { + acc = t2 + t1 + t0 + t0 = t1 + t1 = t2 + t2 = acc + } + return acc +}; diff --git a/HamSungJun/DynamicProgramming/Solution_338.ts b/HamSungJun/DynamicProgramming/Solution_338.ts new file mode 100644 index 0000000..616f0f7 --- /dev/null +++ b/HamSungJun/DynamicProgramming/Solution_338.ts @@ -0,0 +1,14 @@ +function countBits (n: number): number[] { + if (n === 0) return [0] + const DP: number[] = new Array(n + 1) + DP[0] = 0 + DP[1] = 1 + for (let i = 2; i <= n; i++) { + if (Number.isInteger(Math.sqrt(i / 2))) { + DP[i] = 1 + } else { + DP[i] = DP[Math.floor(i / 2)] + (i % 2 === 0 ? 0 : 1) + } + } + return DP +}; diff --git a/HamSungJun/DynamicProgramming/Solution_746.ts b/HamSungJun/DynamicProgramming/Solution_746.ts new file mode 100644 index 0000000..334d331 --- /dev/null +++ b/HamSungJun/DynamicProgramming/Solution_746.ts @@ -0,0 +1,10 @@ +function minCostClimbingStairs (cost: number[]): number { + const DP = new Array(cost.length).fill(0) + for (let i = 2; i < cost.length; i++) { + DP[i] = Math.min( + DP[i - 1] + cost[i - 1], + DP[i - 2] + cost[i - 2] + ) + } + return DP[DP.length - 1] +}; diff --git a/HamSungJun/Solution_1281.ts b/HamSungJun/Solution_1281.ts new file mode 100644 index 0000000..a883e42 --- /dev/null +++ b/HamSungJun/Solution_1281.ts @@ -0,0 +1,11 @@ +function subtractProductAndSum (n: number): number { + let pAcc = 1 + let sAcc = 0 + while (n > 0) { + const nextDigit = (n % 10) + pAcc *= nextDigit + sAcc += nextDigit + n = Math.floor(n / 10) + } + return pAcc - sAcc +}; diff --git a/HamSungJun/Solution_1342.ts b/HamSungJun/Solution_1342.ts new file mode 100644 index 0000000..8e009da --- /dev/null +++ b/HamSungJun/Solution_1342.ts @@ -0,0 +1,12 @@ +function numberOfSteps (num: number): number { + let stepCount = 0 + while (num > 0) { + if (num % 2 === 0) { + num /= 2 + } else { + num -= 1 + } + stepCount += 1 + } + return stepCount +}; diff --git a/HamSungJun/Solution_1720.ts b/HamSungJun/Solution_1720.ts new file mode 100644 index 0000000..e1bede9 --- /dev/null +++ b/HamSungJun/Solution_1720.ts @@ -0,0 +1,8 @@ +function decode (encoded: number[], first: number): number[] { + const decoded = new Array(encoded.length + 1) + decoded[0] = first + for (let i = 1; i < decoded.length; i++) { + decoded[i] = decoded[i - 1] ^ encoded[i - 1] + } + return decoded +};