๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] 2020 KAKAO INTERNSHIP ๋ณด์„ ์‡ผํ•‘

by Fomagran ๐Ÿ’ป 2021. 8. 4.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ณด์„ ์‡ผํ•‘

["DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"] [3, 7]

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์™€ ํˆฌํฌ์ธํŠธ๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

1.Set๋ฅผ ์ด์šฉํ•ด ๋ชจ๋“  ๋ณด์„์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ค๋‹ˆ๋‹ค.

 

Set(gems)์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ค๋‹ˆ๋‹ค.

 

2. ํฌ์ธํ„ฐ ๋‘ ๊ฐœ(First,Last)๋ฅผ ์ฒซ๋ฒˆ์งธ๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

3. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ๋ณด์„์„ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

๋ณด์„์˜ ์ด๋ฆ„์„ key๊ฐ’์œผ๋กœ ๋ณด์„์˜ ์œ„์น˜๋ฅผ value๊ฐ’์œผ๋กœ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

ex) ["DIA":[0,3,4,7],"RUBY":[1,2],"EMERALD":[5]]

 

4.๋ชจ๋“  ๋ณด์„์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ™•์ธ ํ›„ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•ด์ค๋‹ˆ๋‹ค.

 

 

๋ชจ๋“  ๋ณด์„์ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋ชจ๋“  ๋ณด์„์ด ์žˆ์„ ๋•Œ๊นŒ์ง€ Last๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์›€์ง์—ฌ์ค๋‹ˆ๋‹ค.

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋“  First๋ถ€ํ„ฐ Last๊นŒ์ง€ ๋ชจ๋“  ๋ณด์„์ด ์žˆ๋‹ค๋ฉด First์™€ Last์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

 

 

 

First๋ฅผ ์›€์ง์ด๋ฉด์„œ ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์˜ ํ•ด๋‹น ๋ณด์„์„ ์ง€์›Œ์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ํ•ด๋‹น ๋ณด์„์ด ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•ด๋‹น ๋”•์…”๋„ˆ๋ฆฌ ๋ณด์„์„ nil๋กœ ๋งŒ๋“ค์–ด์„œ ์—†์• ์ค๋‹ˆ๋‹ค.

 

๊ทธ ๋‹ค์Œ ๋ณด์„์„ ๋น„๊ตํ•ด์ฃผ๊ณ  ๋งŒ์•ฝ ๋ชจ๋“  ๋ณด์„์ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์‹œ Last๋ฅผ ์›€์ง์—ฌ์ค๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ Last๊ฐ€ ๋ณด์„์˜ ๋งจ ๋์„ ๋„˜์—ˆ์„ ๋•Œ First์™€ Last๋ฅผ ์œ„์น˜๋ฅผ ์ €์žฅํ•œ ๊ฒƒ ์ค‘ ๊ฐ€์žฅ ์ ์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 


Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€