๐Ÿ˜‚ WeCode Codekata 2์ฃผ์ฐจ ์ •๋ฆฌ!

solmiiยท2020๋…„ 6์›” 19์ผ
1

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
2/3
post-thumbnail

์œ„์ฝ”๋“œ ํŽ˜์–ด ์ฝ”๋“œ์นดํƒ€ 2์ฃผ์ฐจ!!
๋„˜๋‚˜๋ฆฌ ์–ด๋ ต๋‹ค.....๐Ÿ˜…
5๋ฌธ์ œ์ค‘์— 2๋ฌธ์ œ๋Š” ๋ชปํ’€์—ˆ๋‹ค....์šฐํ•˜ํ•ซ......


Day 1

ย ย  ๋‚ด ํ’€์ด ย ย 

function romanToNum(s) {
  let romanNum = {
    I:1,
    V:5,
    X:10,
    L:50,
    C:100,
    D:500,
    M:1000
  }
  let result = 0;
  
  let sIdx = s.split("");
  
  for(let i = 0; i < s.length; i++) {
    if(romanNum[sIdx[i]] < romanNum[sIdx[i+1]]){
      result += romanNum[sIdx[i]] - (romanNum[sIdx[i]] * 2);
    } else {
      result += romanNum[sIdx[i]];
    }
  }
  return result;
}

romanToNum('XXIV'); // 24

์•ฝ 1์‹œ๊ฐ„ ๋ฐ˜...? ๊ฑธ๋ฆฐ ๋ฌธ์ œ๐Ÿ˜‚

์ผ๋‹จ romanNum ์„ object๋กœ ๋‹ด์•„๋†“๊ณ , result ๋ณ€์ˆ˜๋„ ์ง€์ •ํ•ด ์ค€๋‹ค.

๊ทธ๋‹ค์Œ์— for ๋ฌธ ๋Œ๋ฆฌ๊ธฐ ์ „์— s๋ฅผ ๊ธ€์ž๋งˆ๋‹ค split ์œผ๋กœ ์ชผ๊ฐœ๋†“๊ณ , ์ด๋ฅผ sIdx ์— ๋„ฃ์–ด๋†“๋Š”๋‹ค!

๋งŒ์•ฝ ํ˜„์žฌ s ์˜ ์ธ์ž๋กœ 'XXIV' ๊ฐ€ ๋“ค์–ด์™€๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ๋กœ ์„ค๋ช… ๊ธ”๊ธ”

  • ์šฐ์„  ์ฒซ๋ฒˆ์งธ if : s์˜ i ๊ฐ€ i+1 ๋ณด๋‹ค ํฐ ์ˆ˜์ผ ๋•Œ, ์ฆ‰, X์˜ value์ธ 10์ด X์˜ value์ธ 10๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„๋•Œ โ‡’ result ์— 10์„ ๋”ํ•œ๋‹ค!

XI ๋‚˜, IV์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ ๊ฒฝ์šฐ์ผ๋•Œ๋Š” else ์กฐ๊ฑด์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค!

  • ๋‘๋ฒˆ์งธ else if : s์˜ i ๊ฐ€ i+1 ๋ณด๋‹ค ํฐ ์ˆ˜์ผ ๋•Œ, ์ฆ‰, I์˜ value์ธ 1์ด V์˜ value์ธ 5๋ณด๋‹ค ์ž‘์„๋•Œ โ‡’ result ์— ( 1 - ( 1 * 2) ) = -1 ๋ฅผ ๋”ํ•œ๋‹ค!

ํ•œ๊ตญ๋‹˜์ด ํ‘ธ์‹  ๋ฐฉ๋ฒ•์„ ๋“ฃ๊ณ  ์ ์šฉํ•œ๊ฑด๋ฐ, ์ด๋ ‡๊ฒŒ ์ผ์˜ ์ž๋ฆฌ๋Š” -2, ์‹ญ์˜ ์ž๋ฆฌ๋Š” -20, ๋ฐฑ์˜ ์ž๋ฆฌ๋Š” -200์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค!
์™œ๋ƒ๋ฉด IV = 4, VI = 6 ์ด๋ผ ํ•ญ์ƒ 2์˜ ์ฐจ์ด๊ฐ€ ๋‚˜๊ณ , ์ด๊ฑด ๋‹ค๋ฅธ ์‹ญ์˜ ์ž๋ฆฌ, ๋ฐฑ์˜ ์ž๋ฆฌ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๊ธฐ ๋•Œ๋ฌธ!
๊ทธ๋ž˜์„œ ๋‚˜์ค‘์— V ๊ฐ’ ๋”ํ•  ๋•Œ +5 ๋ฅผ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ตญ -1 + 5 = 4 ํ•ด์„œ IV๋Š” 4๊ฐ€ ๋œ๋‹ค!

  • ์„ธ๋ฒˆ์งธ else : ์œ„ ๊ฒฝ์šฐ๊ฐ€ ๋ชจ๋‘ ์•„๋‹๋•Œ!! ์ฆ‰, i ๊ฐ€ ์ œ์ผ ๋งˆ์ง€๋ง‰ index์ผ๋•Œ ์กฐ๊ฑด์„ ํ•˜๋‚˜ ๋” ๋„ฃ์–ด์คฌ๋‹ค!!

์ฒซ๋ฒˆ์งธ ์กฐ๊ฑด์— || ์œผ๋กœ ๋„ฃ์–ด์ค„๊นŒ ์‹ถ์–ด์„œ i === s.length[i] ?ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹๋ญ ์ด๋Ÿฐ์‹์œผ๋กœ ๋„ฃ์–ด๋ดค๋Š”๋ฐ ํƒ๋„ ์—†์—ˆ์Œใ…Ž

๊ด‘์˜ค๋‹˜์ด ๋งˆ์ง€๋ง‰์— else ๋กœ ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ๋ฅผ ํ‰์ณ์„œ ๋„ฃ๋Š”๊ฒŒ ์–ด๋– ๋ƒ๊ณ  ํ•˜์…”์„œ ๋ฐ”๋กœ ํ•ด๋ดค๋”๋‹ˆ ์˜ค1!??!? ๋œ๋‹น!!!๐Ÿ˜

ย ย  model solution ย ย 

function romanToNum(s) {
  let matching = {
      I: 1,
      V: 5,
      X: 10,
      L: 50,
      C: 100,
      D: 500,
      M: 1000
  }
  
  let strArr = s.split('');
  let numArr = strArr.map(el => matching[el]);
  let sum = 0;
  
  for (let i = 0; i < numArr.length; i++) {
      if (numArr[i] < numArr[i+1]) {
          sum += (numArr[i+1] - numArr[i]);
          i++;
      } else {
          sum += numArr[i];
      }
  }
  
  return sum;
}

model solution์€ ์ฒ˜์Œ ๊ด‘์˜ค๋‹˜์ด ์ƒ๊ฐํ•˜์‹  ๋ฐฉ๋ฒ•์ด๋ž‘ ๋น„์Šทํ•œ๋ฐ....
์กฐ๊ฑด์€ ๋‚˜๋ž‘ ๊ฐ™๊ณ ! ๊ฒฐ๊ณผ๊ฐ™์„ ๋”ํ•  ๋•Œ (numArr[i+1] - numArr[i]) ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋”ํ•ด๋ฒ„๋ฆฐ๋‹ค!
๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€, ๋‹ค์Œ ์กฐ๊ฑด์œผ๋กœ ๋„˜์–ด๊ฐ”์„๋•Œ ์ด๋ฏธ ๊ณ„์‚ฐ์ด ์™„๋ฃŒ๋œ numArr[i+1] ๋กœ ๋˜ ๊ณ„์‚ฐํ•ด์„œ (numArr[i+2] - numArr[i+1])์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค!

๊ทธ๋ž˜์„œ model solution์—์„œ๋Š” ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ for๋ฌธ์œผ๋กœ ๋ฐ”๋กœ ๋„˜๊ธฐ๊ณ , ๋งŒ์•ฝ ์กฐ๊ฑด์— ๋งž์„ ๊ฒฝ์šฐ์—๋Š” i ์˜ ๊ฐ’์„ ์˜๋„์ ์œผ๋กœ ์˜ฌ๋ ค์„œ, if ๋ฌธ์—์„œ ์กฐ๊ฑด์— ๋งž์„ ๊ฒฝ์šฐ 1๋ฒˆ + ๋‹ค์Œ for๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐˆ๋•Œ ํ•œ๋ฒˆ, ์ด +2 ๊ฐ€ ๋œ๋‹ค!


Day 2

ย ย  ๋‚ด ํ’€์ด ย ย 

let nums = [1];
function moreThanHalf(nums) {
    let idx = Math.ceil(nums.length / 2);
    let numArr = nums.sort();
    let result = numArr[idx];
      if(nums.length > idx) {
        return result;
      } else if(nums.length === 1) {
        return parseInt(nums);
      } else {
      return "๊ณผ๋ฐ˜์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค!"
    }
}
  moreThanHalf(nums);

์ผ๋‹จ ๊ณผ๋ฐ˜์ˆ˜๊ฐ€ ์ ˆ๋ฐ˜ ์ด์ƒ์ธ๊ฐ€ ์ดˆ๊ณผ์ธ๊ฐ€๋ฅผ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋„ค์ด๋ฒ„์— ๊ฒ€์ƒ‰ํ–ˆ๋”ฐ.....^p^....
๋ผ๊ณ  ํ•œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด nums Array์˜ ์š”์†Œ๋“ค์„ ๋‚ฎ์€ ์ˆซ์ž ๋ถ€ํ„ฐ ๋†’์€ ์ˆซ์ž ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ์— ๊ทธ ์ค‘๊ฐ„+1 ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋ฉด ๊ทธ๊ฒŒ ๋ฐ”๋กœ ๊ณผ๋ฐ˜์ˆ˜ ์•„๋ƒ?

์˜ค์˜ค ๋‚˜ ์ข€ ์ฒœ์žฌ์ธ๋“ฏ. ๋ฐ”๋กœ ํ•ด๋ดค๋‹ค!!! (์˜†์—์„œ ๊ด‘์˜ค๋‹˜์ด ์ž๊พธ count ๊ฐ€ ์–ด์ฉŒ๊ตฌ ๋ญ์‹œ๊ธฐ๊ฐ€ ์–ด์ฉŒ๊ตฌ๊ฑฐ๋ ธ๋‹ค๐Ÿ˜ค)

๋ฉ”์„œ๋“œ๋Š” ๊ตฌ๊ธ€๋ง์œผ๋กœ ์•Œ์•„๋ƒˆ๋Š”๋ฐ sort ๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ •๋ ฌํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค!

๋ฐฐ์—ด์˜ ์ค‘๊ฐ„+1 ์œ„์น˜์— ์žˆ๋Š” index๋Š” idx ๋ผ๋Š” ๋ณ€์ˆ˜์— Math.ceil(nums.length / 2) ์ด๋ผ๋Š” ์˜ฌ๋ฆผ ํ•จ์ˆ˜๋กœ ์ง€์ •ํ–ˆ๋‹ค.

๐Ÿคท๐Ÿปโ€โ™€๏ธ ์™œ?
์˜ˆ๋ฅผ๋“ค์–ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 10์ธ๊ฒฝ์šฐ, ์ค‘๊ฐ„ ์œ„์น˜๋Š” 5 , ๊ธธ์ด๊ฐ€ 11์ธ ๊ฒฝ์šฐ์—๋Š” ์ค‘๊ฐ„ ์œ„์น˜ 5.5 ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
์—ฌ๊ธฐ์„œ ์˜ฌ๋ฆผ์„ ํ•˜๋ฉด ์ „์ž๋Š” 6, ํ›„์ž๋„ 6 ์ด ๋œ๋‹ค.

๋ฐ”๋กœ ๋‚˜์˜ด!!! ๊ทผ๋ฐ ๋ฌธ์ œ๋Š” ๋ฐฐ์—ด์— ์š”์†Œ๊ฐ€ ํ•˜๋‚˜์ผ๋•Œ!!
ํ•˜๋‚˜๋ฉด, 100% ๋‹ˆ๊นŒ ๊ทธ ์š”์†Œ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค!

์–ด๋–ป๊ฒŒ ์งง๊ฒŒ ์“ธ๊นŒ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ.... ๊ฒฐ๊ตญ ๊ทธ๋ƒฅ else if ๋กœ ๋„ฃ์—ˆ์Œใ… ใ… ใ… 

์งง๊ฒŒ ๋ฆฌํŒฉํ† ๋ง ํ•˜๊ณ  ์‹ถ๋‹คใ… ใ…  ์•„์ด๋””์–ด ์žˆ๋Š” ๋ถ„ wecode 9๊ธฐ ๊ฐ•์†”๋ฏธํ•œํ…Œ ์„ค๋ช…ํ•ด์ฃผ์„ธ์—ฌ.........

์•”ํŠผ ๊ทธ๋ž˜์„œ else if ๋กœ nums.length ๊ฐ€ 1์ผ๋•Œ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ๊ฑธ์–ด์„œ nums๋ฅผ ๋ฐ˜ํ™˜!
๊ทผ๋ฐ nums ๋Š” ๋ฐฐ์—ด์ด๋‹ˆ๊นŒ, parseInt ๋กœ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค!

๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ๋Š” "๊ณผ๋ฐ˜์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค!" return!

ย ย  model solution 1 ย ย 

function moreThanHalf(nums) {
  let result;
  
  for (let i = 0; i < nums.length; i++) {
    let sum = 0;
    
    for (let j = 0; j < nums.length; j++ ) {
      if (nums[i] === nums[j]) {
        sum += 1;
      }
    }
    
    if (sum > nums.length/2) {
      return nums[i];
    }
  }
}

ย ย  model solution 2 ย ย 

function moreThanHalf(nums) {
  let obj = {};
  let key = '';
  let count = nums.length/2;
  
  for (let i = 0; i < nums.length; i++) {
      let thisNum = nums[i];
      
      if (obj[thisNum]) {
          obj[thisNum]++;
      } else {
          obj[thisNum] = 1;
      }
      
      if (obj[thisNum] > count) {
          key = thisNum;
          break;
      }
  }    
  
  return Number(key);
}

Day 3

ย ย  ๋‚ด ํ’€์ด ย ย 

์ฃผ๋ง์˜ ๋‚ด๊ฐ€ ํ’€๊ฒ ์ง€......๐Ÿ˜…

() {} [] ์ด 3๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ๋ณ€์ˆ˜ ์ง€์ • ํ•ด๋†“๊ณ , s ์•ˆ์— ์ด 3๊ฐœ์ค‘์— ์ผ์น˜ํ•˜๋Š”์• ๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„ ์ž๋ฅด(๋ ค๊ณ  ํ•˜๋Š”๋ฐ ์†์—์„œ ์ฝ”๋“œ๊ฐ€ ์•ˆ๋‚˜์˜จ๋‹ค.)


Day 4

ย ย  ๋‚ด ํ’€์ด ย ย 

๊ด‘์˜ค๋‹˜์ด๋ž‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค ์–˜๊ธฐํ–ˆ๋Š”๋ฐ ์†์—์„œ ์ฝ”๋“œ๊ฐ€ ์•ˆ๋‚˜์˜จ๋‹ค..........๐Ÿ˜…

Day 5

ย ย  ๋‚ด ํ’€์ด ย ย 

function getMaxArea(height) {
  let result = 0;
  let width = 0;
  let minHeight = 0;
  
  for(let i = 0; i<height.length; i++) {
    for(let j = i+1; j < height.length; j++) {
      minHeight = height[i] < height[j] ? height[i] : height[j]
      result = result < (minHeight * (j-i)) ? (minHeight * (j-i)) : result;
    }
  }
  return result;
}

let nums = [1, 8, 6, 2, 5, 4, 8, 3, 7];
getMaxArea(nums); // 49

์ผ๋‹จ... ๋ฉด์ ์€ ๋†’์ด * ๋„ˆ๋น„!!!!!

๊ทผ๋ฐ ์ด๊ฑด ๋ฌผ์„ ๋‹ด์•„์•ผ ํ•˜๋‹ˆ๊นŒ... ๋ฌผ์ด ๋„˜์น˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„ ?

๋†’์ด โ‡’ ๋‘ heigth ๊ฐ’์˜ ์ตœ์†Œ๊ฐ’
๋„ˆ๋น„ โ‡’ index ๊ฐ’์˜ ์ฐจ์ด

๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค!

์ด์ค‘ for ๋ฌธ์œผ๋กœ ๋‘ ๊ฐ’์˜ ๋†’์ด ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ ๋’ค, ๊ฐ ์ตœ์†Œ๊ฐ’์˜ index๋ฅผ ์ด์šฉํ•ด์„œ ๋„ˆ๋น„๋ฅผ ๊ตฌํ•œ๋‹ค.

๋จผ์ €, height์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’๊ณผ ๋‚˜๋จธ์ง€ ๊ฐ’์„ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด i ์™€ i+1 ์„ ๋น„๊ตํ•ด์„œ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ minHeight ์— ์ €์žฅํ•œ๋‹ค.

๋„ˆ๋น„๋Š” ๋‘ index์˜ ์ฐจ์ด๋‹ˆ๊นŒ j-i ๋กœ ๊ตฌํ•œ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์ €์žฅํ•œ ์ตœ์†Œ๊ฐ’๊ณผ ๊ตฌํ•œ ๋„ˆ๋น„๋ฅผ ๊ณฑํ•ด์„œ ์ตœ์ข… ๊ฐ’์ธ result ์™€ ๋น„๊ต!

๊ทธ ์ค‘ ๋” ํฐ ๊ฐ’์„ result ์— ์ €์žฅํ•œ๋‹ค! ์ด๋ฅผ height ์˜ ๋ชจ~~๋“  index์— ๋ฐ˜๋ณต!

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋นจ๋ฆฌ ์ƒ๊ฐํ•ด ๋ƒˆ๋Š”๋ฐ ์†์œผ๋กœ ์“ฐ๋Š”๊ฒŒ ํž˜๋“ค์—ˆ๋‹ค.....
์“ฐ๊ณ ๋‚˜๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ–ˆ๋‹ค.

ย ย  model solution ย ย 

function getMaxArea(height) {
    let size = 0;
    for (let i = 0; i < height.length; i++) {
        for (let j = i+1; j < height.length; j++) {
            size = Math.max(Math.min(height[i], height[j]) * (j-i), size);
        }
    }
    return size;
}

์ปฅ....... ๊ฐœ์งง๋‹ค.....๐Ÿ˜ญ ํ—ˆ๋ฌด์šฐ.... ๊ทผ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‚˜๋ž‘ ๊ฐ™๋‹ค!

๋‚˜๋Š” ์ตœ์†Œ๊ฐ’์„ height[i] < height[j] ์œผ๋กœ ๊ตฌํ–ˆ๊ณ , model solution ์€ Math.min ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‚˜๋Š” ์ตœ์ข… result ์— result < (minHeight * (j-i)) ์œผ๋กœ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ–ˆ๋Š”๋ฐ, model solution ์€ Math.max ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

ํ•œ์ค„๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋๋‚  ์ˆ˜ ์žˆ์—ˆ๋„ค...... model solution ์ฉ๋‹ค....!๐Ÿ˜ญ


ํ›„๊ธฐ : ์Šน๋ฅ  60%...............

profile
ํ•˜๋ฃจํ•˜๋ฃจ๋Š” ์„ฑ์‹คํ•˜๊ฒŒ ์ธ์ƒ ์ „์ฒด๋Š” ๋˜๋Š”๋Œ€๋กœ

0๊ฐœ์˜ ๋Œ“๊ธ€