Skip to content

Instantly share code, notes, and snippets.

@lechuhuuha
Created July 15, 2024 04:56
Show Gist options
  • Save lechuhuuha/51ce59976701df970906df89b03b1fb7 to your computer and use it in GitHub Desktop.
Save lechuhuuha/51ce59976701df970906df89b03b1fb7 to your computer and use it in GitHub Desktop.
test
<?php
function count_highlighted_letters($L, $N, $selections)
{
// Initialize an array to keep track of highlighted status for each character in S
$highlighted = array_fill(0, $L, false);
// Process each selection
foreach ($selections as $selection) {
list($a, $b) = $selection;
// Convert to 0-based index
$a -= 1;
$b -= 1;
// Check if we need to highlight or un-highlight
$highlight = false;
for ($i = $a; $i <= $b; $i++) {
if (!$highlighted[$i]) {
$highlight = true;
break;
}
}
// Apply highlight or un-highlight
for ($i = $a; $i <= $b; $i++) {
$highlighted[$i] = $highlight;
}
}
// Count the number of highlighted letters
$highlighted_count = 0;
foreach ($highlighted as $status) {
if ($status) {
$highlighted_count++;
}
}
return $highlighted_count;
}
// Test cases
function run_tests()
{
$tests = [
[
"input" => [
"L" => 10,
"N" => 3,
"selections" => [
[2, 6],
[6, 8],
[3, 4]
]
],
"expected" => 5
],
[
"input" => [
"L" => 10,
"N" => 10,
"selections" => [
[1, 6],
[2, 5],
[1, 7],
[1, 7],
[2, 7],
[2, 8],
[1, 4],
[2, 2],
[1, 10],
[2, 10]
]
],
"expected" => 1
],
[
"input" => [
"L" => 100,
"N" => 5,
"selections" => [
[1, 100],
[1, 100],
[1, 100],
[1, 100],
[1, 100]
]
],
"expected" => 100
]
];
foreach ($tests as $index => $test) {
$input = $test["input"];
$expected = $test["expected"];
$result = count_highlighted_letters($input["L"], $input["N"], $input["selections"]);
echo "Test " . ($index + 1) . ": " . ($result === $expected ? "Passed" : "Failed") . "\n";
}
}
// Run the tests
run_tests();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment