[programmers] ์˜์ƒ - Java

์ฝ”๋‹ˆ์˜ ์˜์ƒ ์กฐํ•ฉ ๋ฌธ์ œ๋Š” HashMap์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ ์˜์ƒ ์œ ํ˜•์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์„œ๋กœ ๋‹ค๋ฅธ ์˜์ƒ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ฐ ์˜์ƒ ์œ ํ˜•๋ณ„๋กœ ์˜์ƒ์„ ์ž…์ง€ ์•Š์•˜์„ ๋•Œ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์˜์ƒ์˜ ์ˆ˜์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๊ณฑํ•˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ์˜์ƒ์„ ์ž…์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ 1์„ ๋นผ์ค€๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์กฐํ•ฉ ๊ณต์‹์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ๋‹ค.
DriedPollack's avatar
Mar 15, 2024
[programmers] ์˜์ƒ - Java

์˜์ƒ

๋ฌธ์ œ ์„ค๋ช…

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

์ œํ•œ์‚ฌํ•ญ

  • clothes์˜ ๊ฐ ํ–‰์€ [์˜์ƒ์˜ ์ด๋ฆ„, ์˜์ƒ์˜ ์ข…๋ฅ˜]๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋‹ˆ๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ์˜ ์ˆ˜๋Š” 1๊ฐœ ์ด์ƒ 30๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์˜์ƒ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • clothes์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๊ณ  ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋˜๋Š” '_' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

clothes
return
[["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]
5
[["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]]
3

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

์˜ˆ์ œ #1
headgear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด yellow_hat, green_turban์ด๊ณ  eyewear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด blue_sunglasses์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 5๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
1. yellow_hat 2. blue_sunglasses 3. green_turban 4. yellow_hat + blue_sunglasses 5. green_turban + blue_sunglasses
์˜ˆ์ œ #2
face์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด crow_mask, blue_sunglasses, smoky_makeup์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
1. crow_mask 2. blue_sunglasses 3. smoky_makeup

solution.java

import java.util.*; class Solution { public int solution(String[][] clothes) { HashMap<String, Integer> clothesMap = new HashMap<>(); for (String[] cloth : clothes) { String type = cloth[1]; clothesMap.put(type, clothesMap.getOrDefault(type, 0) + 1); } System.out.println(clothesMap); int answer = 1; for (int count : clothesMap.values()) { answer *= (count + 1); } return answer - 1; } }
 

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

  • HashMap์„ ์ƒ์„ฑํ•ด์„œ ๊ฐ ์œ ํ˜•์˜ ์˜ท ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • clothes ๋ฐฐ์—ด์˜ ๊ฐ ํ–‰์„ ๋ฐ˜๋ณตํ•ด์„œ ์œ ํ˜•๋ณ„๋กœ ์˜ท์ด ๋ช‡๊ฐœ๋‚˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ธ๋‹ค.
  • ๊ณฑ์…ˆ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋ฅผ 1๋กœ ์„ค์ •ํ•˜๊ณ , ์˜ท์˜ ์ข…๋ฅ˜๋ณ„๋กœ ์˜ท์„ ์ž…์ง€ ์•Š์•˜์„ ๋•Œ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์˜ท์˜ ์ˆ˜์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๊ณฑํ•œ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ์˜ท์„ ์ž…์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ 1์„ ๋นผ์ค€๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์กฐํ•ฉ ๊ณต์‹์„ ํ™œ์šฉํ•˜๋Š” ๋ฒ•์„ ์ฝ”๋“œ๋กœ ์ตํž ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 3์žฅ ์ •๋ฆฌ

DriedPollack's avatar
March 18, 2024
[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 3์žฅ ์ •๋ฆฌ

[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 2์žฅ ์ •๋ฆฌ

DriedPollack's avatar
March 13, 2024
[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 2์žฅ ์ •๋ฆฌ

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