Created
December 31, 2019 20:12
remove k digits - Dec 29, 2019 10:00 PM - mock interview as an interviewer, the interviewee finished boot camp in New York.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const removeKdigits = (nums, k) => { | |
if(!nums.length) return; | |
else if(k === 0) return Number(nums.join('')); | |
let lowest = Infinity; | |
const helper = (idx, strArr, r) => { | |
if(r === k) { | |
strArr = strArr.concat(nums.slice(idx)); | |
const n = Number(strArr.join('')) | |
lowest = lowest > n ? n : lowest; /* compare to lowest one */ | |
}; | |
for(let i=idx; i<nums.length; i+=1) { | |
helper(i + 1, [...strArr], r + 1); | |
strArr.push(nums[i]); | |
}; | |
}; | |
helper(0, [], 0); | |
return lowest; | |
}; | |
console.log(removeKdigits("1432219", 3)); | |
console.log(removeKdigits("10", 2)); | |
console.log(removeKdigits("10200", 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment