일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Python
- skip-gram
- kakao
- Stack
- Sigmoid
- affine
- hash
- select
- MySQL
- Programmers
- 신경망
- dl
- boj
- backward
- 파이썬
- 프로그래머스
- DeepLearning
- que
- 딥러닝
- SQL
- 자연어처리
- PPMI
- Heap
- Word2vec
- FullyConnectedLayer
- sort
- algorithm
- CBOW
- Numpy
- stak
- Today
- Total
혜온의 이것저것
[HackerRank] Ollivander's Inventory (MySQL) 본문
https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true
Ollivander's Inventory | HackerRank
Help pick out Ron's new wand.
www.hackerrank.com
문제
Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.
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. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.
Input Format
The following tables contain data on the wands in Ollivander's inventory:
- Wands: The id is the id of the wand, code is the code of the wand, coins_needed is the total number of gold galleons needed to buy the wand, and power denotes the quality of the wand (the higher the power, the better the wand is).
- Wands_Property: The code is the code of the wand, age is the age of the wand, and is_evil denotes whether the wand is good for the dark arts. If the value of is_evil is 0, it means that the wand is not evil. The mapping between code and age is one-one, meaning that if there are two pairs, (code_1, age_1) and (code_2, age_2), then code_1≠code_2 and age_1≠age_2.
Sample Input
Wands Table:
![](https://blog.kakaocdn.net/dn/bFK8Ms/btrLtMQM45e/MVqD5EQNGSV0Lt5qhLqBI1/img.png)
Wands_Property Table:
![](https://blog.kakaocdn.net/dn/2PbYj/btrLuC1jhEH/uACf6mWwKM0ZOlr8ChKpCK/img.png)
Sample Output
9 45 1647 10
12 17 9897 10
1 20 3688 8
15 40 6018 7
19 20 7651 6
11 40 7587 5
10 20 504 5
18 40 3312 3
20 17 5689 3
5 45 6020 2
14 40 5408 1
Explanation
The data for wands of age 45 (code 1):
![](https://blog.kakaocdn.net/dn/COpZG/btrLuyR2FGE/8sEO7qPSDLdUKUw8kQ4d81/img.png)
- The minimum number of galleons needed for wand(age=45, power=2)=6020
- The minimum number of galleons needed for wand(age=45,power=10)=1647
The data for wands of age 40 (code 2):
![](https://blog.kakaocdn.net/dn/boVbGW/btrLu2FogQO/hWY9z3DnTyR3OHefzvrx80/img.png)
- The minimum number of galleons needed for wand(age=40,power=1)=5408
- The minimum number of galleons needed for wand(age=40,power=3)=3312
- The minimum number of galleons needed for wand(age=40,power=5)=7587
- The minimum number of galleons needed for wand(age=40,power=7)=6018
The data for wands of age 20 (code 4):
![](https://blog.kakaocdn.net/dn/cTOoKi/btrLy2K9WKl/illSxr8uiNp2ntyoksDi6k/img.png)
- The minimum number of galleons needed for wand(age=20,power=5)=504
- The minimum number of galleons needed for wand(age=20,power=6)=7651
- The minimum number of galleons needed for wand(age=20,power=8)=3688
The data for wands of age 17 (code 5):
![](https://blog.kakaocdn.net/dn/baduo3/btrLwIzGU0B/QUyhYRsTyEUHYw5o3kr0Hk/img.png)
- The minimum number of galleons needed for wand(age=17,power=3)=5689
- The minimum number of galleons needed for wand(age=17,power=10)=9897
풀이
select w.id, p.age, w.coins_needed, w.power
from Wands w
inner join Wands_Property p on p.code=w.code
where is_evil<1
and w.coins_needed =(select min(w2.coins_needed)
from Wands w2
inner join Wands_Property p2 on w2.code=p2.code
where p2.is_evil<1
and w2.power=w.power
and p2.age=p.age)
order by power desc, age desc
'Data Analysis > SQL' 카테고리의 다른 글
[HackerRank] Contest Leaderboard (MySQL) (0) | 2022.09.08 |
---|---|
[HackerRank] Challenges (MySQL) (0) | 2022.09.08 |
[HackerRank] Top Competitors (MySQL) (0) | 2022.09.06 |
[HackerRank] The Report (MySQL) (0) | 2022.08.29 |
[HackerRank] Average Population of Each Continent (MySQL) (0) | 2022.08.29 |