[programmers] ์นด๋“œ ๋ญ‰์น˜ - Java

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

์นด๋“œ ๋ญ‰์น˜

๋ฌธ์ œ ์„ค๋ช…

์ฝ”๋‹ˆ๋Š” ์˜์–ด ๋‹จ์–ด๊ฐ€ ์ ํžŒ ์นด๋“œ ๋ญ‰์น˜ ๋‘ ๊ฐœ๋ฅผ ์„ ๋ฌผ๋กœ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ฝ”๋‹ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์นด๋“œ์— ์ ํžŒ ๋‹จ์–ด๋“ค์„ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
  • ์›ํ•˜๋Š” ์นด๋“œ ๋ญ‰์น˜์—์„œ ์นด๋“œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์žฅ์”ฉ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•œ ์นด๋“œ๋Š” ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ์นด๋“œ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ๊ธฐ์กด์— ์ฃผ์–ด์ง„ ์นด๋“œ ๋ญ‰์น˜์˜ ๋‹จ์–ด ์ˆœ์„œ๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["i", "drink", "water"], ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["want", "to"]๊ฐ€ ์ ํ˜€์žˆ์„ ๋•Œ ["i", "want", "to", "drink", "water"] ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "i"๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "want"์™€ "to"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ฒซ ๋ฒˆ์งธ ์นด๋“œ๋ญ‰์น˜์— "drink"์™€ "water"๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด cards1cards2์™€ ์›ํ•˜๋Š” ๋‹จ์–ด ๋ฐฐ์—ด goal์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, cards1๊ณผ cards2์— ์ ํžŒ ๋‹จ์–ด๋“ค๋กœ goal๋ฅผ ๋งŒ๋“ค ์žˆ๋‹ค๋ฉด "Yes"๋ฅผ, ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋ฉด "No"๋ฅผ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค cards1์˜ ๊ธธ์ด, cards2์˜ ๊ธธ์ด โ‰ค 10
    • 1 โ‰ค cards1[i]์˜ ๊ธธ์ด, cards2[i]์˜ ๊ธธ์ด โ‰ค 10
    • cards1๊ณผ cards2์—๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • 2 โ‰ค goal์˜ ๊ธธ์ด โ‰ค cards1์˜ ๊ธธ์ด + cards2์˜ ๊ธธ์ด
    • 1 โ‰ค goal[i]์˜ ๊ธธ์ด โ‰ค 10
    • goal์˜ ์›์†Œ๋Š” cards1๊ณผ cards2์˜ ์›์†Œ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • cards1cards2goal์˜ ๋ฌธ์ž์—ด๋“ค์€ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

cards1
cards2
goal
result
["i", "drink", "water"]
["want", "to"]
["i", "want", "to", "drink", "water"]
"Yes"
["i", "water", "drink"]
["want", "to"]
["i", "want", "to", "drink", "water"]
"No"

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
๋ณธ๋ฌธ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #2
cards1์—์„œ "i"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  cards2์—์„œ "want"์™€ "to"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "i want to"๊นŒ์ง€๋Š” ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ "water"๊ฐ€ "drink"๋ณด๋‹ค ๋จผ์ € ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฌธ์žฅ์„ ์™„์„ฑ์‹œํ‚ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "No"๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

solution.java

class Solution { public String solution(String[] cards1, String[] cards2, String[] goal) { int idx1 = 0; // cards1 ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค int idx2 = 0; // cards2 ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค // goal ๋ฐฐ์—ด ์ˆœํšŒ for (String currentStr : goal) { // currentStr์ด cards1[idx1] ๋˜๋Š” cards2[idx2]์— ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ if (idx1 < cards1.length && currentStr.equals(cards1[idx1])) { idx1++; // cards1[idx1]์— ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ idx1 ์ฆ๊ฐ€ } else if (idx2 < cards2.length && currentStr.equals(cards2[idx2])) { idx2++; // cards2[idx2]์— ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ idx2 ์ฆ๊ฐ€ } else { return "No"; // ๋‘˜ ๋‹ค ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ } } return "Yes"; } }
 

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

  • ๊ฐ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋”ฐ๋กœ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด no๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
 

๊ฒฐ๋ก !

์ฒ˜์Œ ์‹œ๋„ํ•œ ๋ฐฉ๋ฒ•์€ ๋‹จ์ˆœํžˆ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋ฐ˜๋ณต์‹œํ‚ค๋ ค ํ–ˆ์œผ๋‚˜, ์ธ๋ฑ์Šค๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. ์ด๋ฅผ ์ธ๋ฑ์Šค ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

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