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
70 changes: 57 additions & 13 deletions tsa_php/src/ApartmentAllocation.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
<?php

/**
* Solves the apartment allocation problem.
* @param array $A - Array of integers representing the minimum apartment sizes each applicant desires.
* @param array $B - Array of integers representing the sizes of available apartments.
* @param int $N - Number of applicants (length of array A).
* @param int $M - Number of available apartments (length of array B).
* @param int $K - Maximum allowable size difference for a valid match.
* @return int - Maximum number of apartments that can be allocated to applicants.
*/
function solveApartment($A, $B, $N, $M, $K) {
/**
* @param array $A - Array of integers representing the minimum apartment sizes each applicant desires.
* @param array $B - Array of integers representing the sizes of available apartments.
* @param int $N - Number of applicants (length of array A).
* @param int $M - Number of available apartments (length of array B).
* @param int $K - Maximum allowable size difference for a valid match.
* @return int - Maximum number of apartments that can be allocated to applicants.
*/

// YOUR CODE HERE

return $matches;
}
sort($A);
sort($B);

$i = 0;
$j = 0;
$count = 0;

while ($i < $N && $j < $M) {
if (abs($A[$i] - $B[$j]) <= $K) {
$count++;
$i++;
$j++;
} elseif ($B[$j] < $A[$i] - $K) {
$j++;
} else {
$i++;
}
}

return $count;
}

/**
* Reads input from the user and allocates apartments.
*/
function allocateApartments() {
echo "Enter number of applicants (n): ";
$n = (int)trim(fgets(STDIN));

echo "Enter number of apartments (m): ";
$m = (int)trim(fgets(STDIN));

echo "Enter maximum allowed difference (k): ";
$k = (int)trim(fgets(STDIN));

echo "Enter desired apartment sizes for $n applicants (space-separated):\n";
$a = array_map('intval', explode(' ', trim(fgets(STDIN))));

echo "Enter available apartment sizes for $m apartments (space-separated):\n";
$b = array_map('intval', explode(' ', trim(fgets(STDIN))));

$result = solveApartment($a, $b, $n, $m, $k);

echo "Number of applicants who got an apartment: $result\n";
}

allocateApartments();
?>
86 changes: 81 additions & 5 deletions tsa_php/src/PalindromeQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function __construct($n) {
* @param int $value The value to update at the specified index.
*/
public function update($index, $value) {
// YOUR CODE HERE
$index += $this->n;
$this->tree[$index] = $value;
while ($index > 1) {
$index >>= 1;
$this->tree[$index] = ($this->tree[$index * 2] + $this->tree[$index * 2 + 1]) % MOD;
}
}

/**
Expand All @@ -32,7 +37,15 @@ public function update($index, $value) {
* @return int The sum of values in the range [l, r].
*/
public function query($l, $r) {
// YOUR CODE HERE
$l += $this->n;
$r += $this->n + 1;
$result = 0;
while ($l < $r) {
if ($l % 2 == 1) $result = ($result + $this->tree[$l++]) % MOD;
if ($r % 2 == 1) $result = ($result + $this->tree[--$r]) % MOD;
$l >>= 1;
$r >>= 1;
}
return $result;
}
}
Expand All @@ -43,7 +56,10 @@ public function query($l, $r) {
* @return array An array where hashPower[i] = HASH^i % MOD.
*/
function initializeHashPowers($length) {
// YOUR CODE HERE
$hashPower = [1];
for ($i = 1; $i <= $length; $i++) {
$hashPower[$i] = ($hashPower[$i - 1] * HASH) % MOD;
}
return $hashPower;
}

Expand All @@ -55,7 +71,17 @@ function initializeHashPowers($length) {
* @return array An associative array containing the forward and backward hash segment trees.
*/
function initializeHashTables($n, $s, $hashPower) {
// YOUR CODE HERE
$fwdHash = new HashSegmentTree($n);
$bckHash = new HashSegmentTree($n);

for ($i = 0; $i < $n; $i++) {
$val = (ord($s[$i]) * $hashPower[$i]) % MOD;
$fwdHash->update($i, $val);

$revVal = (ord($s[$i]) * $hashPower[$n - 1 - $i]) % MOD;
$bckHash->update($n - 1 - $i, $revVal);
}

return ['fwdHash' => $fwdHash, 'bckHash' => $bckHash];
}

Expand All @@ -67,9 +93,59 @@ function initializeHashTables($n, $s, $hashPower) {
* @return array An array of results for palindrome queries ("YES" or "NO").
*/
function processOperations($n, $operations, $s) {
// YOUR CODE HERE
$hashPower = initializeHashPowers($n);
$trees = initializeHashTables($n, $s, $hashPower);
$fwd = $trees['fwdHash'];
$bck = $trees['bckHash'];
$sArr = str_split($s);
$results = [];

foreach ($operations as $op) {
if ($op[0] == 1) {
// Update operation
$k = $op[1] - 1;
$x = $op[2];
$sArr[$k] = $x;
$fwd->update($k, (ord($x) * $hashPower[$k]) % MOD);
$bck->update($n - 1 - $k, (ord($x) * $hashPower[$n - 1 - $k]) % MOD);
} else if ($op[0] == 2) {
// Query operation
$a = $op[1] - 1;
$b = $op[2] - 1;
$fwdHash = $fwd->query($a, $b);
$bckHash = $bck->query($n - 1 - $b, $n - 1 - $a);

if ($a <= $b) {
$leftShift = $n - 1 - $b - $a;
$fwdHash = ($fwdHash * $hashPower[$leftShift]) % MOD;
}

$results[] = ($fwdHash == $bckHash) ? "YES" : "NO";
}
}

return $results;
}

// Read input
fscanf(STDIN, "%d %d", $n, $m);
$s = trim(fgets(STDIN));

// Read m operations
$operations = [];
for ($i = 0; $i < $m; $i++) {
$line = explode(' ', trim(fgets(STDIN)));
$type = (int)$line[0];
if ($type == 1) {
$operations[] = [1, (int)$line[1], $line[2]];
} else {
$operations[] = [2, (int)$line[1], (int)$line[2]];
}
}

// Process and print results
$results = processOperations($n, $operations, $s);
foreach ($results as $res) {
echo $res . "\n";
}
?>
15 changes: 15 additions & 0 deletions tsa_php/src/TowerOfHanoi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxiliaryStack)
{
// YOUR CODE HERE
if ($diskNumber == 1) {
$moves[] = [$sourceStack, $destinationStack];
return;
}

moveDisk($diskNumber - 1, $moves, $sourceStack, $auxiliaryStack, $destinationStack);
$moves[] = [$sourceStack, $destinationStack];
moveDisk($diskNumber - 1, $moves, $auxiliaryStack, $destinationStack, $sourceStack);
}

/**
Expand All @@ -23,6 +31,13 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxili
function towerOfHanoi($numberOfDisks)
{
// YOUR CODE HERE
$moves = [];
moveDisk($numberOfDisks, $moves, 1, 3, 2);

echo count($moves) . "\n";
foreach ($moves as [$from, $to]) {
echo "$from $to\n";
}
}

?>