๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript] ํ‰ํ–‰ || ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

728x90
function solution(dots) {
    const xArr = [...dots].sort((a, b) => a[0] - b[0]);
    const yArr = [...dots].sort((a, b) => a[1] - b[1]);

    let result = 0;

    const xParr = ((xArr[1][0] - xArr[0][0]) === (xArr[3][0] - xArr[2][0])
        && (xArr[1][1] - xArr[0][1]) === (xArr[3][1] - xArr[2][1]))

    const yParr = ((yArr[1][0] - yArr[0][0]) === (yArr[3][0] - yArr[2][0])
        && (yArr[1][1] - yArr[0][1]) === (yArr[3][1] - yArr[2][1]))

    // const slide = dots[i][0] / dots[i][1]
    let slide = dots.map(v => v[0] / v[1]);
    if (xParr || yParr) result = 1;
    if (slide.every(v => v === slide[0])) result = 1;
    return result;
}

 

์ฒ˜์Œ์—” ๊ฐ€๊นŒ์šด ๋‘ ์  ์‚ฌ์ด์˜ x์™€ y ์˜ ์ฐจ๊ฐ€ ๊ฐ๊ฐ ๊ฐ™์œผ๋ฉด ํ‰ํ–‰์ด๋‹ค ๋ผ๋Š” ์ƒ๊ฐ์—์„œ ์ถœ๋ฐœํ–ˆ๋Š”๋ฐ, 

solution([[1, 1], [2, 2], [5, 5], [8, 8]]);
solution([[2, 1], [4, 2], [8, 4], [16, 8]]);

 

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์ด๋Ÿฐ ์ผ€์ด์Šค๋Š” 0์ด ๋‚˜์˜จ๋‹ค.

๊ทธ๋ž˜์„œ ์•„๋ž˜์— ๊ธฐ์šธ๊ธฐ๋กœ ์‹์„ ์ถ”๊ฐ€ํ•ด์คฌ๋Š”๋ฐ, 

์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์™€ ๊ฐ™์ด ์ผ€์ด์Šค 1, 10๋ฒˆ์ด ํ†ต๊ณผ๊ฐ€ ์•ˆ๋๋‹ค.

 

์—ฅ ๊ธฐ์šธ๊ธฐ? ๊ธฐ์šธ๊ธฐ๋ฅผ ๋‹ค ๋น„๊ตํ•ด๋ณด๋ฉด ๋œ๋‹ค.

 

์ด ์ „์˜ ์‹์ด dots 2์ฐจ ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ ์“ฐ๋‹ˆ ๋„ˆ๋ฌด ๋ณต์žกํ•ด๋ณด์—ฌ์„œ, 

unction solution(dots) {
    let result = 0;
    const [a, b, c, d] = dots;
    const caseAB = (a[0] - b[0]) / (a[1] - b[1]) === (c[0] - d[0]) / (c[1] - d[1]);
    const caseAC = (a[0] - c[0]) / (a[1] - c[1]) === (b[0] - d[0]) / (b[1] - d[1]);
    const caseAD = (a[0] - d[0]) / (a[1] - d[1]) === (b[0] - c[0]) / (b[1] - c[1]);
    if (caseAB || caseAC || caseAD) result = 1;

    return result;
}

a, b, c, d ์— ๊ฐ ์•„์ดํ…œ์„ ํ• ๋‹นํ•ด์คฌ๋‹ค.

์–ด์ฐจํ”ผ ๋‘๊ฐœ์”ฉ ์ง์ง€์–ด ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š”๊ฑฐ๋‹ˆ,

AB

AC

AD

(๋‚˜๋จธ์ง€๋Š” ์•Œ์•„์„œ ์ง์ด๋จ)

์ด๋ ‡๊ฒŒ ์„ธ๊ฐ€์ง€ ๊ฒฝ์šฐ๋งŒ ์•Œ์•„๋‚ด๋ฉด ๋๋‹ค.

 ์ด ์ค‘ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด result ์— 1๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋œ๋‹ค.

์ด ์„ธ๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ๋‹ค false ๋ผ๋ฉด ํ‰ํ–‰์„ ์ด ์—†๋Š” 0 ์ด return ๋œ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•