I recently interviewed for Shopflo, and here is all the information that I can provide.
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ] Example 2:
Input: candidates = [2,5,2,1,2], target = 5 Output: [ [1,2,2], [5] ]
Constraints:
1 <= candidates.length <= 100 1 <= candidates[i] <= 50 1 <= target <= 30
var combinationSum2 = function(candidates, target) {
candidates.sort((a, b)=> a-b);
let ans = [];
function helper(sum, idx, arr){
if(sum===0){
ans.push([...arr]);
return;
}
if(sum<0) return;
for(let i = idx;i<candidates.length;i++){
if(i!==idx && candidates[i]===candidates[i-1]) continue;
helper(sum-candidates[i], i+1, [...arr, candidates[i]])
}
}
helper(target, 0, []);
return ans;
};
const candidates = [10,1,2,7,6,1,5];
const target = 8;
console.log(combinationSum2(candidates, target))
const join = (a,b,c) => {
return ${a}_${b}_${c} }
const curriedJoin = curry(join)
console.log(curriedJoin(1)(2,3)); //1_2_3
console.log(curriedJoin(1,2)(3)); //1_2_3
console.log(curriedJoin(1)(2)()()(3)); //1_2_3 console.log(curriedJoin(1,2,3)); //1_2_3
// Define the curry function
function curry(fn) {
// The curried function will return a new function to collect the arguments
return function curried(...args) {
// If enough arguments are collected, call the original function
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
// Otherwise, return a new function to collect more arguments
return function(...nextArgs) {
return curried.apply(this, args.concat(nextArgs));
};
}
};
}
// Define the join function
const join = (a, b, c) => {
return `${a}_${b}_${c}`;
}
// Curry the join function
const curriedJoin = curry(join);
// Test cases
console.log(curriedJoin(1)(2, 3)); // 1_2_3
console.log(curriedJoin(1, 2)(3)); // 1_2_3
console.log(curriedJoin(1)(2)()()(3)); // 1_2_3
console.log(curriedJoin(1, 2, 3)); // 1_2_3
Thank you for Reading!