๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 10์ฃผ์ฐจ ๊ต์ ์— ๋ณ„ ๋งŒ๋“ค๊ธฐ (js)

Lennon 2021. 10. 30. 00:48
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/87377?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 10์ฃผ์ฐจ_๊ต์ ์— ๋ณ„ ๋งŒ๋“ค๊ธฐ

[[2, -1, 4], [-2, -1, 4], [0, -1, 1], [5, -8, -12], [5, 8, 12]] ["....*....", ".........", ".........", "*.......*", ".........", ".........", ".........", ".........", "*.......*"] [[0, 1, -1], [1, 0, -1], [1, 0, 1]] ["*.*"] [[1, -1, 0], [2, -1, 0], [4, -

programmers.co.kr

 

function isInteger(num)  {
    return num % 1 === 0;
}

function intersectionPoint(arr1,arr2){
    const denominator = (arr1[0]*arr2[1])-(arr1[1]*arr2[0]);
    const x = ((arr1[1]*arr2[2])-(arr1[2]*arr2[1])) / denominator;
    const y = ((arr1[2]*arr2[0])-(arr1[0]*arr2[2])) / denominator;

    return denominator === 0 ? 0 : [y,x]
}

function solution(line) {
    let meetPointArr = [];

    for(let i = 0; i < line.length; i++){
        for(let j = i+1; j < line.length; j++){
            const value = intersectionPoint(line[i], line[j]);
            if(isInteger(value[0]) && isInteger(value[1])){
                meetPointArr.push(value);
            }
        }
    }

    meetPointArr.sort((a,b) => a[0]-b[0]);
    const [columnMin, columnMax] = [meetPointArr[0][0],meetPointArr[meetPointArr.length-1][0]];
    meetPointArr.sort((a,b) => a[1]-b[1]);
    const [rowMin,rowMax] = [meetPointArr[0][1],meetPointArr[meetPointArr.length-1][1]];

    meetPointArr = meetPointArr.map(v=>v.join(""));

    const answer = [];

    for(let i = columnMin; i <= columnMax; i++){
        let str = '';
        for(let j = rowMin; j <= rowMax; j++){
            if(meetPointArr.includes(i.toString()+j.toString())) str+="*";
            else str+=".";
        }
        answer.push(str);
    }
    return answer.reverse();
}

 

 

์œ„ ์ฝ”๋“œ๋กœ ํ•˜๋ฉด ๊ณ„์† ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 2๋ฒˆ๋งŒ ์•ˆ๋œ๋‹ค. ์ด์œ ๋Š” ์•„์ง๋„ ๋ชป ์ฐพ์•˜์ง€๋งŒ

3์ค‘ ํฌ๋ฌธ์„ ์“ฐ๊ธฐ๊ฐ€ ์‹ซ์–ด์„œ includes ๋น„๊ต๋ฅผ ์œ„ํ•ด ๋ชจ๋“  ์„ ์˜ ์ •์ˆ˜ ๊ต์ ๋“ค์„ ๋ชจ์•„๋†“์€

30 Line์—์„œ meetPointArr ๋ฐฐ์—ด์˜ ๊ต์ ์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ ํ•œ ํ›„

ex) meetPointArr =>  [[-4, -4], [-1, -2]] =>  ["-4-4", "-1-2"]

(i์™€ j๋Š” ๊ต์ ๋“ค ์ค‘ ๊ฐ€์žฅ min๊ฐ’~๊ฐ€์žฅ max๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Œ ex)-4~4)

(37 Line) ๋ฐ˜๋ณต๋ฌธ์˜ i, j๋„ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ๋”ํ•ด meetPointArr์— includes๋˜์–ด์žˆ๋‹ค๋ฉด str+="*"์œผ๋กœ,

์—†์œผ๋ฉด str+="." ์ •๋ฆฌํ–ˆ๋Š”๋ฐ 

(์ค‘๋ณต๋˜๋Š” ๊ต์ ์€ ์žˆ์–ด๋„ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•จ)

(์‹ค์ œ๋กœ ์ค‘๋ณต๋˜๋Š” ๊ต์ ์„ ์ฒซ๋ฒˆ์งธ meetPointArr pushํ•˜๋Š” for๋ฌธ์—์„œ ์ฒ˜๋ฆฌํ•ด๋„ ์•ˆ๋๋‹ค.)

 

2๋ฒˆ๋งŒ ๊ณ„์† ํ†ต๊ณผ๊ฐ€ ์•ˆ๋๋‹ค. 

 

ํ˜น์‹œ๋‚˜ ํ•˜๊ณ  meetPointArr๋Š” ๋ฌธ์ž๋ณ€ํ™˜์ด ์•„๋‹Œ ๊ทธ๋Œ€๋กœ [[-4, -4], [-1, -2]] ์œ ์ง€ํ•˜๊ณ 

 

์•„๋ž˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ includes๊ฐ€ ์•„๋‹Œ forEach๋ฅผ ํ†ตํ•ด ๋น„๊ตํ•˜๋‹ˆ ํ†ต๊ณผ๊ฐ€ ๋๋‹ค.

 

 

function isInteger(num)  {
    return num % 1 === 0;
}

function intersectionPoint(arr1,arr2){
    const denominator = (arr1[0]*arr2[1])-(arr1[1]*arr2[0]);
    const x = ((arr1[1]*arr2[2])-(arr1[2]*arr2[1])) / denominator;
    const y = ((arr1[2]*arr2[0])-(arr1[0]*arr2[2])) / denominator;

    return denominator === 0 ? 0 : [y,x]
}

function solution(line) {
    let meetPointArr = [];
    for(let i = 0; i < line.length; i++){
        for(let j = i+1; j < line.length; j++){
            const value = intersectionPoint(line[i], line[j]);
            if(isInteger(value[0]) && isInteger(value[1])){
                meetPointArr.push(value);
            }
        }
    }
    
    meetPointArr.sort((a,b) => a[0]-b[0]);
    const [columnMin, columnMax] = [meetPointArr[0][0],meetPointArr[meetPointArr.length-1][0]];
    meetPointArr.sort((a,b) => a[1]-b[1]);
    const [rowMin,rowMax] = [meetPointArr[0][1],meetPointArr[meetPointArr.length-1][1]];

    const answer = [];
    
    for(let i = columnMin; i <= columnMax; i++){
        let str = '';
        for(let j = rowMin; j <= rowMax; j++){
            str += "."
            meetPointArr.forEach(v => {
                if(v[0] === i && v[1] === j){
                    str = str.slice(0, -1);
                    str += "*";
                }
            })
        }
        answer.push(str);
    }
    return answer.reverse();
}

 

 

2๋ฒˆ ์ผ€์ด์Šค๊ฐ€ ๋„ˆ๋ฌด๋„ˆ๋ฌด ๊ถ๊ธˆํ•˜๋‹ค. ์ค‘๋ณต๋˜๋Š” ๊ต์ ์ด ์žˆ๋“  ์—†๋“  includes๋Š” ์ƒ๊ด€์ด ์—†์„ํ…๋ฐ..

 

๋งŒ์•ฝ ์‹ค์ œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์‹œํ—˜์— ์ด๊ฒŒ ๋‚˜์™”๋‹ค๋ฉด ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน์—ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค..

 

์ด์ œ ์˜ˆ์™ธ๋Š” ๋ฌด์กฐ๊ฑด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ํ™•์‹คํ•˜๊ฒŒ ํ’€์–ด์•ผ๊ฒ ๋‹ค!

 

728x90
๋ฐ˜์‘ํ˜•