๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋ HackerRank ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://www.hackerrank.com/challenges/harry-potter-and-wands/problem
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age.
> ์ต์ ๊ฐค๋ฐ์ผ๋ก ์ด ์ ์์ผ๋ฉฐ ๊ฐ์ฅ ๋์ power์ age์ non-eveil์ธ ๊ฒ์ ์ฌ๊ณ ์ ํจ.
SELECT W2.id, WP.age, W.coins_needed, W.power
FROM(
SELECT code, min(coins_needed) coins_needed, power
FROM Wands
GROUP BY code, power) W
INNER JOIN Wands_Property WP
ON W.code = WP.code
INNER JOIN Wands W2
ON W.code = W2.code
AND W.coins_needed = W2.coins_needed
AND W.power = W2.power
WHERE WP.is_evil = 0
ORDER BY power DESC, age DESC;
์งํก์ด๋ณ๋ก gold galleons์ด ๊ฐ์ฅ ๋ฎ์(๊ฐ์ฅ ์ ๋ ดํ) ์งํก์ด๋ฅผ ๋ฝ์ผ๋ ค๋ฉด, code์ power์ ๋ํด ๊ทธ๋ฃนํํด์ค์ผํ๋๋ฐ
id ๊ฐ ์์ผ๋ฉด ๊ทธ๋ฃนํ์ด ๋์ง ์๋๋ค. (Wands์์ id๋ ๊ณ ์ ๊ฐ)
์ฐ์ ์ต์ ๊ฐค๋ฐ๊ฐ์ธ ์งํก์ด๋ฅผ ๊ตฌํ๊ธฐ ์ํด code์ power์ ๋ํด ๊ทธ๋ฃนํํ์ฌ ์ถ๋ ฅํด์ค๋ค
> Wands W
Join
minimum gold galleons ์ธ ์งํก์ด์ id๋ฅผ ์ฐพ๊ธฐ ์ํด ๊ธฐ์กด์ Wํ ์ด๋ธ์ธ W2์ inner join ํด์ฃผ๋๋ฐ
id๊ฐ ์ ํํ๊ฒ ๋งค์นญ๋๋๋ก code, coin_needed, power๋ฅผ join ์กฐ๊ฑด์ ๋ชจ๋ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค.
minimum gold galleons ์ธ ์งํก์ด๋ same power๋ผ๋ฉด descending age๋ฅผ ์ฐ์ ์ฌํญ์ผ๋ก ๋์๊ธฐ ๋๋ฌธ์ age์ ๋ณด๊ฐ ์๋ Wands_property ํ ์ด๋ธ WP์๋ inner join ํด์ค๋ค.
๊ณตํต์ปฌ๋ผ์ code
Where : ์ ์ฒด ์กฐ๊ฑด
์ด๋ ๊ฒ ๋๋ฉด minimum gold galleons ์ ๊ฐ๋ ์งํก์ด์ id, age, coins_needed, power ์ ๋ณด๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
์ฌ๊ธฐ์ ํค๋ฅด๋ฏธ์จ๋๊ฐ ์๊ตฌํ๋ ์งํก์ด์ ๋จ์ ์กฐ๊ฑด non-evil
non-evil์ WP ํ ์ด๋ธ์ ์์ผ๋ฏ๋ก where์ ์ ์กฐ๊ฑด์ผ๋ก ์ถ๊ฐํด์ค๋ค.
Order by : ์ ๋ ฌ
๋ง์ง๋ง์ผ๋ก ์๊ตฌ์ฌํญ์ ์ ํ ์์์ ๋ฐ๋ผ power ๊ฐ ํฐ ์์๋๋ก, same power๋ผ๋ฉด age ์์๋๋ก ๋ชฉ๋ก์ด ํ์ํ์ผ๋ฏ๋ก
ORDER BY power DESC, age DESC ํด์ฃผ์ด ์๊ตฌ์ฌํญ์ ๋ง๋ ์งํก์ด ๋ชฉ๋ก์ ์ถ๋ ฅํ ์ ์์๋ค.
๐
minimum gold galleons ์ ์งํก์ด๋ค์ ์ฐพ์ ํ์ ์ด๊ฒ์ id๋ฅผ ์ฐพ๊ธฐ ์ํด ๋ค์ ์๋ ํ ์ด๋ธ๊ณผ ์กฐ์ธํ๋ ๊ฒ์ ์๊ฐํด๋ด๊ธฐ ์ด๋ ค์ ๋ค..
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Hacker Rank] Placement (0) | 2022.08.27 |
---|---|
[Hacker Rank] Top Competitors (0) | 2022.08.27 |
[LeetCode] 176. Second Highest Salary (0) | 2022.08.26 |
[LeetCode] 1179. Reformat Department Table (0) | 2022.08.26 |
[LeetCode] 262. Trips and Users (0) | 2022.08.26 |