[programmers] ํ‰ํ–‰ - Java

๋„ค ์ ์˜ ์ขŒํ‘œ๋ฅผ ๋ฐ›์•„ ๋‘ ์ง์„ ์ด ํ‰ํ–‰์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์—์„œ, ๊ธฐ์šธ๊ธฐ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ. ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์„ธ ๊ฐ€์ง€๋กœ, ๊ฐ ๊ฒฝ์šฐ์—์„œ ๋‘ ์ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‘ ์ ์˜ ๊ธฐ์šธ๊ธฐ์™€ ๊ฐ™์œผ๋ฉด ๋‘ ์„ ๋ถ„์€ ํ‰ํ–‰ํ•˜๋‹ค.
Feb 03, 2024
[programmers] ํ‰ํ–‰ - Java

๋ฌธ์ œ ์„ค๋ช…

์  ๋„ค ๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์€ ์ด์ฐจ์› ๋ฐฐ์—ด  dots๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
์ฃผ์–ด์ง„ ๋„ค ๊ฐœ์˜ ์ ์„ ๋‘ ๊ฐœ์”ฉ ์ด์—ˆ์„ ๋•Œ, ๋‘ ์ง์„ ์ด ํ‰ํ–‰์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด 1์„ ์—†์œผ๋ฉด 0์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • dots์˜ ๊ธธ์ด = 4
  • dots์˜ ์›์†Œ๋Š” [x, y] ํ˜•ํƒœ์ด๋ฉฐ x, y๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    • 0 โ‰ค x, y โ‰ค 100
  • ์„œ๋กœ ๋‹ค๋ฅธ ๋‘๊ฐœ ์ด์ƒ์˜ ์ ์ด ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ๋‘ ์ง์„ ์ด ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ(์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ)์—๋„ 1์„ return ํ•ด์ฃผ์„ธ์š”.
  • ์ž„์˜์˜ ๋‘ ์ ์„ ์ด์€ ์ง์„ ์ด x์ถ• ๋˜๋Š” y์ถ•๊ณผ ํ‰ํ–‰ํ•œ ๊ฒฝ์šฐ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

dots
result
[[1, 4], [9, 2], [3, 8], [11, 6]]
1
[[3, 5], [4, 1], [2, 4], [5, 10]]
0

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
  • ์  [1, 4], [3, 8]์„ ์ž‡๊ณ  [9, 2], [11, 6]๋ฅผ ์ด์œผ๋ฉด ๋‘ ์„ ๋ถ„์€ ํ‰ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #2
  • ์ ์„ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐํ•ด๋„ ํ‰ํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

solution.java

class Solution { public int solution(int[][] dots) { int x1 = dots[0][0], y1 = dots[0][1]; int x2 = dots[1][0], y2 = dots[1][1]; int x3 = dots[2][0], y3 = dots[2][1]; int x4 = dots[3][0], y4 = dots[3][1]; int answer = 0; double slope1 = (double) (y2 - y1) / (x2 - x1); double slope2 = (double) (y4 - y3) / (x4 - x3); if (slope1 == slope2) answer = 1; slope1 = (double) (y3 - y1) / (x3 - x1); slope2 = (double) (y2 - y4) / (x2 - x4); if (slope1 == slope2) answer = 1; slope1 = (double) (y4 - y1) / (x4 - x1); slope2 = (double) (y2 - y3) / (x2 - x3); if (slope1 == slope2) answer = 1; return answer; } }
 

ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ

  • ๊ธฐ์šธ๊ธฐ๋Š” y์˜ ๊ฐ’์˜ ์ฆ๊ฐ€๋Ÿ‰์—์„œ x์˜ ๊ฐ’์˜ ์ฆ๊ฐ€๋Ÿ‰์„ ๋‚˜๋ˆˆ ๊ฐ’๊ณผ ๊ฐ™๋‹ค.
  • ํ•ด๋‹น ๋ฌธ์ œ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”
    • 1๋ฒˆ๊ณผ 2๋ฒˆ, 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ๋งค์นญํ•˜๊ฑฐ๋‚˜
    • 1๋ฒˆ๊ณผ 3๋ฒˆ, 2๋ฒˆ๊ณผ 4๋ฒˆ์„ ๋งค์นญํ•˜๊ฑฐ๋‚˜
    • 1๋ฒˆ๊ณผ 4๋ฒˆ, 2๋ฒˆ๊ณผ 3๋ฒˆ์„ ๋งค์นญํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
  • ํ•ด๋‹น ๊ฒฝ์šฐ์˜ ์ˆ˜์—์„œ ๋‘ ์ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‘ ์ ์˜ ๊ธฐ์šธ๊ธฐ์™€ ๊ฐ™์œผ๋ฉด ๋‘ ์„ ๋ถ„์€ ํ‰ํ–‰ํ•œ๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์ฒ˜์Œ ์ ‘๊ทผํ•œ ๋ฐฉ์‹์€ ๋‘ ์ขŒํ‘œ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•˜๊ณ , ๊ธฐ์šธ๊ธฐ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ปDriedPollack's Blog