From 1362e17907cbd2bee33105e540bee826688ea38b Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Thu, 3 Jul 2025 19:27:41 +0700 Subject: [PATCH 1/8] feat(apartment): implement solution with 18 passing tests --- tsa_js/apartement-allocation.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tsa_js/apartement-allocation.js b/tsa_js/apartement-allocation.js index 709777f..047d3ce 100644 --- a/tsa_js/apartement-allocation.js +++ b/tsa_js/apartement-allocation.js @@ -8,8 +8,17 @@ function solveApartement(A, B, N, M, K) { * @returns {number} - Maximum number of apartments that can be allocated to applicants. */ - // YOUR CODE HERE - + let ans = 0 + for (let aplicantIdx = 0; aplicantIdx < A.length; aplicantIdx++) { + for (let availableIdx = 0; availableIdx < B.length; availableIdx++) { + if (Math.abs(A[aplicantIdx] - B[availableIdx]) <= K) { + ans++ + aplicantIdx++ + } else { + availableIdx++ + } + } + } return ans; } From 11709b7a673b00103bcb144cfe8ad5cabbdb1fe2 Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Wed, 23 Jul 2025 23:00:07 +0700 Subject: [PATCH 2/8] fix(apartment): pass all test cases --- tsa_js/apartement-allocation.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tsa_js/apartement-allocation.js b/tsa_js/apartement-allocation.js index 047d3ce..18da7e3 100644 --- a/tsa_js/apartement-allocation.js +++ b/tsa_js/apartement-allocation.js @@ -7,16 +7,19 @@ function solveApartement(A, B, N, M, K) { * @param {number} K - Maximum allowable size difference for a valid match. * @returns {number} - Maximum number of apartments that can be allocated to applicants. */ - + let aplicantIdx = 0 + let availableIdx = 0 let ans = 0 - for (let aplicantIdx = 0; aplicantIdx < A.length; aplicantIdx++) { - for (let availableIdx = 0; availableIdx < B.length; availableIdx++) { - if (Math.abs(A[aplicantIdx] - B[availableIdx]) <= K) { - ans++ - aplicantIdx++ - } else { - availableIdx++ - } + while (aplicantIdx < A.length && availableIdx < B.length) { + if (Math.abs(A[aplicantIdx] - B[availableIdx]) <= K) { + aplicantIdx++ + availableIdx++ + ans++ + } else if (A[aplicantIdx] < B[availableIdx]) { + aplicantIdx++ + } else { + availableIdx++ + } } return ans; From 3e6ca719f92523aa0a461fc7d6e6529608a2579c Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Thu, 24 Jul 2025 10:34:30 +0700 Subject: [PATCH 3/8] feat(tower-of-hanoi): implement solution with 12 passing tests --- tsa_js/tower-of-hanoi.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tsa_js/tower-of-hanoi.js b/tsa_js/tower-of-hanoi.js index 9d13004..300a5ba 100644 --- a/tsa_js/tower-of-hanoi.js +++ b/tsa_js/tower-of-hanoi.js @@ -8,7 +8,20 @@ function moveDisk(diskNumber, moves, sourceStack, destinationStack, auxiliarySta * @param {number} auxiliaryStack - The index of the auxiliary stack (typically 1, 2, or 3). */ - // YOUR CODE HERE + if (diskNumber < 1) { + return + + } + + if (diskNumber === 1) { + moves.push([sourceStack, destinationStack]) + return + + } + console.log(diskNumber, moves, sourceStack, destinationStack, auxiliaryStack) + moveDisk(diskNumber - 1, moves, sourceStack, auxiliaryStack, destinationStack) + moves.push([sourceStack, destinationStack]) + moveDisk(diskNumber - 1, moves, auxiliaryStack, destinationStack, sourceStack) } // Function to solve Tower of Hanoi problem @@ -16,8 +29,9 @@ function towerOfHanoi(numberOfDisks) { /** * @param {number} numberOfDisks - The total number of disks to move. Must be a positive integer. */ + + // YOUR CO - // YOUR CODE HERE } module.exports = { moveDisk, towerOfHanoi }; \ No newline at end of file From 3d5bf7785a6584872295d869b3b81c8162132ece Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Sat, 26 Jul 2025 20:39:51 +0700 Subject: [PATCH 4/8] fix(tower-of-hanoi): pass all test cases --- tsa_js/tower-of-hanoi.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tsa_js/tower-of-hanoi.js b/tsa_js/tower-of-hanoi.js index 300a5ba..edc6707 100644 --- a/tsa_js/tower-of-hanoi.js +++ b/tsa_js/tower-of-hanoi.js @@ -18,7 +18,7 @@ function moveDisk(diskNumber, moves, sourceStack, destinationStack, auxiliarySta return } - console.log(diskNumber, moves, sourceStack, destinationStack, auxiliaryStack) + moveDisk(diskNumber - 1, moves, sourceStack, auxiliaryStack, destinationStack) moves.push([sourceStack, destinationStack]) moveDisk(diskNumber - 1, moves, auxiliaryStack, destinationStack, sourceStack) @@ -29,8 +29,14 @@ function towerOfHanoi(numberOfDisks) { /** * @param {number} numberOfDisks - The total number of disks to move. Must be a positive integer. */ - - // YOUR CO + const moves = [] + moveDisk(numberOfDisks, moves, 1, 3, 2); + + console.log(moves.length) + for (const [from, to] of moves) { + console.log(from, to) + + } } From a3bae328008a203882dcc954a25f3141bb977532 Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Sat, 26 Jul 2025 20:51:11 +0700 Subject: [PATCH 5/8] feat(palindrome): implement solution with 8 passing tests --- tsa_js/palindrome-queries.js | 37 ++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/tsa_js/palindrome-queries.js b/tsa_js/palindrome-queries.js index 49d7358..864d62b 100644 --- a/tsa_js/palindrome-queries.js +++ b/tsa_js/palindrome-queries.js @@ -17,7 +17,12 @@ class HashSegmentTree { * @param {number} value - The value to update at the specified index. */ update(index, value) { - // YOUR CODE HERE + index += this.n + this.tree[index] = value % MOD + while (index > 1) { + index = Math.floor(index / 2) + this.tree[index] = (this.tree[index * 2] + this.tree[index * 2 + 1]) % MOD + } } /** @@ -30,8 +35,25 @@ class HashSegmentTree { let result = 0; l += this.n; r += this.n + 1; - - // YOUR CODE HERE + + if (l > r) { + return 0; + } + + while (l < r) { + if (l % 2 === 1) { + result = (result + this.tree[l]) % MOD; + l++ + } + + if (r % 2 === 1) { + r-- + result = (result + this.tree[r]) % MOD + } + + l = Math.floor(l / 2) + r = Math.floor(r / 2) + } return result; } @@ -43,9 +65,12 @@ class HashSegmentTree { * @returns {number[]} - An array where hashPower[i] = HASH^i % MOD. */ function initializeHashPowers(length) { - // YOUR CODE HERE - - return hashPower; + const hashPower = [1] + for (let i = 1; i < length; i++) { + hashPower.push((hashPower[i - 1] * HASH) % MOD) + } + + return hashPower } /** From 0d6e0d48d953203bccf54ed32650b2174271547c Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Sun, 27 Jul 2025 14:16:53 +0700 Subject: [PATCH 6/8] fix(palindrome): pass all 12 test cases --- tsa_js/palindrome-queries.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tsa_js/palindrome-queries.js b/tsa_js/palindrome-queries.js index 864d62b..43319f8 100644 --- a/tsa_js/palindrome-queries.js +++ b/tsa_js/palindrome-queries.js @@ -81,8 +81,19 @@ function initializeHashPowers(length) { * @returns {Object} - An object containing the forward and backward hash segment trees. */ function initializeHashTables(n, s, hashPower) { - // YOUR CODE HERE + const fwdHash = new HashSegmentTree(n) + const bckHash = new HashSegmentTree(n) + + for (let i = 0; i < n; i++) { + const code = s.charCodeAt(i) + + const fwdVal = (code * hashPower[i]) % MOD + const bckVal = (code * hashPower[n - 1 - i]) % MOD + fwdHash.update(i, fwdVal) + bckHash.update(i, bckVal) + } + return { fwdHash, bckHash }; } From 228b6291422eafc2bcab60c5d7bb845a900a1cdb Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Sun, 27 Jul 2025 21:25:12 +0700 Subject: [PATCH 7/8] fix(palindrome): pass all test cases --- tsa_js/palindrome-queries.js | 48 +++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tsa_js/palindrome-queries.js b/tsa_js/palindrome-queries.js index 43319f8..5984e59 100644 --- a/tsa_js/palindrome-queries.js +++ b/tsa_js/palindrome-queries.js @@ -107,7 +107,53 @@ function initializeHashTables(n, s, hashPower) { * @returns {string[]} - An array of results for palindrome queries ("YES" or "NO"). */ function processOperations(n, operations, s) { - // YOUR CODE HERE + const hashPowers = initializeHashPowers(n) + + let inverseHash = 1 + let hashBase = HASH % MOD + let exponent = MOD - 2 + while (exponent > 0) { + if (exponent % 2 === 1) { + inverseHash = (inverseHash * hashBase) % MOD + } + hashBase = (hashBase * hashBase) % MOD + exponent = Math.floor(exponent / 2) + } + + const inversePowers = [1] + for (let i = 1; i < n; i++) { + inversePowers.push((inversePowers[i - 1] * inverseHash) % MOD) + + } + + const { fwdHash, bckHash } = initializeHashTables(n, s, hashPowers) + const results = [] + for (const operation of operations) { + if (operation[0] === 1) { + const updateIdx = operation[1] + const newChar = operation[2] + const charCode = newChar.charCodeAt(0) + const newFwdHash = (charCode * hashPowers[updateIdx]) % MOD + const newBckHash = (charCode * hashPowers[n - 1 - updateIdx]) % MOD + fwdHash.update(updateIdx, newFwdHash) + bckHash.update(updateIdx, newBckHash) + } else if (operation[0] === 2) { + let left = operation[1] - 1 + let right = operation[2] - 1 + const forwardHash = fwdHash.query(left, right) + const backwardHash = bckHash.query(left, right) + const normalizedForward = (forwardHash * inversePowers[left]) % MOD + const normalizedBackward = (backwardHash * inversePowers[n - 1 - right]) % MOD + if (normalizedForward === normalizedBackward) { + results.push("YES") + } else { + results.push("NO") + } + + } + } + + return results } module.exports = { From 7e07f99917e0a48ec3e02dceefccd173f51b37ee Mon Sep 17 00:00:00 2001 From: crbsdndr Date: Sun, 27 Jul 2025 22:55:50 +0700 Subject: [PATCH 8/8] style: add missing semicolons and fix spacing --- tsa_js/apartement-allocation.js | 19 ++--- tsa_js/palindrome-queries.js | 121 ++++++++++++++++++-------------- tsa_js/tower-of-hanoi.js | 22 +++--- 3 files changed, 90 insertions(+), 72 deletions(-) diff --git a/tsa_js/apartement-allocation.js b/tsa_js/apartement-allocation.js index 18da7e3..a1aa4b5 100644 --- a/tsa_js/apartement-allocation.js +++ b/tsa_js/apartement-allocation.js @@ -7,20 +7,23 @@ function solveApartement(A, B, N, M, K) { * @param {number} K - Maximum allowable size difference for a valid match. * @returns {number} - Maximum number of apartments that can be allocated to applicants. */ - let aplicantIdx = 0 - let availableIdx = 0 - let ans = 0 + let aplicantIdx = 0; + let availableIdx = 0; + let ans = 0; while (aplicantIdx < A.length && availableIdx < B.length) { if (Math.abs(A[aplicantIdx] - B[availableIdx]) <= K) { - aplicantIdx++ - availableIdx++ - ans++ + aplicantIdx++; + availableIdx++; + ans++; + } else if (A[aplicantIdx] < B[availableIdx]) { - aplicantIdx++ + aplicantIdx++; + } else { - availableIdx++ + availableIdx++; } + } return ans; } diff --git a/tsa_js/palindrome-queries.js b/tsa_js/palindrome-queries.js index 5984e59..70ffb6e 100644 --- a/tsa_js/palindrome-queries.js +++ b/tsa_js/palindrome-queries.js @@ -17,11 +17,12 @@ class HashSegmentTree { * @param {number} value - The value to update at the specified index. */ update(index, value) { - index += this.n - this.tree[index] = value % MOD + index += this.n; + this.tree[index] = value % MOD; while (index > 1) { - index = Math.floor(index / 2) - this.tree[index] = (this.tree[index * 2] + this.tree[index * 2 + 1]) % MOD + index = Math.floor(index / 2); + this.tree[index] = (this.tree[index * 2] + this.tree[index * 2 + 1]) % MOD; + } } @@ -32,27 +33,30 @@ class HashSegmentTree { * @returns {number} - The sum of values in the range [l, r]. */ query(l, r) { - let result = 0; - l += this.n; - r += this.n + 1; - if (l > r) { return 0; } + let result = 0; + l += this.n; + r += this.n + 1; + while (l < r) { if (l % 2 === 1) { result = (result + this.tree[l]) % MOD; - l++ + l++; + } if (r % 2 === 1) { - r-- - result = (result + this.tree[r]) % MOD + r--; + result = (result + this.tree[r]) % MOD; + } - l = Math.floor(l / 2) - r = Math.floor(r / 2) + l = Math.floor(l / 2); + r = Math.floor(r / 2); + } return result; @@ -65,12 +69,13 @@ class HashSegmentTree { * @returns {number[]} - An array where hashPower[i] = HASH^i % MOD. */ function initializeHashPowers(length) { - const hashPower = [1] + const hashPower = [1]; for (let i = 1; i < length; i++) { - hashPower.push((hashPower[i - 1] * HASH) % MOD) + hashPower.push((hashPower[i - 1] * HASH) % MOD); + } - return hashPower + return hashPower; } /** @@ -81,17 +86,16 @@ function initializeHashPowers(length) { * @returns {Object} - An object containing the forward and backward hash segment trees. */ function initializeHashTables(n, s, hashPower) { - const fwdHash = new HashSegmentTree(n) - const bckHash = new HashSegmentTree(n) - + const fwdHash = new HashSegmentTree(n); + const bckHash = new HashSegmentTree(n); for (let i = 0; i < n; i++) { - const code = s.charCodeAt(i) - - const fwdVal = (code * hashPower[i]) % MOD - const bckVal = (code * hashPower[n - 1 - i]) % MOD + const code = s.charCodeAt(i); + + const fwdVal = (code * hashPower[i]) % MOD; + const bckVal = (code * hashPower[n - 1 - i]) % MOD; - fwdHash.update(i, fwdVal) - bckHash.update(i, bckVal) + fwdHash.update(i, fwdVal); + bckHash.update(i, bckVal); } return { fwdHash, bckHash }; @@ -107,53 +111,64 @@ function initializeHashTables(n, s, hashPower) { * @returns {string[]} - An array of results for palindrome queries ("YES" or "NO"). */ function processOperations(n, operations, s) { - const hashPowers = initializeHashPowers(n) + const hashPowers = initializeHashPowers(n); - let inverseHash = 1 - let hashBase = HASH % MOD - let exponent = MOD - 2 + let inverseHash = 1; + let hashBase = HASH % MOD; + let exponent = MOD - 2; while (exponent > 0) { if (exponent % 2 === 1) { - inverseHash = (inverseHash * hashBase) % MOD + inverseHash = (inverseHash * hashBase) % MOD; + } - hashBase = (hashBase * hashBase) % MOD - exponent = Math.floor(exponent / 2) + + hashBase = (hashBase * hashBase) % MOD; + exponent = Math.floor(exponent / 2); + } - const inversePowers = [1] + const inversePowers = [1]; for (let i = 1; i < n; i++) { - inversePowers.push((inversePowers[i - 1] * inverseHash) % MOD) - + inversePowers.push((inversePowers[i - 1] * inverseHash) % MOD); } - const { fwdHash, bckHash } = initializeHashTables(n, s, hashPowers) - const results = [] + const { fwdHash, bckHash } = initializeHashTables(n, s, hashPowers); + const results = []; for (const operation of operations) { if (operation[0] === 1) { - const updateIdx = operation[1] - const newChar = operation[2] - const charCode = newChar.charCodeAt(0) - const newFwdHash = (charCode * hashPowers[updateIdx]) % MOD - const newBckHash = (charCode * hashPowers[n - 1 - updateIdx]) % MOD - fwdHash.update(updateIdx, newFwdHash) - bckHash.update(updateIdx, newBckHash) + const updateIdx = operation[1]; + const newChar = operation[2]; + + const charCode = newChar.charCodeAt(0); + + const newFwdHash = (charCode * hashPowers[updateIdx]) % MOD; + const newBckHash = (charCode * hashPowers[n - 1 - updateIdx]) % MOD; + + fwdHash.update(updateIdx, newFwdHash); + bckHash.update(updateIdx, newBckHash); + } else if (operation[0] === 2) { - let left = operation[1] - 1 - let right = operation[2] - 1 - const forwardHash = fwdHash.query(left, right) - const backwardHash = bckHash.query(left, right) - const normalizedForward = (forwardHash * inversePowers[left]) % MOD - const normalizedBackward = (backwardHash * inversePowers[n - 1 - right]) % MOD + let left = operation[1] - 1; + let right = operation[2] - 1; + + const forwardHash = fwdHash.query(left, right); + const backwardHash = bckHash.query(left, right); + + const normalizedForward = (forwardHash * inversePowers[left]) % MOD; + const normalizedBackward = (backwardHash * inversePowers[n - 1 - right]) % MOD; + if (normalizedForward === normalizedBackward) { - results.push("YES") + results.push("YES"); + } else { - results.push("NO") + results.push("NO"); + } } } - return results + return results; } module.exports = { diff --git a/tsa_js/tower-of-hanoi.js b/tsa_js/tower-of-hanoi.js index edc6707..4b500d4 100644 --- a/tsa_js/tower-of-hanoi.js +++ b/tsa_js/tower-of-hanoi.js @@ -9,19 +9,19 @@ function moveDisk(diskNumber, moves, sourceStack, destinationStack, auxiliarySta */ if (diskNumber < 1) { - return + return; } if (diskNumber === 1) { - moves.push([sourceStack, destinationStack]) - return + moves.push([sourceStack, destinationStack]); + return; } - moveDisk(diskNumber - 1, moves, sourceStack, auxiliaryStack, destinationStack) - moves.push([sourceStack, destinationStack]) - moveDisk(diskNumber - 1, moves, auxiliaryStack, destinationStack, sourceStack) + moveDisk(diskNumber - 1, moves, sourceStack, auxiliaryStack, destinationStack); + moves.push([sourceStack, destinationStack]); + moveDisk(diskNumber - 1, moves, auxiliaryStack, destinationStack, sourceStack); } // Function to solve Tower of Hanoi problem @@ -29,13 +29,13 @@ function towerOfHanoi(numberOfDisks) { /** * @param {number} numberOfDisks - The total number of disks to move. Must be a positive integer. */ - const moves = [] + const moves = []; moveDisk(numberOfDisks, moves, 1, 3, 2); - - console.log(moves.length) + + console.log(moves.length); for (const [from, to] of moves) { - console.log(from, to) - + console.log(from, to); + } }