Skip to content
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

LC_31. Next Permutation #197

Open
murane opened this issue Mar 16, 2025 · 0 comments
Open

LC_31. Next Permutation #197

murane opened this issue Mar 16, 2025 · 0 comments
Assignees
Labels

Comments

@murane
Copy link
Contributor

murane commented Mar 16, 2025

Problem link

https://leetcode.com/problems/next-permutation/description/

Problem Summary

Image

Solution

Source Code

class LC_31 {
    fun nextPermutation(nums: IntArray): Unit {
        val len = nums.size
        var pivot = -1

        for (i in len - 1 downTo 1) {
            if (nums[i - 1] < nums[i]) {  // 증가하는 부분 찾기
                pivot = i - 1
                break
            }
        }

        if (pivot == -1) {
            nums.reverse()
            return
        }

        for (i in len - 1 downTo 0) {
            if (nums[pivot] < nums[i]) {
                // Swap pivot과 해당 값
                nums[pivot] = nums[i].also { nums[i] = nums[pivot] }
                break
            }
        }

        nums.reverse(pivot + 1, len)
    }
}
@murane murane added the SOLVED label Mar 16, 2025
@murane murane self-assigned this Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: To do
Development

No branches or pull requests

1 participant