Introduction
It is my first Sunday 10:00 am mock interview. I like to write a short case study on this mock interview.
Case study
I will add more content later.
This file contains hidden or 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)); |
No comments:
Post a Comment