학습 기록 (Learning Logs)/알고리즘

프로그래머스 직사각형 나머지 한 점

devWonny 2022. 8. 21. 14:16

https://school.programmers.co.kr/learn/courses/18/lessons/1878

 

프로그래머스

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

programmers.co.kr

 

 

핵심: 사각형은 숫자가 2번씩 겹친다. 겹치지 않는 값을 x,y에 넣어주면 된다

 

function solution(v) {
    let left = new Map();
    let right = new Map();
    let answer = [];
    
    // let result = v.map((ele)=>{
    //     map.has(ele[0]){
            
    //     }

    // })

    for(let [key, value] of v){
        // console.log(key, value);
        checkAdd(key, left);
        checkAdd(value, right);
        // console.log(left);
        // console.log(right);
    }
    // console.log(left)
    // console.log(right)

    for(tt of left){
        // console.log(tt);
        // console.log(tt[1]);
       if(tt[1]==1) answer.push(tt[0]); 
    }
    for(tt of right){
        // console.log(tt);
        // console.log(tt[1]);
       if(tt[1]==1) answer.push(tt[0]); 
    }
    
   

    console.log(answer);

    return answer;
}

function checkAdd(key, map){
    if(map.has(key)){
            let temp = map.get(key);
            map.set(key,temp+1);
        }else{
            map.set(key,1);
        }
}

 

 

map: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Keyed_collections

자바스크립트 map 반복문으로 요소 접근하기: https://developer-talk.tistory.com/170