Code Monkey home page Code Monkey logo
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script>

Next Permutation

Tag: leetcode


##Problem Statement

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1

The hard point of this problem is the definition of next permutation. The description of how to generate next permutatio itself is an algorithm to solve this problem.

From wikipedia, there's a simple algorithm. But we need to give some remarks to analyze it.

  1. Find the largest index $k$ such that $a[k] &lt; a[k + 1]$. If no such index exists, the permutation is the last permutation.
  1. Find the largest index $l$ greater than $k$ such that $a[k] &lt; a[l]$.
  2. Swap the value of $a[k]$ with that of $a[l]$.
  3. Reverse the sequence from $a[k + 1]$ up to and including the final element $a[n]$.

The reason why we search from right to left, because that's the first place to decrease the value of permutation.

If we get to the beginning, that means what we deal is the minimum permutation.

Otherwise, if we get the largest $k$ such that $a[k] &lt; a[k+1]$, that means:

  • $a[k+1]$ to $a[n]$ is in decreasing order
  • $a[k]$ is also one element in the range from $a[n]$ to $a[k]$.

These mean we've get the last permutation when we fix {$a[1], a[2], ..., a[k]$}.

So the next permutation should begin with {$a[1], a[2], ..., a[k-1], a[k']$}, where $a[k']$ is the next larger element than $a[k]$. After that, we start our permutation from $$a[1], a[2], ..., a[k-1], a[k'], a[k+2], ..., a[n]$$, where $$a[1], a[2], ..., a[k-1], a[k']$$ is in increasing order.

So, in order to get the next permutation, first we need to find the next larger element than $a[k]$. The method we use is step 2:

Find the largest index $l$ greater than $k$ such that $a[k] &lt; a[l]$.

Then, we need to swap the two elements $a[k]$ and $a[l]$. Now:

  1. the next permutation's begining part {$a[1], a[2], ..., a[k]$} has been reached right places.
  1. the new $a[l]$ is just less than $a[l-1]$ and greater than $a[l+1]$. So, {$a[k+1], ..., a[n]$} is in decreasing order.

So, the last step, we need to reverse the remaining parts, {$a[k+1], ..., a[n]$}, to get the right next permutation.


The complete code is here

TerenceXie's Projects

apollo-11 icon apollo-11

Original Apollo 11 Guidance Computer (AGC) source code for the command and lunar modules.

blockchain_guide icon blockchain_guide

Introduce blockchain related technologies, from theory to practice with bitcoin, ethereum and hyperledger.

chinadns icon chinadns

Protect yourself against DNS poisoning in China.

concurrency icon concurrency

Java 并发编程知识梳理以及常见处理模式 features and patterns

convnetjs icon convnetjs

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.

coursera icon coursera

Script for downloading Coursera.org videos and naming them.

cs193p-spring-2016 icon cs193p-spring-2016

These are the lectures, slides, reading assignments, and problem sets for the 'Developing iOS 9 Apps with Swift' CS193p course offered at the Stanford School of Engineering and available on iTunes U.

dhulkeecoursera icon dhulkeecoursera

Dhulkee Coursera, a software intended to batch download videos from Coursera.org.

go-coco icon go-coco

go micro service framework [open source]

neno icon neno

使用svelte+tailwindcss 仿照 浮墨 写的pwa应用

old-immersive-translate icon old-immersive-translate

Let's experience immersive web translation, with bilingual simultaneous display and translation of only the important content. Think of the browser's reading mode, but in immersive translation.

prml icon prml

PRML algorithms implemented in Python

resist_oped icon resist_oped

🕵🏽‍♀️ Identifying the author behind New York Time’s op-ed from inside the Trump White House.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.