We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Bigger is Greater
Bigger is Greater
Sort by
recency
|
968 Discussions
|
Please Login in order to post a comment
My solution in Java language:
Tip: A nice observation is that once we swap our pivot position and the character most to the right that is greater than our pivot, we can reverse the rest of the string after the pivot (instead of sort).
This is because while we were finding the character to swap, we already ensured that the elements to the right of the pivot ARE smaller, so it's descending order. That's why reversing after the pivot point works.
Thank you Stepanov, with STL this is one liner with ternary operator...
C++ O(n) solution thanks to std::next_permutation: