-
Notifications
You must be signed in to change notification settings - Fork 2
feat(datastructures, trees): tree traversal algorithms #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
72fbed2
feat(datastructures, binary-trees): sum nodes in range
BrianLusina 7075c1b
updating DIRECTORY.md
7d19b5f
feat(algorithms, dynamic-proramming): frog jumps
BrianLusina 5eec48b
feat(algorithms, backtracking): letter tile possibilities
BrianLusina df555a5
test(algorithms, dynamic-proramming): word-break additional tests
BrianLusina 7417216
feat(algorithms, dynamic-proramming): maximum profit in job scheduling
BrianLusina 2983f23
feat(algorithms, graphs): network delay time
BrianLusina 77cdb66
feat(algorithms, graphs): single cycle check
BrianLusina f43beb5
feat(algorithms, graphs): valid tree
BrianLusina dd4a1d0
feat(datastructures, tree): longest univalue
BrianLusina 0d8cfc2
updating DIRECTORY.md
67508bb
feat(math, geometry): self crossing
BrianLusina 5b54d51
feat(algorithms, two-pointers): sort by parity
BrianLusina a87ffb3
docs(algorithms, sliding-window): substring with concatenation
BrianLusina 0bc5654
updating DIRECTORY.md
a22aa85
Update datastructures/trees/binary/search_tree/binary_search_tree.py
BrianLusina 832a2ac
Update datastructures/trees/binary/search_tree/binary_search_tree.py
BrianLusina 40f7c03
Update algorithms/graphs/network_delay_time/test_network_delay_time.py
BrianLusina 07e2005
Update algorithms/dynamic_programming/frog_jump/README.md
BrianLusina 321f8e7
Update algorithms/sliding_window/substring_concatenation/README.md
BrianLusina cf79f0c
Update algorithms/dynamic_programming/max_profit_in_job_scheduling/RE…
BrianLusina 48b8618
Update algorithms/dynamic_programming/maximal_rectangle/README.md
BrianLusina 038c144
Update algorithms/intervals/data_stream/README.md
BrianLusina 1a71df5
Update algorithms/sliding_window/minimum_window_substring/README.md
BrianLusina 1536489
Update algorithms/sliding_window/substring_concatenation/__init__.py
BrianLusina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
144 changes: 144 additions & 0 deletions
144
algorithms/backtracking/letter_tile_possibilities/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Letter Tile Possibilities | ||
|
|
||
| You are given a string, tiles, consisting of uppercase English letters. You can arrange the tiles into sequences of any | ||
| length (from 1 to the length of tiles), and each sequence must include at most one tile, tiles[i], from tiles. | ||
|
|
||
| Your task is to return the number of possible non-empty unique sequences you can make using the letters represented on | ||
| tiles[i]. | ||
|
|
||
| ## Constraints: | ||
|
|
||
| - 1 ≤ `tiles.length` ≤ 7 | ||
| - The `tiles` string consists of uppercase English letters. | ||
|
|
||
| ## Examples | ||
|
|
||
| Example 1: | ||
|
|
||
| ```text | ||
| Input: tiles = "AAB" | ||
| Output: 8 | ||
| Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". | ||
| ``` | ||
|
|
||
| Example 2: | ||
| ```text | ||
| Input: tiles = "AAABBC" | ||
| Output: 188 | ||
| ``` | ||
|
|
||
| Example 3: | ||
| ```text | ||
| Input: tiles = "V" | ||
| Output: 1 | ||
| ``` | ||
|
|
||
| ## Topics | ||
|
|
||
| - Hash Table | ||
| - String | ||
| - Backtracking | ||
| - Counting | ||
|
|
||
| ## Solutions | ||
|
|
||
| ### Permutations and Combinations | ||
|
|
||
| This problem would have been straightforward if the tiles had no duplicates and we only needed to find sequences of the | ||
| same length as the given tiles. In that case, we could simply calculate the total unique sequences using n! (where n is | ||
| the length of the tiles). | ||
|
|
||
|  | ||
|
|
||
| However, in this problem, we need to find all unique sequences of any length—from 1 to the full length of the tiles—while | ||
| accounting for duplicate tiles. To achieve this, we take the following approach: | ||
|
|
||
| We begin by sorting the letters in tiles so that similar letters are grouped. This allows us to systematically explore | ||
| possible letter sets without repeating unnecessary work. | ||
|
|
||
| To generate sequences of lengths ranging from 1 to n, we consider each letter one by one. We start with a single | ||
| character, determine its possible sequences, then move to two-character combinations, find their possible sequences, | ||
| and so on. For each new character, we have two choices: either include the current letter in our selection or skip it | ||
| and move to the next one. By exploring both choices, we generate all possible letter sets. | ||
|
|
||
| To prevent counting the same sequences multiple times due to duplicate letters, we ensure (in the previous step) that | ||
| duplicate letter sets are not counted more than once while generating different letter sets. Then, for each set, we | ||
| calculate how many distinct sequences can be formed by applying the updated formula for permutations, which accounts | ||
| for repeated letters: `n! / c!`, where c is the count of each repeated letter. | ||
|
|
||
| The more repeated letters in a set, the fewer unique ways it can be arranged. | ||
|
|
||
| We repeat this process for every possible letter set. Eventually, we sum the count of all these possible sequences to | ||
| obtain the total number of unique non-empty sequences. Finally, we subtract one from our final count to exclude the | ||
| empty set, as it does not contribute to valid arrangements. | ||
|
|
||
| Let’s look at the following illustration to understand this better. | ||
|
|
||
|  | ||
|
|
||
| Now, let’s look at the algorithm steps of this solution: | ||
|
|
||
| - Initialize a set, unique_letter_sets, to track unique letter sets. | ||
| - Sort the input string, tiles, to group identical letters together. | ||
| - Generate unique letter sets and count their permutations: | ||
| - The function generate_sequences is used to recursively generate all possible letter subsets. | ||
| - We calculate the number of valid sequences using `count_permutations` for each unique letter set. The | ||
| `count_permutations` uses the helper function `factorial(n)`, which computes the factorial of a given number. | ||
| - The recursive function, `generate_sequences(tiles, current_letter_set, index, unique_letter_sets)`, works as follows: | ||
| - **Base case**: If `index` reaches the end of tiles, check if `current_letter_set` is unique. If it is: | ||
| - Add it to `unique_letter_sets`. | ||
| - Compute its permutations and return the count. | ||
| - **Recursive cases**: | ||
| - Exclude the current character (move to the next index). Store its result in the variable `without_letter`. | ||
| - Include the current character (append it and move to the next index). Store its result in the variable `with_letter`. | ||
| - The sum of both recursive calls gives the total count of valid sequences. | ||
| - Once all the letters in tiles have been explored, return the output. As an empty set is also considered in the | ||
| recursive process, we subtract 1 before returning the output. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| #### Time Complexity | ||
|
|
||
| Let’s break down and analyze the time complexity of this solution: | ||
| - Sorting the input string takes `O(n log(n))` time, where n is the length of the tiles. | ||
| - The recursive function explores all possible subsets of the given tiles. As each tile can either be included or | ||
| skipped, there are 2^n subsets in the worst case. | ||
| - For each subset, we calculate the number of unique sequences that can be formed using the formula that accounts for | ||
| duplicate letters. | ||
| - Computing the factorial and handling character frequencies takes at most `O(n)` time per subset. | ||
| - As we do this for all 2^n subsets, the total time complexity is `O(2 ^n × n)`, which dominates the sorting step. | ||
|
|
||
| If we sum these up, the overall time complexity simplifies to: | ||
|
|
||
| `O(nlogn) + O(2^n) + O(2^n×n) = O(2^n×n)` | ||
|
|
||
| #### Space Complexity | ||
| Let’s analyze the space complexity of this solution: | ||
| - As the recursion goes as deep as the length of the string n, the worst case space used by the function calls is `O(n)`. | ||
| - We store all unique subsets of tiles in a set, which can hold up to O(2^n) subsets in the worst case. Each sequence in | ||
| the set can be up to length n. So, the set uses `O(2^n × n)` space. | ||
| - The permutation calculation uses extra space, but this is at most O(n). | ||
|
|
||
| If we add these up, the overall space complexity simplifies to: | ||
|
|
||
| `O(n) + O(2^n × n) + O(n) = O(2^n × n)` | ||
94 changes: 94 additions & 0 deletions
94
algorithms/backtracking/letter_tile_possibilities/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from typing import List, Set | ||
| from itertools import permutations | ||
|
|
||
|
|
||
| def num_tile_possibilities(tiles: str) -> int: | ||
| # Store unique sequences to avoid duplicates | ||
| unique_letter_sets: Set[str] = set() | ||
|
|
||
| # Sort characters to handle duplicates efficiently | ||
| sorted_tiles: str = "".join(sorted(tiles)) | ||
|
|
||
| def generate_sequences(current: str, index: int): | ||
| # Recursively generates all possible sequences by including/excluding each tile. | ||
| # Once a new sequence is found, its unique permutations are counted. | ||
| if index >= len(sorted_tiles): | ||
| # If a new sequence is found, count its unique permutations | ||
| if current not in unique_letter_sets: | ||
| unique_letter_sets.add(current) | ||
|
|
||
| total_permutations = len(set(permutations(current))) | ||
| return total_permutations | ||
| return 0 | ||
|
|
||
| # Explore two possibilities: skipping the current character or including it | ||
| without_letter = generate_sequences(current, index=index + 1) | ||
| with_letter = generate_sequences(current + sorted_tiles[index], index=index + 1) | ||
|
|
||
| # Return all the possible sequences | ||
| return without_letter + with_letter | ||
|
|
||
| # Optionally, you can write the count_permutations and the factorial method if not using builtin methods | ||
| # def factorial(n): | ||
| # | ||
| # # Computes the factorial of a given number. | ||
| # if n <= 1: | ||
| # return 1 | ||
| # | ||
| # result = 1 | ||
| # for num in range(2, n + 1): | ||
| # result *= num | ||
| # return result | ||
| # | ||
| # def count_permutations(sequence): | ||
| # | ||
| # # Calculates the number of unique permutations of a sequence, accounting for duplicate characters. | ||
| # permutations = factorial(len(sequence)) | ||
| # | ||
| # # Adjust for repeated characters by dividing by factorial of their counts | ||
| # for count in Counter(sequence).values(): | ||
| # permutations //= factorial(count) | ||
| # | ||
| # return permutations | ||
|
|
||
| output = generate_sequences("", 0) | ||
|
|
||
| return output - 1 | ||
|
|
||
|
|
||
| def num_tile_possibilities_with_recursion(tiles: str) -> int: | ||
| sequences: Set[str] = set() | ||
| used: List[bool] = [False] * len(tiles) | ||
|
|
||
| def generate_sequences(current: str) -> None: | ||
| sequences.add(current) | ||
|
|
||
| for idx, char in enumerate(tiles): | ||
| if not used[idx]: | ||
| used[idx] = True | ||
| generate_sequences(current + char) | ||
| used[idx] = False | ||
|
|
||
| generate_sequences("") | ||
| return len(sequences) - 1 | ||
|
|
||
|
|
||
| def num_tile_possibilities_with_optimized_recursion(tiles: str) -> int: | ||
| char_count: List[int] = [0] * 26 | ||
| for letter in tiles: | ||
| char_count[ord(letter) - ord("A")] += 1 | ||
|
|
||
| def generate_sequences() -> int: | ||
| result = 0 | ||
| for idx in range(26): | ||
| if char_count[idx] == 0: | ||
| continue | ||
|
|
||
| result += 1 | ||
| char_count[idx] -= 1 | ||
| result += generate_sequences() | ||
| char_count[idx] += 1 | ||
|
|
||
| return result | ||
|
|
||
| return generate_sequences() |
Binary file added
BIN
+17.8 KB
...tile_possibilities/images/examples/letter_tile_possibilities_example_sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+67 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17.6 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.8 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+38.1 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.5 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+43.5 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+44.1 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+41 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+46.2 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.6 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.3 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+46.7 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.8 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.9 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27 KB
...r_tile_possibilities/images/solutions/letter_tile_possibilities_solution_21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.8 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+49.6 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.7 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+39 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+42.9 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+44.6 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.5 KB
...er_tile_possibilities/images/solutions/letter_tile_possibilities_solution_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions
40
algorithms/backtracking/letter_tile_possibilities/test_num_tile_possibilities.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from algorithms.backtracking.letter_tile_possibilities import ( | ||
| num_tile_possibilities, | ||
| num_tile_possibilities_with_recursion, | ||
| num_tile_possibilities_with_optimized_recursion, | ||
| ) | ||
|
|
||
| LETTER_TILE_POSSIBILITIES = [ | ||
| ("AAB", 8), | ||
| ("ABC", 15), | ||
| ("ABC", 15), | ||
| ("CDB", 15), | ||
| ("ZZZ", 3), | ||
| ("AAABBC", 188), | ||
| ("V", 1), | ||
| ] | ||
|
|
||
|
|
||
| class NumTilePossibilitiesTestCase(unittest.TestCase): | ||
| @parameterized.expand(LETTER_TILE_POSSIBILITIES) | ||
| def test_num_tile_possibilities(self, tiles: str, expected: int): | ||
| actual = num_tile_possibilities(tiles) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(LETTER_TILE_POSSIBILITIES) | ||
| def test_num_tile_possibilities_with_recursion(self, tiles: str, expected: int): | ||
| actual = num_tile_possibilities_with_recursion(tiles) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(LETTER_TILE_POSSIBILITIES) | ||
| def test_num_tile_possibilities_with_optimized_recursion( | ||
| self, tiles: str, expected: int | ||
| ): | ||
| actual = num_tile_possibilities_with_optimized_recursion(tiles) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # Frog Jump | ||
|
|
||
| A frog is trying to cross a river by jumping on stones placed at various positions along the river. The river is divided | ||
| into units, and some units contain stones while others do not. The frog can only land on a stone, but it must not jump | ||
| into the water. | ||
|
|
||
| You are given an array, stones, that represents the positions of the stones in ascending order (in units). The frog | ||
| starts on the first stone, and its first jump must be exactly 1 unit. | ||
|
|
||
| If the frog’s last jump was of length k units, its next jump must be either k−1, k, or k+1 units. The frog can only move | ||
| forward. | ||
|
|
||
| Your task is to determine whether the frog can successfully reach the last stone. | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 2 ≤ `stones.length` ≤ 1000 | ||
| - 0 ≤ `stones[i]` ≤ 2^20 − 1 | ||
| - stones[0] == 0 | ||
| - The stones array is sorted in a strictly increasing order. | ||
|
|
||
| ## Topics | ||
|
|
||
| - Array | ||
| - Dynamic Programming | ||
|
|
||
| ## Solution | ||
|
|
||
| The solution’s essence lies in leveraging dynamic programming to track all the jump lengths that can reach each stone. | ||
| The frog begins on the first stone with a jump length of zero, and from every reachable stone, it tries jumps of the | ||
| same size, one unit longer, or one unit shorter. Each valid move updates the DP table to record new reachable states. | ||
| The algorithm stores each stone’s position along with its index in a map, allowing for quick checks to determine if a | ||
| stone exists at a given position. Finally, if any jump length reaches the last stone, the frog can successfully cross | ||
| the river. | ||
|
|
||
| Using the intuition above, we implement the algorithm as follows: | ||
| - Store the number of stones in a variable n. | ||
| - Create an unordered map, `mapper`, to store each stone’s position as the key and its index as the value for fast | ||
| lookups. | ||
| - Populate the `mapper` by mapping every stone position, `stones[i]`, to its index, `i`. | ||
| - Initialize a 2D array, `dp`, of size `1001 x 1001` with zeros to track reachable states. | ||
| > Note that this is highly dependable on the constraints, larger sizes of stones will cause an Index out of bounds | ||
| > error. So, ensure to initialize the size correctly | ||
| - Set `dp[0][0]` to `1` to indicate that the frog starts on the first stone with a previous jump of 0. | ||
| - Iterate over each stone index `i`: | ||
| - For each stone, iterate over all possible previous jump lengths `prevJump`. | ||
| - Check if `dp[i][prevJump]` is 1 to confirm that the frog can reach stone `i` with jump size `prevJump`: | ||
| - If reachable, store the current position as `currPos = stones[i]`. | ||
| - Check if a stone exists at `currPos + prevJump`; if yes, mark `dp[mapper[currPos + prevJump]][prevJump]` as `1`. | ||
| - Check if a stone exists at `currPos + prevJump + 1`; if yes, mark `dp[mapper[currPos + prevJump + 1]][prevJump + 1]` as `1` | ||
| - If `prevJump > 1` and a stone exists at `currPos + prevJump - 1`, mark `dp[mapper[currPos + prevJump - 1]][prevJump - 1]` as `1`. | ||
| - After filling the DP table, iterate over all possible jump sizes `jump` for the last stone. | ||
| - If any `dp[n - 1][jump]` equals `1`, return TRUE to indicate that the frog can cross the river. | ||
| - If no valid jump leads to the last stone, return FALSE. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| ### Time Complexity | ||
|
|
||
| The algorithm’s time complexity is O(n^2), where n is the number of stones. For each stone, it may check all possible | ||
| previous jump lengths, and for each valid state, it performs constant-time lookups in the map. | ||
|
|
||
| ### Space Complexity | ||
|
|
||
| The algorithm's space complexity is `O(n)` for the mapper map, plus `O(1)` for the fixed-size 2D DP array (1001 x 1001), assuming the constraints guarantee `n ≤ 1000`. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.