본문 바로가기

Test

기업 코테 풀이

728x90

#1.

function make_not_continue_number(arr)
{
    var answer = [arr[0]]; 
    
    for (var i=1; i< arr.length; i++) {
        var top = answer[answer.length-1]; 
        if (top !== arr[i]) {
            answer.push(arr[i])
        }
    }
    
    return answer;
}

function solution(s) {
    var answer = '';

    var key_pad = {1: ['.', 'Q', 'Z'], 2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 
                   4: ['G', 'H', 'I'], 5: ['J', 'K', 'L'], 6: ['M', 'N', 'O'], 
                   7: ['P', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y']}

    var new_s = make_not_continue_number(s)
    var tmp = []
    var start = 0;
    for (var i = 0; i < new_s.length; i++) {
        var cnt = 0;
        if (new_s[i] === '0') continue;
        
        for (var idx = start; idx < s.length; idx++) {
            if (new_s[i] === s[idx]) {
                cnt++;
            } else if (new_s[i] !== s[idx]) {
                tmp.push([parseInt(new_s[i]), cnt])
                start = idx;
                if (s[idx] === '0') {
                    start = idx + 1;
                }
                break;
            } 
        }
    if (i === new_s.length - 1) {
        tmp.push([parseInt(new_s[i]), cnt])
        }
    }
    for (let j = 0; j < tmp.length; j++) {
        answer += key_pad[tmp[j][0]][tmp[j][1] - 1]
    }
    return answer;
}

#2.

function solution(n, v) {
    var answer = -1;
    for (let i = 0; i < v.length; i++) {
        var buy_money = v[i];
        for (let j = i; j < v.length; j++) {
            var sell_money = v[j];
            if (buy_money - sell_money > answer) {
                answer = buy_money - sell_money
            }
        }
    }

    if (answer === 0) {
        return -1;
    }
    return answer;
}

 

#3.

function solution(r, c) {
    const dp = Array.from(Array(r), () => Array(c).fill(0))
    dp[0][0] = 1;

    for (let i = 0; i < dp.length; i++) {
        for (let j = 0; j < dp[i].length; j++) {
            if (i === 0 || j === 0) dp[i][j] = 1;
        }
    }
    for (let i = 1; i < dp.length; i++) {
        for (let j = 1; j < dp[i].length; j++) {
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        }
    }
    return dp[dp.length - 1][dp[0].length - 1];
}

 

728x90