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

문제 유형 : 문자열, 구현

난이도: 실버 3

https://www.acmicpc.net/problem/2607

 

2607번: 비슷한 단어

첫째 줄에는 단어의 개수가 주어지고 둘째 줄부터는 한 줄에 하나씩 단어가 주어진다. 모든 단어는 영문 알파벳 대문자로 이루어져 있다. 단어의 개수는 100개 이하이며, 각 단어의 길이는 10 이

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const N = Number(input.shift());
const STRING = input.shift();
const stringDic = {};
STRING.split("").forEach((s) => {
    if(stringDic[s] === undefined) {
        stringDic[s] = 1;
    } else {
        stringDic[s]++;
    }
});

const strArr = input;
let answer = 0;

// 문자가 구성을 갖는지 확인하는 함수
const sameWord = (str) => {
    let string = STRING.split("").sort().join("");
    str = str.split("").sort().join("");
    return string === str;
}

strArr.forEach((s) => {
    // 기준이 되는 단어보다 비교하는 단어의 길이가 1 작은 경우
    let string = STRING.split("").sort();
    let compare = s.split("").sort();

    for(let i = 0 ; i < string.length ; i++) {
        for(let j = 0 ; j < compare.length ; j++) {
            if(compare[j] === string[i]) {
                string[i] = '';
                compare[j] = '';
            }
        }
    }
    string = string.filter((s) => s !== '');
    compare = compare.filter((s) => s !== '');
   // 기준이 되는 단어와 비교하는 단어의 길이는 최대 1까지 차이 날 수 있다.
   if(s.length === STRING.length) {
       // 기준이 되는 단어와 비교하는 단어의 길이가 같은 경우
       if(sameWord(s)) {
           answer++;
       } else {
           if(string.length === 1 && compare.length === 1) {
               answer++;
           }
       }
   } else if(s.length +1 === STRING.length ) {
       if(string.length === 1 && compare.length === 0) {
           answer++;
       }
   } else if(s.length -1 === STRING.length ) {
       // 기준이 되는 단어보다 비교하는 단어의 길이가 1 큰 경우
       if(string.length === 0 && compare.length === 1) {
           answer++;
       }
   } else {
       // 그 외는 같은 구성의 단어가 될 수 없음.
   }
});

console.log(answer);
728x90

https://www.acmicpc.net/problem/13305

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split("\n");

const N = input[0];
const km = input[1].split(" ").map( i => BigInt(i));
const cost = input[2].split(" ").map( i => BigInt(i));

let i = 0;
let current = cost[i];
let total = 0n;

while(true) {
    if(current >  cost[i]) {
        current =  cost[i];
    }
    total += current * km[i];
    i+=1;

    if(i === km.length) {
        break;
    }
}

console.log(String(total));

값이 크기 때문에 BigInt 를 쓰지 않으면 통과 하지 못함.

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;
}

+ Recent posts