그래프에 물을 채워서 가장 많이 채우는 경우를 찾아라

정은경·2019년 12월 20일
0
  • 문제
    인자인 height는 숫자로 이루어진 배열입니다.
    그래프로 생각한다면 y축의 값이고, 높이 값을 갖고 있습니다.

아래의 그래프라면 height 배열은 [1, 8, 6, 2, 5, 4, 8, 3, 7] 입니다.

저 그래프에 물을 담는다고 생각하고,
물을 담을 수 있는 가장 넓은 면적의 값을 반환해주세요.

  • 가정
    배열의 길이는 2이상입니다.
function getMaxArea(height) {
  
  width = []
  final = height[height.length-1]
  rlt_list = []
  
  max = 0;
  for(let i=0; i<height.length ; i++){
    width = []
    width.push(final)
    width.push(height[i])
    //console.log(width)
    width.sort()
    //console.log(width)
    rlt = (width[0])*(height.length-i-1);
    //console.log(rlt)
    rlt_list.push(rlt)
    if(rlt > max){
      //console.log("hello")
      max = rlt;
    }
    //console.log(i+":  "+width + ":  " + rlt)
    //return max;
    
  }
  console.log(max)
  return max;

}

height = [1, 1]
getMaxArea(height)
profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글