-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms, greedy): max swap #173
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
2 commits
Select commit
Hold shift + click to select a range
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,101 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| def maximum_swap(num: int) -> int: | ||
| # Convert the number to a list of characters for easy manipulation | ||
| digits: List[str] = list(str(num)) | ||
| n = len(digits) | ||
|
|
||
| # Variables to track the index of the maximum digit and the two swap indices | ||
| max_digit_index = index_1 = index_2 = -1 | ||
|
|
||
| # Iterate the string from the last digit to the first | ||
| for i in range(n - 1, -1, -1): | ||
| # Update the max digit index if the current digit is greater | ||
| if max_digit_index == -1 or digits[i] > digits[max_digit_index]: | ||
| max_digit_index = i | ||
| # If the current digit is less than the max digit found so far, | ||
| # mark this index (index_1) and the max digit's index (index_2) for swapping | ||
| elif digits[i] < digits[max_digit_index]: | ||
| index_1 = i | ||
| index_2 = max_digit_index | ||
|
|
||
| # Perform the swap if valid indices are found | ||
| if index_1 != -1 and index_2 != -1: | ||
| digits[index_1], digits[index_2] = digits[index_2], digits[index_1] | ||
|
|
||
| # Convert the list back to an integer and return it | ||
| return int("".join(digits)) | ||
|
|
||
|
|
||
| def maximum_swap_suboptimal_greedy(num: int) -> int: | ||
| num_str = str(num) | ||
| n = len(num_str) | ||
| last_seen = [-1] * 10 # Store the last occurrence of each digit | ||
|
|
||
| # Record the last occurrence of each digit | ||
| for i in range(n): | ||
| last_seen[int(num_str[i])] = i | ||
|
|
||
| # Traverse the string to find the first digit that can be swapped with a larger one | ||
| for i in range(n): | ||
| for d in range(9, int(num_str[i]), -1): | ||
| if last_seen[d] > i: | ||
| # Perform the swap | ||
| num_str = list(num_str) | ||
| num_str[i], num_str[last_seen[d]] = ( | ||
| num_str[last_seen[d]], | ||
| num_str[i], | ||
| ) | ||
| num_str = "".join(num_str) | ||
|
|
||
| return int(num_str) # Return the new number immediately after the swap | ||
|
|
||
| return num # Return the original number if no swap can maximize it | ||
|
|
||
|
|
||
| def maximum_swap_greedy_two_pass(num: int) -> int: | ||
| num_str = list(str(num)) | ||
| n = len(num_str) | ||
| max_right_index = [0] * n | ||
|
|
||
| max_right_index[n - 1] = n - 1 | ||
| for i in range(n - 2, -1, -1): | ||
| max_right_index[i] = ( | ||
| i | ||
| if num_str[i] > num_str[max_right_index[i + 1]] | ||
| else max_right_index[i + 1] | ||
| ) | ||
|
|
||
| for i in range(n): | ||
| if num_str[i] < num_str[max_right_index[i]]: | ||
| num_str[i], num_str[max_right_index[i]] = ( | ||
| num_str[max_right_index[i]], | ||
| num_str[i], | ||
| ) | ||
| return int("".join(num_str)) | ||
|
|
||
| return num | ||
|
|
||
|
|
||
| def maximum_swap_brute_force(num: int) -> int: | ||
| num_str = str(num) # Convert num to string for easy manipulation | ||
| n = len(num_str) | ||
| max_num = num # Track the maximum number found | ||
|
|
||
| # Try all possible swaps | ||
| for i in range(n): | ||
| for j in range(i + 1, n): | ||
| num_list = list(num_str) # Convert the string to list for swapping | ||
|
|
||
| num_list[i], num_list[j] = ( | ||
| num_list[j], | ||
| num_list[i], | ||
| ) # Swap digits at index i and j | ||
| temp = int( | ||
| "".join(num_list) | ||
| ) # Convert the list back to string and then to integer | ||
|
|
||
| max_num = max(max_num, temp) # Update max_num if the new number is larger | ||
|
|
||
| return max_num # Return the largest number after all possible swaps |
Binary file added
BIN
+25.6 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+21.7 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+27.8 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+27 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+30.4 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+30.1 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+29.3 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+23.7 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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
+22.5 KB
algorithms/greedy/maximum_swap/images/solutions/maximum_swap_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.
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,44 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from algorithms.greedy.maximum_swap import ( | ||
| maximum_swap, | ||
| maximum_swap_suboptimal_greedy, | ||
| maximum_swap_greedy_two_pass, | ||
| maximum_swap_brute_force, | ||
| ) | ||
|
|
||
| MAXIMUM_SWAP_TEST_CASES = [ | ||
| (4121, 4211), | ||
| (87654, 87654), | ||
| (1643, 6143), | ||
| (123, 321), | ||
| (14, 41), | ||
| (2736, 7236), | ||
| (9973, 9973), | ||
| ] | ||
|
|
||
|
|
||
| class MaximumSwapTestCase(unittest.TestCase): | ||
| @parameterized.expand(MAXIMUM_SWAP_TEST_CASES) | ||
| def test_maximum_swap(self, num: int, expected: int): | ||
| actual = maximum_swap(num) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(MAXIMUM_SWAP_TEST_CASES) | ||
| def test_maximum_swap_suboptimal_greedy(self, num: int, expected: int): | ||
| actual = maximum_swap_suboptimal_greedy(num) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(MAXIMUM_SWAP_TEST_CASES) | ||
| def test_maximum_swap_greedy_two_pass(self, num: int, expected: int): | ||
| actual = maximum_swap_greedy_two_pass(num) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(MAXIMUM_SWAP_TEST_CASES) | ||
| def test_maximum_swap_brute_force(self, num: int, expected: int): | ||
| actual = maximum_swap_brute_force(num) | ||
| 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
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.