Why not std::next_permutation() in C++?

  • 0

    The simplest C++ code is obviously

    class Solution {
        void nextPermutation(vector<int>& nums) {
            next_permutation(nums.begin(), nums.end());

    which even does the correct thing if there is no next permutation and is guaranteed to be the most efficient (at most nums.size()/2 swaps, see cppreference), so not using it would be just stupid,

  • 1

    No, using it would be just stupid. What do you think your interviewer is going to think of you when you do that?

  • 0

    @StefanPochmann There is perhaps a difference between an interview situation and the real world of coding. I was referring to the latter.

  • 0

    Well, as the LeetCode front page says:

    LeetCode OJ is a platform for preparing technical coding interviews.

    And yes, there's definitely a difference between interview and real world. In an interview, it's clear that the interviewer wants us to implement the function, not use it. Your above answer would be inappropriate and the interviewer would likely think you're unprepared/stupid. I'd maybe mention it like "So it's like the library function?", which shows them that I know that one and also quickly checks whether I understood the task, which is good anyway. In the real world on the other hand, it would be very appropriate to point out the existing function and to ask whether that's an option and if not, then why not. I mean, maybe the boss actually simply doesn't know that it's already there.

    At least that's the way I see it all. Anyway, I hope this answers your title question "Why not std::next_permutation() in C++?".

  • 0

    Oh, seeing your title again, I just remembered: It's not like nobody here used std::next_permutation. Actually even I myself did :-)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.