[programmers] ์‹œ์†Œ ์ง๊ฟ - Java

- ์‹œ์†Œ ์ง๊ฟ ๋ฌธ์ œ๋Š” ๋‘ ์‚ฌ๋žŒ์ด ์‹œ์†Œ์—์„œ ์™„์ „ํ•œ ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ์Œ์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. - ์‹œ์†Œ๋Š” 2m, 3m, 4m ๊ฑฐ๋ฆฌ์— ์ขŒ์„์ด ์žˆ์œผ๋ฉฐ, ๋ฌด๊ฒŒ์™€ ๊ฑฐ๋ฆฌ์˜ ๊ณฑ์ด ์–‘์ชฝ์ด ๊ฐ™์•„์•ผ ๊ท ํ˜•์„ ์ด๋ฃฌ๋‹ค. - HashMap์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ ๋ฌด๊ฒŒ์˜ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ , ๊ฐ™์€ ๋ฌด๊ฒŒ์™€ ๋‹ค๋ฅธ ๋ฌด๊ฒŒ์˜ ์Œ์„ ๊ฐ๊ฐ ๊ณ„์‚ฐํ•œ๋‹ค. - ๊ฐ€๋Šฅํ•œ ๊ฑฐ๋ฆฌ ๋น„์œจ (1:1, 2:3, 3:4, 2:4)์„ ๊ณ ๋ คํ•˜์—ฌ ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ๋ชจ๋“  ์Œ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
DriedPollack's avatar
Dec 23, 2024
[programmers] ์‹œ์†Œ ์ง๊ฟ - Java

์‹œ์†Œ ์ง๊ฟ

๋ฌธ์ œ ์„ค๋ช…

์–ด๋Š ๊ณต์› ๋†€์ดํ„ฐ์—๋Š” ์‹œ์†Œ๊ฐ€ ํ•˜๋‚˜ ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์‹œ์†Œ๋Š” ์ค‘์‹ฌ์œผ๋กœ๋ถ€ํ„ฐ 2(m), 3(m), 4(m) ๊ฑฐ๋ฆฌ์˜ ์ง€์ ์— ์ขŒ์„์ด ํ•˜๋‚˜์”ฉ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด ์‹œ์†Œ๋ฅผ ๋‘ ๋ช…์ด ๋งˆ์ฃผ ๋ณด๊ณ  ํƒ„๋‹ค๊ณ  ํ•  ๋•Œ, ์‹œ์†Œ๊ฐ€ ํ‰ํ˜•์ธ ์ƒํƒœ์—์„œ ๊ฐ๊ฐ์— ์˜ํ•ด ์‹œ์†Œ์— ๊ฑธ๋ฆฌ๋Š” ํ† ํฌ์˜ ํฌ๊ธฐ๊ฐ€ ์„œ๋กœ ์ƒ์‡„๋˜์–ด ์™„์ „ํ•œ ๊ท ํ˜•์„ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ ๋‘ ์‚ฌ๋žŒ์„ ์‹œ์†Œ ์ง๊ฟ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํƒ‘์Šนํ•œ ์‚ฌ๋žŒ์˜ ๋ฌด๊ฒŒ์™€ ์‹œ์†Œ ์ถ•๊ณผ ์ขŒ์„ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ์˜ ๊ณฑ์ด ์–‘์ชฝ ๋‹ค ๊ฐ™๋‹ค๋ฉด ์‹œ์†Œ ์ง๊ฟ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ๋žŒ๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ ๋ชฉ๋ก weights์ด ์ฃผ์–ด์งˆ ๋•Œ, ์‹œ์†Œ ์ง๊ฟ์ด ๋ช‡ ์Œ ์กด์žฌํ•˜๋Š”์ง€ ๊ตฌํ•˜์—ฌ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 2 โ‰ค weights์˜ ๊ธธ์ด โ‰ค 100,000
  • 100 โ‰ค weights[i] โ‰ค 1,000
    • ๋ชธ๋ฌด๊ฒŒ ๋‹จ์œ„๋Š” N(๋‰ดํ„ด)์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
    • ๋ชธ๋ฌด๊ฒŒ๋Š” ๋ชจ๋‘ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

weights
result
[100,180,360,100,270]
4

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

{100, 100} ์€ ์„œ๋กœ ๊ฐ™์€ ๊ฑฐ๋ฆฌ์— ๋งˆ์ฃผ๋ณด๊ณ  ์•‰์œผ๋ฉด ๊ท ํ˜•์„ ์ด๋ฃน๋‹ˆ๋‹ค.
{180, 360} ์€ ๊ฐ๊ฐ 4(m), 2(m) ๊ฑฐ๋ฆฌ์— ๋งˆ์ฃผ๋ณด๊ณ  ์•‰์œผ๋ฉด ๊ท ํ˜•์„ ์ด๋ฃน๋‹ˆ๋‹ค.
{180, 270} ์€ ๊ฐ๊ฐ 3(m), 2(m) ๊ฑฐ๋ฆฌ์— ๋งˆ์ฃผ๋ณด๊ณ  ์•‰์œผ๋ฉด ๊ท ํ˜•์„ ์ด๋ฃน๋‹ˆ๋‹ค.
{270, 360} ์€ ๊ฐ๊ฐ 4(m), 3(m) ๊ฑฐ๋ฆฌ์— ๋งˆ์ฃผ๋ณด๊ณ  ์•‰์œผ๋ฉด ๊ท ํ˜•์„ ์ด๋ฃน๋‹ˆ๋‹ค.

solution.java

import java.util.*; class Solution { public long solution(int[] weights) { // HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๊ฐ€์ค‘์น˜์˜ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐ Map<Integer, Long> weightCount = new HashMap<>(); for (int weight : weights) { weightCount.put(weight, weightCount.getOrDefault(weight, 0L) + 1); } long totalPairs = 0; // ๊ฐ€๋Šฅํ•œ ๊ฑฐ๋ฆฌ ๋น„์œจ int[][] ratios = {{1, 1}, {2, 3}, {3, 4}, {2, 4}}; for (Map.Entry<Integer, Long> entry : weightCount.entrySet()) { int weight = entry.getKey(); long count = entry.getValue(); // ๊ฐ™์€ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ pair ์ˆ˜ ์„ธ๊ธฐ if (count > 1) { totalPairs += (count * (count - 1)) / 2; } // ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ pair ์ˆ˜ ์„ธ๊ธฐ for (int[] ratio : ratios) { int num = ratio[0]; int denom = ratio[1]; if (weight * denom % num == 0) { int pairedWeight = weight * denom / num; if (pairedWeight > weight && weightCount.containsKey(pairedWeight)) { totalPairs += count * weightCount.get(pairedWeight); } } } } return totalPairs; } }
 

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

  • HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ณ„๋กœ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ๊ฐ™์€ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์Œ์˜ ์ˆซ์ž๋ฅผ ์„ธ์„œ, ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ totalPairs๋กœ ์ €์žฅํ•œ๋‹ค.
  • ๋‹ค๋ฅธ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์Œ์ค‘, ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ์Œ์˜ ์ˆซ์ž๋ฅผ ์„ธ์„œ totalPairs์— ๋”ํ•œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, 2m์™€ 3m ์ž๋ฆฌ์— ์œ„์น˜ํ–ˆ์„ ๋•Œ ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ๊ฒฝ์šฐ, ์‹์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ด๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ์‹์„ ๋Œ์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
      • ๋งŒ์•ฝ ๋‘ ๋ฌด๊ฒŒ๊ฐ€ ๊ท ํ˜•์„ ์ด๋ฃฌ๋‹ค๋ฉด, ๋‹ค์Œ ๋‘ ์กฐ๊ฑด์„ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
      • pairedWeight > weight
        • ์ด๋ฏธ ๊ณ„์‚ฐํ–ˆ๋˜ ์Œ์„ ๋ฐ˜๋ณตํ•ด์„œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์ด๋‹ค.
      • weightCount.containsKey(pairedWeight)
        • ๊ณ„์‚ฐ๋œ pairedWeight๊ฐ€ ์ž…๋ ฅ ๋ฐฐ์—ด์— ์—†์œผ๋ฉด ์Œ์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด ์กฐ๊ฑด์—์„œ ํ•„ํ„ฐ๋งํ•œ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‘ ๋ฌด๊ฒŒ๊ฐ€ ์Œ์„ ์ด๋ฃจ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ์Œ์˜ ์ด ๊ฐœ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด HashMap์„ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

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