이 영역을 누르면 첫 페이지로 이동
쿄코코 블로그의 첫 페이지로 이동

쿄코코

페이지 맨 위로 올라가기

쿄코코

얼레벌레 생활🤯

Ollivander's Inventory, Challenges,Contest Leaderboard

  • 2022.11.22 04:32
  • SQL/HackerRank
    반응형

     

    Ollivander's Inventory 📝 

    https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true 

    ➡️ wands 테이블, wands_property 테이블 

    • 출력 : wands의 code, wands_property의 age,wands의 coins_needed,wands의 power
    • 조건 1️⃣ : wands, wands_property JOIN하기 -> code 같음 
    • 조건 2️⃣ : non-evil을 뽑아야하므로 is_evil = 0
    • 조건 3️⃣ : age,power가 같은 경우 coins_needed가 작은 값을 찾기 (galleons을 적게 들게 )
    • 조건 4️⃣ : ORDER BY power -> ORDER BY age
     

    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-evilwand 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,(code1,age1)  and (code2,age2), then  code1 ≠ code2 and age1 ≠ age2.

    Sample Input

    Wands Table: 

    Wands_Property Table: 

    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): 

    • 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): 

    • 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): 

    • 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): 

    • 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
    --방법 1
    SELECT w.id,p.age,w.coins_needed,w.power
    FROM wands_property p JOIN wands w ON p.code=w.code
    WHERE p.is_evil=0
        AND w.coins_needed=(SELECT MIN(coins_needed)
                            FROM wands w1 JOIN wands_property p1
                            ON w1.code = p1.code
                            WHERE w.power=w1.power AND p.age=p1.age)
    ORDER BY w.power DESC,p.age DESC;
    
    --방법 2
    SELECT W.id, P.age, W.coins_needed, W.power
    FROM (SELECT code, MIN(coins_needed) AS coins_needed, power
          FROM Wands
          GROUP BY code, power) AS M
    INNER JOIN Wands AS W ON M.code=W.code AND M.power=W.power AND M.coins_needed=W.coins_needed
    INNER JOIN Wands_Property AS P ON P.code=W.code
    WHERE P.is_evil=0
    ORDER BY W.power DESC, P.age DESC;

     


     

    Challenges📝 

    https://www.hackerrank.com/challenges/challenges/problem?isFullScreen=true 

    ➡️ hackers,challenges 테이블 

    • 출력 : hacker_id,name,COUNT(hacker_id)-> hacker마다 몇번 시도했는지 
    • 조건 1️⃣ 
      challenges 테이블에서 challenge_id COUNT 해서 challenge_created 컬럼 만들기
      -> challenges_created 의 개수를 세었을 때 1인 challenges 값을 찾는다.
      -> 이 때 찾은 challenges 값들이 cnt 값에 있는 경우 찾기

    • 조건 2️⃣ 
      challenges 테이블에서 challenge_id COUNT해서 challenge_created 컬럼 만들기
      -> challenges_created의 MAX 값과 같은 cnt 값 찾기 

     

    Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.

    Input Format

    The following tables contain challenge data:

      • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
     
      • Challenges: The challenge_id is the id of the challenge, and hacker_id is the id of the student who created the challenge.
     

     

    Sample Input 0

    Hackers Table: 

     Challenges Table: 

    Sample Output 0

    21283 Angela 6
    88255 Patrick 5
    96196 Lisa 1
    

    Sample Input 1

    Hackers Table: 

     Challenges Table: 

    Sample Output 1

    12299 Rose 6
    34856 Angela 6
    79345 Frank 4
    80491 Patrick 3
    81041 Lisa 1
    

    Explanation

    For Sample Case 0, we can get the following details: 

     
    Students  and  both created  challenges, but the maximum number of challenges created is  so these students are excluded from the result.

    For Sample Case 1, we can get the following details: 

     
    Students  and  both created  challenges. Because  is the maximum number of challenges created, these students are included in the result.

     

    SELECT h2.hacker_id,h2.name,COUNT(h2.hacker_id) AS cnt
    FROM hackers h2 JOIN challenges c2 
    ON h2.hacker_id = c2.hacker_id
    GROUP BY h2.hacker_id,h2.name
    HAVING cnt IN (SELECT challenges_created
                  FROM
                      (SELECT COUNT(c.challenge_id) challenges_created
                       FROM challenges c
                       GROUP by hacker_id) m1
                   GROUP BY challenges_created
                   HAVING COUNT(challenges_created)=1)
    OR cnt = (SELECT MAX(challenges_created)
              FROM
                  (SELECT COUNT(c.challenge_id) challenges_created
                   FROM challenges c
                   GROUP by hacker_id) m1)
    ORDER BY cnt DESC,h2.hacker_id;

     


     

    Contest Leaderboard 📝 

    https://www.hackerrank.com/challenges/contest-leaderboard/problem?isFullScreen=true 

    ➡️ wands 테이블, wands_property 테이블 

    • 출력 : H.hacker_id, H.name, sub2.total_score
    • 단계 1️⃣ : hacker_id,challenge_id로 그룹 지었을 때 MAX(score)의 값을 구하기 -> 즉 값은 문제를 맞혔을 경우 큰 점수를 맞았을 때의 값을 구하도록
    • 단계 2️⃣ : hacker_id로 그룹지었을 때 sum 구하기 
    • 단계 3️⃣ : 그렇게 만든 hacker_id로 JOIN해서 hacker_id,name,total_score 출력하기 
     

    You did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too! 

    The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id, name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id. Exclude all hackers with a total score of  from your result.

    Input Format

    The following tables contain contest data:

    • Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
    •  
    • Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge for which the submission belongs to, and score is the score of the submission.
    •  

    Sample Input

    Hackers Table: 

    Submissions Table: 

    Sample Output

    4071 Rose 191
    74842 Lisa 174
    84072 Bonnie 100
    4806 Angela 89
    26071 Frank 85
    80305 Kimberly 67
    49438 Patrick 43
    

    Explanation

    Hacker 4071 submitted solutions for challenges 19797 and 49593, so the total score = 95 + max(43,96) = 191

    Hacker 74842 submitted solutions for challenges 19797and 63132, so the total score = max(98,5) +76 = 174 

    Hacker 84072 submitted solutions for challenges 49593and 63132, so the total score = 100+0 = 100.

    The total scores for hackers 4806, 26071, 80305, and 49438 can be similarly calculated.

     

    SELECT H.hacker_id, H.name, sub2.total_score
    FROM (SELECT sub.hacker_id, SUM(max_score) AS total_score
          FROM (SELECT hacker_id, challenge_id, MAX(score) AS max_score
                FROM Submissions 
                GROUP BY hacker_id, challenge_id) sub
          GROUP BY sub.hacker_id
          HAVING total_score != 0) sub2
     INNER JOIN Hackers H ON sub2.hacker_id = H.hacker_id
    ORDER BY sub2.total_score DESC, H.hacker_id
    반응형

    'SQL > HackerRank' 카테고리의 다른 글

    Draw The Triangle 1,Draw The Triangle 2,Print Prime Numbers  (0) 2022.12.20
    SQL Project Planning,Placements,Symmetric Pairs  (0) 2022.12.06
    African Cities, The Report, Top Competitors  (0) 2022.11.15
    African Cities, Average Population of Each Continent, Weather Observation Station 5, Binary Tree Nodes, New Companies  (0) 2022.10.31
    Weather Observation Station 16,17,18,19 | Population Census  (0) 2022.10.24

    댓글

    이 글 공유하기

    • 구독하기

      구독하기

    • 카카오톡

      카카오톡

    • 라인

      라인

    • 트위터

      트위터

    • Facebook

      Facebook

    • 카카오스토리

      카카오스토리

    • 밴드

      밴드

    • 네이버 블로그

      네이버 블로그

    • Pocket

      Pocket

    • Evernote

      Evernote

    다른 글

    • Draw The Triangle 1,Draw The Triangle 2,Print Prime Numbers

      Draw The Triangle 1,Draw The Triangle 2,Print Prime Numbers

      2022.12.20
    • SQL Project Planning,Placements,Symmetric Pairs

      SQL Project Planning,Placements,Symmetric Pairs

      2022.12.06
    • African Cities, The Report, Top Competitors

      African Cities, The Report, Top Competitors

      2022.11.15
    • African Cities, Average Population of Each Continent, Weather Observation Station 5, Binary Tree Nodes, New Companies

      African Cities, Average Population of Each Continent, Weather Observation Station 5, Binary Tree Nodes, New Companies

      2022.10.31
    다른 글 더 둘러보기

    정보

    쿄코코 블로그의 첫 페이지로 이동

    쿄코코

    • 쿄코코의 첫 페이지로 이동

    검색

    메뉴

    • 홈

    카테고리

    • 분류 전체보기 (168)
      • Python (24)
        • 😈 99클럽 코테 스터디 4기 TIL (23)
        • 궁금한거 정리 (1)
      • SQL (16)
        • HackerRank (15)
      • [백준] Python,Java로 풀기📖 (71)
        • 정렬(Sorting) (6)
        • 그리디 (5)
        • 문자열 (7)
        • 수학 (3)
        • DFS&BFS (10)
        • 구현 (4)
        • 다이나믹 (17)
        • 이분탐색 (1)
        • 자료구조 (10)
        • 최단거리 (5)
        • 인덱스트리 (0)
      • [프로그래머스]Python,Java로 풀기 (6)
        • Level 1 (4)
        • Level 2 (2)
      • Study Platform📚 (25)
        • (운영체제) - 블로그 및 강의 참고 (0)
        • 김영한👨🏻‍🏫의 스프링 부트와 JPA 실무 완전 .. (5)
        • (알고리즘)- [이코테] 이것이 코딩테스트다 정리 (10)
        • 그림으로 배우는 Http&Network Basic (10)
      • 까먹을까봐 적는 것들 (4)
      • 테스트 보고 난 후..🤔 (0)
      • kt 에이블스쿨 (18)

    최근 글

    인기 글

    댓글

    공지사항

    아카이브

    태그

    • 티스토리챌린지
    • 항해99
    • 코딩테스트준비
    • 프로그래머스
    • 오블완
    • TiL
    • 백준
    • 99클럽

    나의 외부 링크

    정보

    쿄코코의 쿄코코

    쿄코코

    쿄코코

    블로그 구독하기

    • 구독하기
    • RSS 피드

    방문자

    • 전체 방문자
    • 오늘
    • 어제

    티스토리

    • 티스토리 홈
    • 이 블로그 관리하기
    • 글쓰기
    Powered by Tistory / Kakao. © 쿄코코. Designed by Fraccino.

    티스토리툴바