본문 바로가기

코테

코테 - 완전탐색 - 최소 직사각형

Map()쓰려했지만 구현한 로직에 생각지 못한 오류 발견하여 너무 졸려서 오늘 안에 풀고싶기에 결국 다른방법 찾음

풀고 다른사람 풀이를 보니 역시 더 좋은 로직들이 많다

function solution(sizes) {
    const length = []
    const whith =[]
    sizes.forEach((size)=>{
      if(size[0]>size[1]) {
        length.push(size[0])
        whith.push(size[1])
      }else{
        length.push(size[1])
        whith.push(size[0])
      }})
  a=Math.max(...length)
  b=Math.max(...whith)
    var answer = a*b
    return answer;
}

 

 

문제가 있는 로직

[[10, 7], [12, 3], [8, 15], [14, 7], [5, 15]]

두 숫자중 큰 숫자를 key로 저장하면 15가 두번 저장되는데 이때 두번째 저장되는 15의 value가 8보다 작은 5로 변경되어 저장되어 실패한로직....

function solution(sizes) {
    let a;
    let b;
    const map = new Map()
    const length = []
    const whith =[]
    sizes.forEach((size)=>{size[0]>size[1]?map.set(size[0],size[1]):map.set(size[1],size[0])})

    for(let [key,value]of map){
      length.push(key)
      whith.push(value)
    }
  a=Math.max(...length)
  b=Math.max(...whith)
    var answer = a*b
    return answer;
}

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

'코테' 카테고리의 다른 글

코테 - 행렬 - 덧셈  (0) 2023.06.21
코테 - 연습문제 - 카드뭉치  (0) 2023.06.19
코테 - 콜라  (0) 2023.06.08
코테 - 탐욕법 lev1  (0) 2023.05.30
코테 - 두 개 뽑아서 더하기  (0) 2023.03.31