https://school.programmers.co.kr/learn/courses/30/lessons/1844

 

프로그래머스

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

programmers.co.kr

function solution(maps) {
    var answer = 0;
    // 상 하 좌 우
    const vector = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    
    const bfs = () => {
        const queue = [[0,0]];
        
        while(queue.length) {
            const cur = queue.shift();            
            if(maps[cur[0]][cur[1]] !== 0) {
                const vectors = [];
                
                vector.forEach((v) => {
                    const x = Number(cur[0])+ Number(v[0]);
                    const y = Number(cur[1])+ Number(v[1]);
                    
                    if(x >= 0 && y >= 0 && x < maps.length && y < maps[0].length && maps[x][y] === 1) {
                        vectors.push([x,y]);
                    }
                });
                
                const prev = maps[cur[0]][cur[1]];
                
                vectors.forEach((v) => {
                    maps[v[0]][v[1]] = prev+1;
                });
                queue.push(...vectors);
            }
        }
    }
    
    bfs();
    return maps[maps.length-1][maps[0].length-1] === 1 ? -1 : maps[maps.length-1][maps[0].length-1];
}
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/49189

 

프로그래머스

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

programmers.co.kr

function solution(n, edge) { 
    const map = new Array(n+1).fill().map(i => []);

    for(let i = 0 ; i < edge.length ; i++) {
        const [e1, e2] = edge[i];
        map[e1].push(e2);
        map[e2].push(e1);
    }

    const visited = new Array(n+1).fill(0);
    visited[0] = -1; // 사용하지 않는 index
    const bfs = (start) => {
        const queue = [start];
        
        while(queue.length) {
            console.log(queue);
            const cur = queue.shift(); // 현재 node 위치
            
            for(let i = 0 ; i < map[cur].length ; i++) {
                // 현재 위치의 node에서 이동가능한 다음 node가 무엇이 있는지 순회
                const next = map[cur][i];
                
                // next가 방문한 적 없는 node이거나, 
                // visited[cur]+1 현재 노드까지 방문한 길이에 1 을 더한 것 보다 큰 경우
                if(!visited[next] || visited[next] > visited[cur] + 1) {
                    queue.push(next);
                    visited[next] = visited[cur] + 1;
                }
            }
        }
    };
    
    bfs(1);
        
    // index 0 -> 사용 X
    // index1 -> node 1임으로 제외
    visited.shift();
    visited.shift();
    
    const maxValue = Math.max(...visited);
    return visited.filter((el) => el === maxValue).length;
}

풀이나 접근을 참고하여 문제 해결

다음에 다시 풀어보기 

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12945

 

프로그래머스

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

programmers.co.kr

function solution(n) {        
    const fibo = [0,1];
    for(let i = 2; i <= n-1; i++) {
        fibo[i] = (fibo[i-1] + fibo[i-2])%1234567;
    }
    
    return (fibo[n-1]+fibo[n-2]) % 1234567;
}
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12924

 

프로그래머스

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

programmers.co.kr

function solution(n) {
    var answer = 0;
    // Math.floor(n/2) 이상부터는 더해서 n이 되는 값이 없음
    
    if(n === 1) return 1;
    
    let start = 1;
    while(start <= Math.floor(n/2)) {
        let sum = 0;
        for(let i = start; i < n ; i++) {
            sum += i;
            
            if(sum === n) {
                answer++;
            } else if( sum > n ) {
                break;
            }
        }
        start++;
    }
    
    return answer + 1;
}
728x90

+ Recent posts