Last active
January 20, 2021 09:25
-
-
Save skawnkk/9c36f5e5ed15d5aef2ff9cb48906b0b5 to your computer and use it in GitHub Desktop.
WEEK3 알고리즘
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
//크레인 인형뽑기 | |
function solution(board, moves) { | |
var answer = 0; | |
let basket = [0]; //처음에 비교할 기본값을 미리 넣어준다. | |
while (moves.length > 0) { | |
let move = moves.shift(); | |
for (let line = 0; line < board.length; line++) { | |
if (board[line][move - 1] !== 0) { | |
basket.push(board[line][move - 1]) | |
board[line].splice(move - 1, 1, 0) | |
//basket안에서 중복된 경우 | |
if (basket[basket.length - 2] === basket[basket.length - 1]) { | |
basket.splice(-2, 2); | |
answer += 2; | |
} | |
break; //다음 move로 넘어가기 | |
} | |
} | |
} | |
return answer; | |
} | |
//---------------------------------------------------------------------- | |
function test() { | |
let board = [ | |
[0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 3], | |
[0, 2, 5, 0, 1], | |
[4, 2, 4, 4, 2], | |
[3, 5, 1, 3, 1] | |
]; | |
let moves = [1, 5, 3, 5, 1, 2, 1, 4]; | |
console.log("result:", solution(board, moves)); | |
} | |
test(); | |
function test2() { | |
let board = [ | |
[0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 3], | |
[0, 2, 5, 0, 1], | |
[4, 2, 4, 4, 2], | |
[3, 5, 1, 3, 1] | |
] | |
let moves = [1, 5, 3, 5, 1, 2, 1, 4]; | |
console.log("result:", solution(board, moves)); | |
} | |
test2(); |
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
function solution(array, commands) { | |
var answer = []; | |
for (let command of commands) { | |
let sorted = array.slice(command[0] - 1, command[1]).sort((a, b) => a - b); | |
answer.push(sorted[command[2] - 1]) | |
} | |
return answer; | |
} | |
//map 활용해보기 -------------------------------------------------------------------------- | |
function solution(array, commands) { | |
return commands.map(command => { | |
return array.slice(command[0] - 1, command[1]).sort((a, b) => a - b)[command[2] - 1]; | |
}) | |
} | |
//----------------------------------------------------------- | |
function test() { | |
const array = [1, 5, 2, 6, 3, 7, 4]; | |
const commands = [ | |
[2, 5, 3], | |
[4, 4, 1], | |
[1, 7, 3] | |
] | |
return solution(array, commands); | |
} | |
console.log(test()) |
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
function solution(answers) { | |
var answer = []; | |
let everybody = [ | |
[1, 2, 3, 4, 5], | |
[2, 1, 2, 3, 2, 4, 2, 5], | |
[3, 3, 1, 1, 2, 2, 4, 4, 5, 5] | |
]; | |
let score = []; | |
for (let one of everybody) { | |
let count = 0; | |
for (let i = 0; i < answers.length; i++) { | |
if (one[i % one.length] === answers[i]) count++; | |
} | |
score.push(count); | |
} | |
let maxNum = Math.max.apply(null, score); | |
for (let el in score) { | |
if (score[el] === maxNum) { | |
answer.push(parseInt(el) + 1); | |
} | |
} | |
return answer; | |
} |
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
//12930 | |
function solution(s) { | |
var answer = ''; | |
let index = 0; | |
for (let i = 0; i < s.length; i++) { | |
if (s[i] === ' ') { | |
index = 0; | |
answer += ' '; | |
} else { | |
if (index % 2 === 0) { | |
answer += s[i].toUpperCase() | |
index++; | |
} else { | |
answer += s[i].toLowerCase(); | |
index++; | |
} | |
} | |
} | |
return answer; | |
} | |
//----------------------------------------------------- | |
function test() { | |
let s = "try hello world"; //TrY HeLlO WoRlD | |
console.log(s.length) | |
return solution(s); | |
} | |
console.log(test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment