Lab 4
Lab 4
Algorithms
Lab Exam -4
Deep within the fabled city of El Dorado, hidden among golden temples and ancient relics, lies a
sacred cipher—the key to untold riches and power. Legend has it that only those who can
correctly rearrange its symbols may unlock the city’s greatest treasure. However, the cipher is
protected by two ancient traps, designed to keep unworthy explorers at bay.
You, a daring adventurer, have stumbled upon the cipher. Now, you must rearrange its letters
while obeying the rules of the Golden Code to claim the hidden treasure.
○ If the same symbol appears too close together, the cipher will activate a deadly
trap, sealing El Dorado forever.
○ If any sequence reads the same forward and backward, the illusion of El Dorado
will break, and the treasure will vanish into thin air.
Your Mission
If you can successfully rearrange the symbols while obeying these two rules, the golden vaults
will open before you.
If it is impossible, return an empty response (""), signifying that El Dorado’s riches will remain
lost forever.
Example 1:
Input: s = "aabbcc", k = 3, L = 3
Output: "abcabc"
Explanation:
In "abcabc":
● Every contiguous substring of length 3 (e.g., "abc", "bca", "cab") is not a palindrome.
Example 2:
Input: s = "aaabb", k = 2, L = 2
Output: "ababa"
Explanation:
In "ababa":
Constraints:
● 1 ≤ s.length ≤ 10⁴
● 1 ≤ k ≤ s.length
● 2 ≤ L ≤ s.length
Once upon a time, in the mystical land of Gridora, a brave traveler found himself at the entrance
of an enchanted maze. This maze was no ordinary labyrinth—it was a vast N × M grid, where each
cell held a mysterious cost, representing the energy needed to step on it.
The traveler’s goal was clear: to journey from the top-left corner (0,0) to the bottom-right
corner (N-1, M-1), where the legendary treasure of Gridora awaited. However, there was a
catch—he could only move right or down at each step.
The maze was treacherous, and every step drained his energy. The traveler had to be wise and
choose a path that would minimize his total energy consumption to reach the treasure.
1. The traveler starts at position (0,0) and must reach (N-1, M-1).
2. Vjr e. D. FEach step costs energy, as defined by the number on the cell.
4. His challenge is to find the path that requires the least amount of energy to complete the
journey.
Example 1:
Input:
3 3
1 3 1
1 5 1
4 2 1
Output:
Explanation:
The minimum-cost path is (0,0) → (0,1) → (0,2) → (1,2) → (2,2) with cost:
1 + 3 + 1 + 1 + 1 = 7
Constraints:
● 1≤N,M≤1000
● 1≤grid[i][j]≤100