Algorithm - CodeKata #10 πŸ“Œ

Sangho MoonΒ·2020λ…„ 9μ›” 12일
0

Algorithm

λͺ©λ‘ 보기
15/37
post-thumbnail
post-custom-banner

1. Question

인자인 heightλŠ” 숫자둜 이루어진 λ°°μ—΄μž…λ‹ˆλ‹€.
κ·Έλž˜ν”„λ‘œ μƒκ°ν•œλ‹€λ©΄ yμΆ•μ˜ 값이고, 높이 값을 κ°–κ³  μžˆμŠ΅λ‹ˆλ‹€.

μ•„λž˜μ˜ κ·Έλž˜ν”„λΌλ©΄ height 배열은 [1, 8, 6, 2, 5, 4, 8, 3, 7] μž…λ‹ˆλ‹€.

μ € κ·Έλž˜ν”„μ— 물을 λ‹΄λŠ”λ‹€κ³  μƒκ°ν•˜κ³ ,
물을 담을 수 μžˆλŠ” κ°€μž₯ 넓은 면적의 값을 λ°˜ν™˜ν•΄μ£Όμ„Έμš”.

  • κ°€μ •
    λ°°μ—΄μ˜ κΈΈμ΄λŠ” 2μ΄μƒμž…λ‹ˆλ‹€.

2. Answer

function getMaxArea(height) {
  
  lastValue = height[height.length-1];
  rlt_list = [];
  width = [];
  
  max = 0;
  for (let i = 0; i < height.length; i++) {
    width.push(lastValue, height[i]);
    width.sort();
    rlt = (width[0]) * (height.length-i-1);
    rlt_list.push(rlt);
    if (rlt > max) {
      max = rlt;
    }
  }
  return max;
}

getMaxArea([1, 1, 2, 3]); // 3
profile
Front-end developer
post-custom-banner

0개의 λŒ“κΈ€