diff --git a/tsa_js/apartement-allocation.js b/tsa_js/apartement-allocation.js index 709777f..a1aa4b5 100644 --- a/tsa_js/apartement-allocation.js +++ b/tsa_js/apartement-allocation.js @@ -7,9 +7,24 @@ 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. */ - - // YOUR CODE HERE + 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++; + } else if (A[aplicantIdx] < B[availableIdx]) { + aplicantIdx++; + + } else { + availableIdx++; + + } + + } return ans; } diff --git a/tsa_js/palindrome-queries.js b/tsa_js/palindrome-queries.js index 49d7358..70ffb6e 100644 --- a/tsa_js/palindrome-queries.js +++ b/tsa_js/palindrome-queries.js @@ -17,7 +17,13 @@ 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; + + } } /** @@ -27,11 +33,31 @@ class HashSegmentTree { * @returns {number} - The sum of values in the range [l, r]. */ query(l, r) { + if (l > r) { + return 0; + } + let result = 0; l += this.n; r += this.n + 1; - - // YOUR CODE HERE + + 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,8 +69,12 @@ class HashSegmentTree { * @returns {number[]} - An array where hashPower[i] = HASH^i % MOD. */ function initializeHashPowers(length) { - // YOUR CODE HERE - + const hashPower = [1]; + for (let i = 1; i < length; i++) { + hashPower.push((hashPower[i - 1] * HASH) % MOD); + + } + return hashPower; } @@ -56,8 +86,18 @@ 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 }; } @@ -71,7 +111,64 @@ 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 = { diff --git a/tsa_js/tower-of-hanoi.js b/tsa_js/tower-of-hanoi.js index 9d13004..4b500d4 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; + + } + + 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,15 @@ function towerOfHanoi(numberOfDisks) { /** * @param {number} numberOfDisks - The total number of disks to move. Must be a positive integer. */ + const moves = []; + moveDisk(numberOfDisks, moves, 1, 3, 2); + + console.log(moves.length); + for (const [from, to] of moves) { + console.log(from, to); + + } - // YOUR CODE HERE } module.exports = { moveDisk, towerOfHanoi }; \ No newline at end of file