Vradhit
Vradhit2y ago

Vradhit – 14-13 Jul 30

can anyone please tell me why this telling maximum callstack exceeded in this snippet of leetcode
var permute = function(nums) {


let res = [];
let temp = [];
backtrack(nums,temp,0);
return res;

function backtrack(nums,temp,start)
{
if(start===nums.length){
res.push([...temp])
return;

}

for(let i=0;i<nums.length;i++){

temp.push(nums[i]);
backtrack(nums,temp,i+1);
temp.pop();

}



}


};
var permute = function(nums) {


let res = [];
let temp = [];
backtrack(nums,temp,0);
return res;

function backtrack(nums,temp,start)
{
if(start===nums.length){
res.push([...temp])
return;

}

for(let i=0;i<nums.length;i++){

temp.push(nums[i]);
backtrack(nums,temp,i+1);
temp.pop();

}



}


};
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Vradhit
Vradhit2y ago
I changed i to start now the error is not there but i am not getting permuation results Wrong Answer Runtime: 91 ms Your input [1,2,3] Output [[1,2,3],[1,3],[2,3],[3]] Expected [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] /** * @param {number[]} nums * @return {number[][]} */
var permute = function(nums) {


let res = [];
let temp = [];
backtrack(nums,temp,0);
return res;

function backtrack(nums,temp,start)
{
if(start===nums.length){
res.push([...temp])
return;

}

for(let i=start;i<nums.length;i++){

temp.push(nums[i]);
backtrack(nums,temp,i+1);
temp.pop();

}



}


};
var permute = function(nums) {


let res = [];
let temp = [];
backtrack(nums,temp,0);
return res;

function backtrack(nums,temp,start)
{
if(start===nums.length){
res.push([...temp])
return;

}

for(let i=start;i<nums.length;i++){

temp.push(nums[i]);
backtrack(nums,temp,i+1);
temp.pop();

}



}


};
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Vradhit
Vradhit2y ago
actually that internally backtrack works for loop
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1002942005351035001