1. ๋ฌธ์ œ

๋‹จ ํ•œ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ–ˆ๋‹ค. 

์ฐธ์—ฌ ์„ ์ˆ˜์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant

์™„์ฃผํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด completion

์ฐธ๊ฐ€์ž ์ค‘์—๋Š” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

์ž…๋ ฅ์˜ˆ์ œ | participant ["leo", "kiki", "eden"], completion ["eden", "kiki"]

์ถœ๋ ฅ์˜ˆ์ œ | "leo"

 

 

2. ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ํ’€์—ˆ๋˜ ์ฝ”๋“œ

const solution = (participant, completion) => {
    participant.sort();
    completion.sort();
    for (let i = 0; i < participant.length; i++) {
        if (participant[i] !== completion[i])
            return participant[i];
    }
}

const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));

 

3. ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘ผ ์ฝ”๋“œ

const solution = (participant, completion) => {
    let answer = '';

    let map1 = new Map();
    let map2 = new Map();

    for (let x of participant) {
        if (map1.has(x)) { map1.set(x, map1.get(x) + 1) }
        else { map1.set(x, 1) }
    };

    for (let x of completion) {
        if (map2.has(x)) { map2.set(x, map2.get(x) + 1) }
        else { map2.set(x, 1) }
    };

    for (let [key, value] of map1) {
        if (!map2.has(key) || map2.get(key) !== value) {
            answer = key;
        }
    }
    return answer;
}

const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));

ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘ธ๋Š” ๊ฒŒ ํšจ์œจ์„ฑ์ด ๋” ์ข‹๋‹ค. 

ํ•ด์‹œ, ํˆฌํฌ์ธํ„ฐ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋Š” ๋ฌธ์ œ

 

1. ๋ฌธ์ œ

์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์—์„œ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ.

๋ถ€๋ถ„๋ฌธ์ž์—ด์€ ์—ฐ์†๋œ ๋ฌธ์ž์—ด์ด๋‹ค. 

์ž…๋ ฅ์˜ˆ์ œ | ๋ฌธ์ž์—ด bacaAacba, ๋ถ€๋ถ„ ๋ฌธ์ž์—ด abc

์ถœ๋ ฅ์˜ˆ์ œ | 3 

 

 

2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

์ฃผ์–ด์ง„ ๋ถ€๋ถ„๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€(๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length-1)๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ”๊พผ๋‹ค.

ํฌ์ธํ„ฐ ์ง€์ ์„ ์ฐ์–ด๋†“๊ณ  for๋ฌธ์œผ๋กœ ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length๋งŒํผ์˜ ์ž๋ฆฌ๋ฅผ ๋น„๊ตํ•œ๋‹ค.

์•„๋‚˜๊ทธ๋žจ์ด ์•„๋‹ ๊ฒฝ์šฐ ํฌ์ธํ„ฐ๋ฅผ ๋‹ค์Œ ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚ค๊ณ ,

์•„๋‚˜๊ทธ๋žจ์ผ ๊ฒฝ์šฐ answer๋ฅผ ์นด์šดํŒ…ํ•˜๊ณ  ๋‹ค์Œ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚จ๋‹ค. 

 

 

3. ํ’€์ด๋ฐฉ๋ฒ•

1) answer์— ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ… ํ•ด์ฃผ๊ธฐ์œ„ํ•ด์„œ 0์œผ๋กœ ์„ธํŒ…ํ•œ๋‹ค. 

let answer = 0;

 

2) ์ฐพ์•„์•ผํ•˜๋Š” ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์–ด์ง„ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ์•ŒํŒŒ๋ฒณ๊ณผ ๊ฐ’์„ ๊ฐ์ฒดํ˜•ํƒœ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค. map ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

let map1 = new Map();
let map2 = new Map();

for (let x of t) {
	if (map2.has(x)) {
    	map2.set(x, map2.get(x) + 1);
    } else {
    	map2.set(x, 1);
    }
}

 

3) ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ธฐ๋ณธ ์„ธํŒ…์„ ํ•ด์ค€๋‹ค. 

let len = t.length -1;

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ์•ž์— 2๊ฐœ์˜ ๋ฌธ์ž๋งŒ ๊ฐ์ฒด๋กœ ์„ธํŒ…ํ•œ๋‹ค.

length๋ฅผ ๋ถ€๋ฌธ๋ถ„์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ํ•˜์ง€์•Š๊ณ  -1์„ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š”

๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๊ฐ€ 3์ผ๋•Œ, 2๋Š” ๊ณ ์ •์œผ๋กœ ๊ณ„์† ๋‘๊ณ  ํ•œ์ž๋ฆฌ๋งŒ ๋ฐ”๊พธ๋ฉด์„œ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

for (let i = 0; i < len; i++) {
	if (map1.has(s[i])) {
    	map1.set(s[i], map1.get(s[i] + 1);
    } else {
    	map1.set(s[i], 1);
    }
}

 

4) for๋ฌธ์œผ๋กœ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ

1๊ฐœ ์ถ”๊ฐ€ํ•ด์„œ 3๊ฐœ์”ฉ ๋น„๊ตํ•˜๊ณ (๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ length) ์•„๋‚˜๊ทธ๋žจ์ด ์•„๋‹ ๊ฒฝ์šฐ ๋นผ์ฃผ๊ณ  ๋ฐ˜๋ณตํ•œ๋‹ค. 

๋น„๊ต ์ดํ›„์— ๋นผ์ฃผ๋Š” ์ž‘์—…์„ ํ•˜๊ธฐ์œ„ํ•ด์„œ ํˆฌ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ์ž‘์„ฑํ•ด์ค€๋‹ค.

let lt = 0;
rt๋Š” for ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ์„ ์–ธ
for (let rt = len; rt < s.length; rt++) {
    if (map1.has(s[rt])) {
      map1.set(s[rt], map1.get(s[rt]) + 1);
    } else {
      map1.set(s[rt], 1);
    }

	// ๋น„๊ต
    if (compareMaps(map1, map2)) {
      answer++;
    }
    
    // ๋น„๊ต ์ดํ›„์— ์™ผ์ชฝ ํฌ์ธํ„ฐ ๋นผ์ฃผ๋Š” ์ž‘์—… 
    map1.set(s[lt], map1.get(s[lt]) - 1); // ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค 1์ž‘๊ฒŒ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ์„ธํŒ…
    if (map1.get(s[lt]) === 0) {
      map1.delete(s[lt]);
    }
    lt++;
}

 

5) ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜ ์ž‘์„ฑ

๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ

key๊ฐ’์ด ๊ฐ™์€์ง€, value ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ

const compareMaps = (map1, map2) => {
	if (map1.size !== map2.size) {
    	return false;
    }
    for (let [key, value] of map1) {
    	if (!map2.has(key) || map2.get(key) !== value) {
        	return false;
        }
    }
    retrun true; // ๋ชจ๋‘ ์•„๋‹ˆ๋ฉด ๊ฐ™์€ ๊ฒƒ์œผ๋ฏ€๋กœ true ๋ฐ˜ํ™˜
}

 

 

4. ์ฝ”๋“œ

const solution = (s, t) => {
  // ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด ๊ฐœ์ˆ˜
  let answer = 0;

  let map1 = new Map(); // ๋ฌธ์ž์—ด์„ map ๊ฐ์ฒด๋กœ
  let map2 = new Map(); // ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ map ๊ฐ์ฒด๋กœ

  // ์ฐพ์•„์•ผํ•˜๋Š” ์—ฐ์†๋œ ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ map๊ฐ์ฒด๋กœ ๋งŒ๋“ฌ
  for (let x of t) {
    if (map2.has(x)) {
      map2.set(x, map2.get(x) + 1);
    } else {
      map2.set(x, 1);
    }
  }

  // ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํ•  ๋•Œ ๊ธธ์ด๊ฐ€ 3์ด๋ฉด 2๊ฐœ๋ฅผ ๋จผ์ € ์„ธํŒ…ํ•ด ๋†“๋Š” ๊ฒƒ
  let len = t.length - 1;

  for (let i = 0; i < len; i++) {
    if (map1.has(s[i])) {
      map1.set(s[i], map1.get(s[i]) + 1);
    } else {
      map1.set(s[i], 1);
    }
  }

  let lt = 0;

  // ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— rt๋Š” len๊ธธ์ด๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์„ธํŒ…
  // ์ถ”๊ฐ€ํ•ด์„œ 3๊ฐœ๋กœ ๋งž์ถฐ์ฃผ๊ณ 
  for (let rt = len; rt < s.length; rt++) {
    if (map1.has(s[rt])) {
      map1.set(s[rt], map1.get(s[rt]) + 1);
    } else {
      map1.set(s[rt], 1);
    }

    // 3๊ฐœ์ด๋‹ˆ๊นŒ ์ด์ œ ๋น„๊ต๊ฐ€๋Šฅ
    if (compareMaps(map1, map2)) {
      answer++;
    }

    // ๋น„๊ต ์ดํ›„์— ์™ผ์ชฝ ํฌ์ธํ„ฐ ๋นผ์ฃผ๋Š” ์ž‘์—… 
    map1.set(s[lt], map1.get(s[lt]) - 1); // ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค 1์ž‘๊ฒŒ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ์„ธํŒ…
    if (map1.get(s[lt]) === 0) {
      map1.delete(s[lt]);
    }
    lt++;
  }
  return answer;
}

const compareMaps = (map1, map2) => {
  if (map1.size !== map2.size) { // 1. ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ
    return false;
  }
  for (let [key, value] of map1) { // 2. key ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ
    if (!map2.has(key) || map2.get(key) !== value) { // 3. value ๊ฐ’์ด ๊ฐ™์€์ง€ ํ™•์ธ
      return false;
    }
  }
  return true;
}

const str1 = "bacaAacba";
const str2 = "abc";
console.log(solution(str1, str2));

 

๋ฌธ์ œ

5๋ช…์˜ ํ•™์ƒ์ด ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ํ•™์ƒ์ด ๋ฐ›์€ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๊ธฐ์ค€์— ๋”ฐ๋ผ ํ•™์ ์„ ๋ถ€์—ฌ

์กฐ๊ฑด : ์ž๊ธฐ์ž์‹ ์„ ํ‰๊ฐ€ํ•œ ์ ์ˆ˜๊ฐ€ ์œ ์ผํ•œ ์ตœ๊ณ ์  ๋˜๋Š” ์œ ์ผํ•œ ์ตœ์ €์ ์ด๋ผ๋ฉด, ๊ทธ ์ ์ˆ˜๋Š” ์ œ์™ธํ•˜๊ณ  ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. 

 

 

ํ’€์ด

ํ‰๊ท ์„ ๋จผ์ € ๊ตฌํ•˜๊ณ , ํ•™์ ์„ ๊ตฌํ•œ๋‹ค. ๊ฐ ํ–‰์— ํ‰๊ท ์„ ๊ตฌํ•ด์•ผ ํ•  ์ ์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. 

 

 

ํ†ต๊ณผํ•˜์ง€๋ชปํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let sum = 0;

  let sumArr = [];
  let scoreArr = [];
  let studentScoreArr = [];
  let len;

  let answer = '';

  // ๋ฐฐ์—ด ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    studentScoreArr = [];
    for (let j = 0; j < scores.length; j++) {
      studentScoreArr.push(scores[j][i]);
    }
    scoreArr.push(studentScoreArr);
  }

  // ์ ์ˆ˜ ๊ฐ€๊ณต
  for (let i = 0; i < scores.length; i++) {
    sum = 0;
    len = scoreArr[i].length;
    for (let j = 0; j < scores.length; j++) {
      let findIdx = scoreArr.indexOf(scoreArr[i]);
      let findCurrentIdx = scoreArr[i].indexOf(scoreArr[i][j]);

      if (findIdx === findCurrentIdx) {

        const myScore = scoreArr[i][j];
        const dubCheck = isDuplicate(scoreArr[i]);

        if (dubCheck) {
          if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
            let myIdx = scoreArr[i].indexOf(myScore);
            console.log(myIdx);
            scoreArr[i].splice(myIdx, 1, 0);
            len--;
          } else {
            sum += scoreArr[i][j];
          }
        } else if (Math.max(...scoreArr[i]) === myScore || Math.min(...scoreArr[i]) === myScore) {
          let myIdx = scoreArr[i].indexOf(myScore);
          scoreArr[i].splice(myIdx, 1, 0);
          len--;
        } else {
          sum += scoreArr[i][j]
        }
      } else {
        sum += scoreArr[i][j];
      }
    }
    console.log(len);
    sumArr.push(sum / len);
  }

  // ํ•™์  ๊ณ„์‚ฐ
  for (let i = 0; i < sumArr.length; i++) {
    if (sumArr[i] >= 90) {
      answer += 'A';
    } else if (90 > sumArr[i] && sumArr[i] >= 80) {
      answer += 'B';
    } else if (80 > sumArr[i] && sumArr[i] >= 70) {
      answer += 'C';
    } else if (70 > sumArr[i] && sumArr[i] >= 50) {
      answer += 'D';
    } else {
      answer += 'F';
    }
  }
  return answer;
}

const isDuplicate = (arr) => {
  const isDup = arr.some(function (x) {
    return arr.indexOf(x) !== arr.lastIndexOf(x);
  });
  return isDup;
}

๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ–ˆ๋Š”๋ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 3๊ฐœ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

 

 

 

 

ํ†ต๊ณผํ•œ ์ฝ”๋“œ

const solution = (scores) => {
  let answer = '';

  for (let i = 0; i < scores.length; i++) {
    let myScore = scores[i][i];
    let count = scores.length;

    let min = 101;
    let max = -1;
    let sum = 0;

    let flag = true;

    for (let j = 0; j < scores.length; j++) {
      let score = scores[j][i];

      if (i !== j && myScore === score) { // ๋‚ด์ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ณ  ๋™์ผํ•œ ์ ์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ 
        flag = false;
      }

      min = Math.min(min, score);
      max = Math.max(max, score);
      sum += score;
    }

    if (flag && (min === myScore || max === myScore)) { // ๋‚ด ์ ์ˆ˜์ผ ๋•Œ
      count--;
      sum -= myScore;
    }

    answer += findGrade(sum / count);
  }
  return answer;
}

const findGrade = (score) => {
  if (score >= 90) {
    return 'A';
  } else if (90 > score && score >= 80) {
    return 'B';
  } else if (80 > score && score >= 70) {
    return 'C';
  } else if (70 > score && score >= 50) {
    return 'D';
  } else {
    return 'F';
  }
}

๋ฌธ์ œ

์ˆซ์ž์˜ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๊ฐ€ ์˜๋‹จ์–ด๋กœ ๋ฐ”๋€Œ์–ด์กŒ๊ฑฐ๋‚˜, ํ˜น์€ ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ์ธ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. s๊ฐ€ ์˜๋ฏธํ•˜๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ.

์ž…๋ ฅ์˜ˆ์ œ | "one4seveneight"

์ถœ๋ ฅ์˜ˆ์ œ | 1478

 

์ฒ˜์Œ ์ฝ”๋“œ(์‹คํŒจ)

const solution = (s) => {
  const numStr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

  let answer = s;

  for (let i = 0; i < numStr.length; i++) {
    let arr = answer.split(numStr[i]);

    if (arr.length > 1) {
      let idx = numStr.indexOf(numStr[i]);
      arr.splice(1, 0, num[idx]);
      answer = arr.join(i);
    }
  }
  let answer2 = answer.split('');

  let result = answer2.reduce((acc, cur, idx, arr) => {
    if (arr.indexOf(cur) === idx) acc.push(cur);
    return acc;
  }, []);

  return Number(result.join(''));
}

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒ ๋ณด๊ณ  ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ํ•˜๋ฉด๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ํ•˜๊ณ ๋ณด๋‹ˆ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์— ์ค‘๋ณต์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ๋งˆ์ € ์—†์• ๋Š” ์ฝ”๋“œ๊ฐ€ ๋˜์—ˆ๋‹ค. ๋‹ค์‹œ ์ƒˆ๋กœ ์งœ์•ผํ–ˆ๋‹ค. 

 

 

๋‹ค๋ฅธํ’€์ด

const solution = (s) => {
  let answer = 0;

  s = s.replace(/one/g, '1');
  s = s.replace(/two/g, '2');
  s = s.replace(/three/g, '3');
  s = s.replace(/four/g, '4');
  s = s.replace(/five/g, '5');
  s = s.replace(/six/g, '6');
  s = s.replace(/seven/g, '7');
  s = s.replace(/eight/g, '8');
  s = s.replace(/nine/g, '9');
  s = s.replace(/zero/g, '0');

  console.log(s);
  return Number(s);
}

 

 

๋‹ค๋ฅธํ’€์ด

const solution = (s) => {
  let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];

  for (let i = 0; i < numbers.length; i++) {
    let arr = s.split(numbers[i]);
    s = arr.join(i);
  }

  return Number(s);
}

๋ฌธ์ œ

๊ฐ™์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์€ ๊ฐ™์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋‹ค. 

์ตœ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ฐ€์ง€๊ธธ ์›ํ•œ๋‹ค. 

์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ํฌํ•จํ•ด์„œ N/2๋งˆ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 

 

N๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,

N/2๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘

๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฌ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„

ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๋ฅผ return

 

์ž์—ฐ์ˆ˜

์–‘์˜ ์ •์ˆ˜, ์‚ฌ๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ๋•Œ ์“ฐ์ด๋Š” ์ˆ˜์—ฌ์„œ ๊ฐ€์žฅ '์ž์—ฐ์Šค๋Ÿฌ์šด ์ˆ˜' 

 

 

์ฝ”๋“œ

const solution = (nums) => {
  const selectNum = nums.length / 2;

  const possibleSelect = nums.reduce((acc, cur, idx, origin) => {
    if (origin.indexOf(cur) === idx) acc.push(cur);
    return acc;
  }, []);

  return possibleSelect.length >= selectNum ? selectNum : possibleSelect.length;
}

์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ๋จผ์ €ํ•ด์ฃผ๊ณ , ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

reduce๋ฅผ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” Set ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

 

๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด

function solution(nums) {
  const max = nums.length / 2;
  const arr = [...new Set(nums)];

  return arr.length > max ? max : arr.length
}

๋ฌธ์ œ

์–ด๋–ค ์ •์ˆ˜๋“ค์ด ์žˆ๋‹ค. ์ด ์ •์ˆ˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด์—์„œ, ์‹ค์ œ ์ •์ˆ˜๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ returnํ•ด๋ผ

 

์ˆ˜ํ•™๊ฐœ๋…

์ ˆ๋Œ“๊ฐ’(absolute value)

: ์–ด๋–ค ๊ฐ’์„ ์–‘์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•œ ์žฅ์น˜

 

์ •์ˆ˜

: ์–‘์˜์ •์ˆ˜, 0, ์Œ์˜ ์ •์ˆ˜์˜ ์ด์นญ

 

์ฝ”๋“œ

const solution = (absolutes, signs) => {
  let answer = absolutes.map((num, idx) => signs[idx] ? num : parseInt(-num)).reduce((a, b) => a + b);
  return answer;
}

๋ฌธ์ œ

์ •์ˆ˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด arr์˜ ํ‰๊ท ๊ฐ’์„ returnํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด๋ผ

 

์—๋Ÿฌ๋‚œ ์ฝ”๋“œ

const average = (arr) => {
  let answer = 0;

  for (let i of arr) {
    answer += i;
  }

  return answer/arr.length;
}

๋ฌธ์ œ์—์„œ ํ•จ์ˆ˜ solution์„ ์ž‘์„ฑํ•˜๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ ๋‚˜ํ˜ผ์ž ํ•จ์ˆ˜๋ช…์„ ๋”ฐ๋กœ ์ง€์–ด๋ฒ„๋ ธ๋‹ค....์•ˆ๋ ๋ฆฌ๊ฐ€ ์—†๋Š”๋ฐ ์™œ ์•ˆ๋˜์ง€ ์‹ถ์—ˆ๋„ค.. ์‚ฌ์†Œํ•œ ์‹ค์ˆ˜ ํ•˜์ง€์•Š๋„๋ก ํ•˜์ž.

 

 

์ฝ”๋“œ

const solution = (arr) => {
  let answer = 0;

  for (let i of arr) {
    answer += i;
  }

  return answer/arr.length;
}

์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ -> ์กฐํ•ฉ์„ ์ด์šฉ

 

4Combination3 = 4C3

4๊ฐœ ์ค‘์— 3๊ฐœ์”ฉ ์„ ํƒํ•  ๋•Œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•œ๋‹ค. ์ด ๋•Œ, ์กฐํ•ฉ์˜ ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†๋‹ค. ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๊ฐ™์€ ๊ตฌ์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ์กฐํ•ฉ์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. 

 

์†Œ์ˆ˜ 

: 1๊ณผ ์ž๊ธฐ ์ž์‹ ๋งŒ์„ ์•ฝ์ˆ˜๋กœ ๊ฐ–๋Š” ์ˆ˜, ์ž์‹ ๋ณด๋‹ค ์ž‘์€ ๋‘ ๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋ฅผ ๊ณฑํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” 1๋ณด๋‹ค ํฐ ์ž์—ฐ์ˆ˜

 

์ฒ˜์Œ ์‹œ๋„ํ•œ ๋ฐฉ๋ฒ•

์กฐํ•ฉ์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋ฐฐ์—ด๋กœ ์ฐพ์•„์„œ ๋‹ค ๋”ํ•œ๋‹ค์Œ์— ์†Œ์ˆ˜ํŒ๋ณ„ํ•  ์ƒ๊ฐ์ด์—ˆ๋‹ค. -> ์‹คํŒจ

 

์กฐํ•ฉ์„ ์ด์šฉํ•˜๊ธฐ

์„œ๋กœ ๋‹ค๋ฅธ n๊ฐœ์˜ ๋ฌผ๊ฑด์—์„œ ์ˆœ์„œ๋ฅผ ์ƒ๊ฐํ•˜์ง€์•Š๊ณ  r๊ฐœ๋ฅผ ํƒํ•  ๋•Œ, n๊ฐœ์—์„œ r๋ฅผ ํƒํ•˜๋Š” ์กฐํ•ฉ -> nCr

5๊ฐœ ์ค‘์—์„œ 3๊ฐœ๋ฅผ ๋”ํ•˜๋Š” ์กฐํ•ฉ 5C3 = 5*4*3 /3*2*1 = 10๊ฐ€์ง€

 

๋ฐฐ์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๊ณ ์ •ํ•˜๋ฉด์„œ ๋’ค์˜ ๋‚˜๋จธ์ง€ ๋ฐฐ์—ด์— ๋Œ€ํ•ด์„œ ์กฐํ•ฉ์„ ๊ตฌํ•œ๋‹ค. 

 

 

 

์ฝ”๋“œ

const solution = (nums) => {
  let answer = 0;
  const len = nums.length;

  for (let i = 0; i < len; i++) {
    for (let j = i + 1; j < len; j++) {
      for (let k = j + 1; k < len; k++) {
        const sum = nums[i] + nums[j] + nums[k];
        if (isPrime(sum)) answer++;
      }
    }
  }
  return answer;
}

const isPrime = (sum) => {
  for (let i = 2; i < sum; i++)
    if (sum % i === 0) return false;
  return true;
}

 

+ Recent posts