νλ‘κ·Έλλ¨Έμ€ λ¬Έμ νμ΄ - μμ£Όνμ§ λͺ»ν μ μ / λ μ μ μ¬μ΄μ ν© / νκ· κ΅¬νκΈ° / μ§μμ νμ / 체μ‘볡
Math.abs() / filter()λ₯Ό μ΄μ©ν΄ ν©μ§ν©, κ΅μ§ν©, μ°¨μ§ν© ꡬνλ λ²
π μλ μ μμ±νλ νμ΄
function solution(participant, completion) {
for (let i = 0; i < completion.length; i++) { // μν
var index = participant.indexOf(completion[i]); // μ ν κ²μ
participant.splice(index, 1); // μμ
}
var answer = participant.toString();
return answer;
}
π μμ ν νμ΄
function solution(participant, completion) {
var answer = '';
participant.sort();
completion.sort();
for (let i = 0; i < participant.length; i++) {
if (participant[i] !== completion[i]) {
answer = participant[i];
break;
}
}
return answer;
}
π λ€λ₯Έ μ¬λμ νμ΄ 1
function solution (participant, completion) {
participant.sort();
completion.sort();
while (participant.length) {
let participant_pop = participant.pop();
if (participant_pop !== completion.pop()) {
return participant_pop;
}
}
}
π λ€λ₯Έ μ¬λμ νμ΄ 2
forEach()
μ ν΄μ ν
μ΄λΈ(Hash Table)
μ΄μ©
function solution(participant, completion) {
let hash = [];
participant.forEach(name => {
hash[name] = hash[name] ? hash[name] + 1 : 1
})
completion.forEach(name => {
hash[name] = hash[name] - 1
})
for (var name in hash) {
if (hash[name] >= 1) {
return name
}
}
}
π μ²μ μμ±ν νμ΄
function solution(a, b) {
const nums = [];
if (a < b) {
for (let i = a; i <= b; i++) {
nums.push(i);
}
} else if (a > b) {
for (let i = b; i <= a; i++) {
nums.push(i);
}
} else {
return a;
}
return nums.reduce((prev, curr) => prev + curr, 0);
}
π μμ ν νμ΄
function solution(a, b) {
if (a === b) {
return a;
}
let sum = Math.min(a, b);
for (let i = Math.min(a, b) + 1; i <= Math.max(a, b); i++) {
sum = sum + i;
}
return sum;
}
π λ€λ₯Έ μ¬λμ νμ΄
Math.abs()
μ΄μ©
μ£Όμ΄μ§ μ«μμ μ λκ°
μ λ°ννλ€
ex) console.log(Math.abs(4 - 9)); // 5
function solution(a, b) {
return (a + b) * (Math.abs(b - a) + 1) / 2;
}
π λ΄κ° μμ±ν νμ΄
function solution(arr) {
return arr.reduce((prev, curr) => prev + curr, 0) / arr.length;
}
π λ΄κ° μμ±ν νμ΄
function solution(num) {
if (num % 2 === 0) {
return 'Even';
} else {
return 'Odd';
}
}
π λ€λ₯Έ μ¬λμ νμ΄
function solution(num) {
return num % 2 === 0 ? 'Even' : 'Odd';
}
π μ²μ μμ±ν νμ΄
κ°κ° lostμ reserveμμ 체μ‘볡 μ¬λ²μ κ°μ§κ³ μμΌλ©΄μ λμμ ν λ²μ λλλΉν νμλ€ μ¦, lostμ reserveμ κ΅μ§ν©μ μ κ±°ν ν, lostμ reserve λ°°μ΄μ λ΄κΈ΄ μμ(νμλ€ λ²νΈ)λ€μ 쑰건μ κ³ λ €ν΄ λΉκ΅νκ³ μ νλ€.
ν μ€νΈ μΌμ΄μ€λ ν΅κ³ΌνμΌλ, μ€μ μ±μ μ μ νλ ν μ€νΈμμ 55μ λ§μ λ΅μ...μ΄μλλ° λ€μ νλκΉ 85μ λμ¨λ€. λμ§? μ΄μ¨λ νλ¦° νμ΄λ€.
function solution(n, lost, reserve) {
var answer = 0;
const lostCount = lost.length;
const reserveCount = reserve.length;
const intersection = [];
for (let i = 0; i < lost.length; i++) {
for (let j = 0; j < reserve.length; j++) {
if (lost[i] === reserve[j]) {
intersection.push(lost[i]);
lost.splice(i, 1);
reserve.splice(j, 1);
break;
}
}
}
let lentCount = 0;
for (let i = 0; i < reserve.length; i++) {
for (let j = 0; j < lost.length; j++) {
if (reserve[i] === lost[j] - 1 || reserve[i] === lost[j] + 1) {
lentCount++;
lost.splice(j , 1);
break;
}
}
}
const rest = n - (lostCount + reserveCount - intersection.length);
answer = rest + reserveCount + lentCount;
return answer;
}
π λ λ²μ§Έ μμ±ν νμ΄
ν μ€νΈ μΌμ΄μ€λ₯Ό νλμ© μΆκ°ν΄μ μ€λ₯λ₯Ό μ°Ύμλ€. κ΅μ§ν©μ ꡬνλ for 루νμμ spliceλ₯Ό μ΄μ©ν΄ lost λ°°μ΄μ μ€κ° μμλ₯Ό κ³μ μ κ±°νλ λΆλΆμ΄ λ¬Έμ μλ€. λ°κΉ₯ 루νκ° λ€μ 루νλ₯Ό λ λ iλ μ΄μ μ λΉν΄ λκ°μ΄ 1μ΄ μ¦κ°νμ§λ§, μ€μ lost λ°°μ΄μμλ μ΄μ μμμ λ€μ μμκ° μλλΌ λ€λ€μ μμκ° μ‘νκ² λλ€. μ΄λ μ΄μ 루νμμ spliceμ μν΄ κ·Έ 루νμμ μ‘νλ μμλ₯Ό μ κ±°νκΈ° λλ¬Έμ΄λ€.
λν, spliceλ‘ μΈν΄ lost λ°°μ΄κ³Ό reserve λ°°μ΄μ μ κ±°νμ΄λ lentCountλ₯Ό μ¦κ°μν€λ μ€μ²© 루νμμ μ¬μ©λλ lost λ°°μ΄κ³Ό reserve λ°°μ΄μ λΉμ°νκ²λ solution ν¨μμ 맀κ°λ³μλ‘ μ λ¬λ λ°°μ΄λ€ κ·Έλλ‘μκΈ° λλ¬Έμ μλνλ λλ‘ λΉκ΅κ° λμ§ μμλ€.
κ°κ° lost λ°°μ΄κ³Ό reserve λ°°μ΄μμ κ΅μ§ν©μ μ κ±°νλ λ° μ€μ²© 루νλ₯Ό μ¬μ©νλκΉ λΈλ‘ μ€μ½ν μμμλ§ λ³νκ° μ μ©λΌμ λ€μ μ½λμμ μ΄μ΄μ μΈ μκ° μμλ€. λ€λ₯Έ λ°©λ²μ΄ μλ κ²μν΄λ³΄λ€ μ μΈκ³ λ°κ²¬. [javascript] λ°°μ΄ κ° μ€λ³΅μ κ±°, λ°°μ΄ν©μΉκΈ° μ°Έκ³
λ§μ μ€ μμλλ° 90μ μ΄ λμλ€. λκ° λ¬Έμ μΌκΉ.
function solution(n, lost, reserve) {
var answer = 0;
const lostCount = lost.length;
const reserveCount = reserve.length;
// lost + reserve
const sum = lost.concat(reserve);
// κ΅μ§ν©
const intersection = sum.filter((item, index) => sum.indexOf(item) !== index);
// μ°¨μ§ν© : lost - (lost β© reserve)
const restLost = lost.filter(x => !intersection.includes(x));
// μ°¨μ§ν© : reserve - (lost β© reserve)
const restReserve = reserve.filter(x => !intersection.includes(x));
// 체μ‘볡μ λΉλ¦° νμ μ
let lentCount = 0;
for (let i = 0; i < restReserve.length; i++) {
for (let j = 0; j < restLost.length; j++) {
if (restReserve[i] === restLost[j] - 1 || restReserve[i] === restLost[j] + 1) {
lentCount++;
restLost.splice(j , 1);
break;
}
}
}
// λ³ΈμΈ μ²΄μ‘λ³΅λ§ κ°μ Έμμ λ³ΈμΈμ΄ μ
λ νμ μ (λλx, μ¬λ³x)
const rest = n - (lostCount + reserveCount - intersection.length);
// 'μ 체 νμ μ n - 체μ‘볡μ λͺ» λΉλ¦° νμ μ'μ κ°λ€
answer = rest + reserveCount + lentCount;
return answer;
}
π μΈ λ²μ§Έ μμ±ν νμ΄
맨 μ²μμ lost λ°°μ΄κ³Ό reserve λ°°μ΄μ μ€λ¦μ°¨μμΌλ‘ μ λ ¬ν΄μ£Όλ μ½λλ₯Ό μΆκ°νλ€.
λλμ΄ 100μ μ΄λ€.
function solution(n, lost, reserve) {
lost.sort((a, b) => a - b);
reserve.sort((a, b) => a - b);
const sum = lost.concat(reserve);
const intersection = sum.filter((item, index) => sum.indexOf(item) !== index);
const restLost = lost.filter(x => !intersection.includes(x));
const restReserve = reserve.filter(x => !intersection.includes(x));
const lostCount = lost.length;
const reserveCount = reserve.length;
let lentCount = 0;
for (let i = 0; i < restReserve.length; i++) {
for (let j = 0; j < restLost.length; j++) {
if (restReserve[i] === restLost[j] - 1 || restReserve[i] === restLost[j] + 1) {
lentCount++;
restLost.splice(j , 1);
break;
}
}
}
const rest = n - (lostCount + reserveCount - intersection.length);
return rest + reserveCount + lentCount;
}