알고리즘
두개뽑아서 더하기
const solution = (numbers) =>{
let result = [];
numbers.sort();
numbers.forEach((element,idx,arr) => {
let tmpResult = [...result];
if(idx === 0 || arr[idx] !==arr[idx-1]){
for(let i = idx+1 ; i < arr.length ; i++){
const num = element+arr[i];
if(tmpResult.indexOf(num) === -1){
tmpResult.push(num);
}
}
tmpResult.sort((a,b)=>Number(a)-Number(b));
}
result = [...tmpResult]
});
return result
}
크레인 인형뽑기
const solution = (board, moves) =>{
const reverseBoard = [...board].reverse()
const basketWidth = reverseBoard[0].length;
const newBasket = [];
let result = 0;
const columnBoard = new Array(basketWidth);
for(let row in reverseBoard){
for(let i = 0 ; i < reverseBoard[row].length ; i++){
if(reverseBoard[row][i]!==0 && columnBoard[i] === undefined){
columnBoard[i] = [reverseBoard[row][i]];
}else if(reverseBoard[row][i]!==0){
columnBoard[i].push(reverseBoard[row][i]);
}
}
}
for(let val of moves){
if(columnBoard[val-1].length!== 0 && columnBoard[val-1][columnBoard[val-1].length-1]===newBasket[newBasket.length-1]){
columnBoard[val-1].pop();
newBasket.pop();
result +=2;
}else if(columnBoard[val-1].length!== 0){
newBasket.push(columnBoard[val-1].pop());
}
}
return result;
}