Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1050. Actors and Directors Who Cooperated At Least Three Times

์ธ” 2023. 9. 8. 12:57

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ”๋ฌธ์ œ

https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/description/

 

Actors and Directors Who Cooperated At Least Three Times - LeetCode

Can you solve this real interview question? Actors and Directors Who Cooperated At Least Three Times - Table: ActorDirector +-------------+---------+ | Column Name | Type | +-------------+---------+ | actor_id | int | | director_id | int | | timestamp | in

leetcode.com

Table : ActorDirector

Write a solution to find all the pairs (actor_id, director_id) where the actor has cooperated with the director at least three times.

Return the result table in any order.

The result format is in the following example.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

 

๋ฐฐ์šฐ์™€ ๊ฐ๋…์ด ์ตœ์†Œ 3๋ฒˆ ์ด์ƒ ํ˜‘์—…ํ•œ ํŽ˜์–ด๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์ด๋‹ค.

์ง์„ ์ฐพ๋Š” ์‰ฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ group by ๋ฅผ ์ ์šฉํ–ˆ๋‹ค.

 

actor_id์™€ director_id์— ๋Œ€ํ•ด group์„ ์ ์šฉํ•˜๋ฉด  <์˜ˆ์‹œ¹> ์™€ ๊ฐ™์ด ์ตœ์ข…์ ์œผ๋กœ ํŽ˜์–ด ํšŸ์ˆ˜๋ฅผ countํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” ํŽ˜์–ด ํšŸ์ˆ˜๊ฐ€ ์ตœ์†Œ 3ํšŒ ์ด์ƒ์ด ์กฐ๊ฑด์œผ๋กœ ์ฃผ์–ด์กŒ์œผ๋ฏ€๋กœ ์ง‘๊ณ„์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ having์ ˆ์— ์ ์šฉํ•ด์„œ ์•„๋ž˜ ์ฟผ๋ฆฌ๋กœ ๋‹ต์•ˆ์„ ์ถœ๋ ฅํ–ˆ๋‹ค.

SELECT actor_id, director_id
 FROM ActorDirector
 GROUP BY 1, 2
 HAVING COUNT(*) >= 3;

 

1. ์˜ˆ์‹œ

SELECT actor_id, director_id, COUNT(*)
 FROM ActorDirector
 GROUP BY 1, 2;