Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11-alstjr7437 #39

Merged
merged 1 commit into from
Feb 28, 2024
Merged

11-alstjr7437 #39

merged 1 commit into from
Feb 28, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Feb 24, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฒ ์ŠคํŠธ ์•จ๋ฒ”

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„

๐Ÿ“œ ๋ฌธ์ œ ์„ค๋ช…

๊ฐ ๋…ธ๋ž˜๋ณ„ ์žฅ๋ฅด์™€ ์žฌ์ƒ๋œ ํšŸ์ˆ˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

0 1 2 3 4
classic pop classic classic pop
500 600 150 800 2500

์œ„ ํ‘œ์™€ ๊ฐ™์ด ๊ฐ ์žฅ๋ฅด์™€ ์žฌ์ƒ ํšŸ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด

  1. ๊ฐ ์žฅ๋ฅด๋ณ„ ์žฌ์ƒํšŸ์ˆ˜ ๋จผ์ € ์„ ์ •
    classic : 500 + 150 + 800 = 1450
    pop : 600 + 2500 = 3100
    ์œผ๋กœ pop์žฅ๋ฅด๊ฐ€ ๋” ๋งŽ์ด ์žฌ์ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  2. ์žฅ๋ฅด๋‚ด ๋งŽ์ด ์žฌ์ƒ๋œ ๊ณก ์„ ์ •
    ๊ทธ๋ ‡๊ฒŒ ๋˜์„œ pop์žฅ๋ฅด์˜ ์žฌ์ƒ์ˆœ์œผ๋กœ 2๊ณก์„ ๋จผ์ € [4,1]
    ๊ทธ๋ฆฌ๊ณ  classic์žฅ๋ฅด์˜ ์žฌ์ƒ์ˆœ์œผ๋กœ 2๊ณก์„ ๋ฝ‘์•„์„œ [4,1,3,0]์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. ์žฌ์ƒ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ๋…ธ๋ž˜ ๋จผ์ € ์„ ์ •

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ทธ๋ ‡๊ฒŒ ์œ„ ๋ฌธ์ œ๋ฅผ ์ด์šฉํ•ด์„œ ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ๋จผ์ € ์งœ๋ฉด

  1. ๊ฐ ์žฅ๋ฅด์™€ ์žฌ์ƒํšŸ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ ๋‹ค

    • ํ˜•ํƒœ๋Š” {'์žฅ๋ฅด' : [[์žฌ์ƒํšŸ์ˆ˜,์ธ๋ฑ์Šค]], 'pop' : [[600, 1], [2500, 4]]}
  2. ํ•ด๋‹น ๋”•์…”๋„ˆ๋ฆฌ์˜ ์žฌ์ƒํšŸ์ˆ˜์™€ ์ธ๋ฑ์Šค ๋ถ€๋ถ„์„ ์žฌ์ƒํšŸ์ˆ˜๋ณ„๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.

    • {'pop' : [[2500, 4], [600, 1]]}
  3. ๊ฐ key๋ฅผ ๊ฐ€์ง€๊ณ  ์ดํ•ฉ์„ ๊ณ„์‚ฐํ•ด์ค€๋‹ค.(๊ฐ ์žฅ๋ฅด๋ณ„ ์ด ์žฌ์ƒํšŸ์ˆ˜)

  4. ์ดํ•ฉ์„ ๊ฐ€์ง€๊ณ  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์žฌ์ •๋ ฌ ํ•ด์ค€๋‹ค.

    • pop์ด ๋” ๋งŽ์œผ๋‹ˆ pop์ด ๋จผ์ €์˜ค๊ฒŒ
  5. ์ •๋‹ต์— ๊ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ณ„๋กœ 2๊ฐœ์”ฉ ๋“ค๊ณ ์™€์„œ ์ถ”๊ฐ€ํ•œ๋‹ค.

    • ๋Œ€์‹  ํ•œ๊ฐœ๋ฉด ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์ˆ˜๋„์ฝ”๋“œ๋Š” ์ž˜ ๋‚˜์™”๋Š”๋ฐ ๊ตฌํ˜„ ๋ถ€๋ถ„์—์„œ ๋ง‰ํžˆ๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค..

1๋ฒˆ, 3๋ฒˆ, 5๋ฒˆ์€ ์–ด๋–ป๊ฒŒ ์ž˜ ๋์ง€๋งŒ 2๋ฒˆ,4๋ฒˆ์—์„œ ์•„์ง lambdaํ•จ์ˆ˜๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ๋ง‰ํžˆ๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค..
๊ทธ๋ƒฅ lambda๋งŒ ์žˆ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ์ž˜ ์“ธ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ๋ฐ˜๋ณต๋ฌธ๊นŒ์ง€ ๊ฐ™์ด ์“ฐ๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ์ฐธ ํž˜๋“ค๋”๋ผ๊ตฌ์š”

def solution(genres, plays):
    answer = []
    dict = {}

    # 1. ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“ค๊ธฐ
    for i in range(len(genres)):
        if genres[i] not in dict :
            dict[genres[i]] = [[plays[i], i]]
        else :
            dict[genres[i]].append([plays[i],i])
            
    
    # 2. ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— ์žฌ์ƒํšŒ์ˆ˜๋กœ ์žฌ์ •๋ ฌ
    for genres, plays in dict.items():
        dict[genres] = sorted(plays, key=lambda x: x[0], reverse=True)

    # 3. ๊ฐ ์žฅ๋ฅด ์ดํ•ฉ ๊ณ„์‚ฐ
    totals = {i: sum(j[0] for j in songs) for i, songs in dict.items()}

    # 4. ์ดํ•ฉ ๊ธฐ์ค€์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ์ˆœ์„œ ๋ณ€๊ฒฝ
    sorted_data = {k: v for k, v in sorted(dict.items(), key=lambda item: totals[item[0]], reverse=True)}


    # 5. ์ •๋‹ต ์ธ๋ฑ์Šค ์ถ”๊ฐ€ ๋ถ€๋ถ„
    for i in sorted_data.values():
        answer.append(i[0][1])
        if len(i) != 1:
            answer.append(i[1][1])
        
    return answer

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

lambdaํ•จ์ˆ˜์— ๊ด€ํ•ด์„œ ํ•œ๋ฒˆ ์ •๋ฆฌ๋ฅผ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.. ํ•ด์‹œ๋ฅผ ์–ด๋Š์ •๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์†Œ๋งˆ ์ฝ”ํ…Œ์—์„œ ์‰ฝ๊ฒŒ ๋‚˜์™”๋Š”๋ฐ lambda๋ฅผ ์ž˜ ๋ชป์“ฐ๋‹ˆ๊นŒ ๋ง‰ํžˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ๊ธฐ๋„ ํ•˜๋”๋ผ๊ตฌ์š”

๊ทธ๋ฆฌ๊ณ  ์ฝ”ํ…Œ ๋“ฑ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์–ด๋–ค ์‹์œผ๋กœ ํ’€์ง€ ์šฐ์„  ํ•˜๋‚˜์”ฉ ์ ์–ด๋‘๊ณ  ํ‘ธ๋Š” ์Šต๊ด€์„ ๊ฐ€์ง€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค ์˜ค๋Š˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด!
๊ทธ๋ ‡๊ฒŒ ์•ˆํ•˜๋‹ˆ๊นŒ ์ค‘๊ฐ„์— ๋ง‰ํžˆ๋Š” ๋ถ€๋ถ„์—์„œ ์–ด๋””์„œ ๋ง‰ํ˜”๋Š”์ง€ ๋ชฐ๋ผ์„œ ์ƒ๊ฐ์„ ๋‹ค์‹œ ๋‹คํ•˜๋Š” ์ฐธ์‚ฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค..

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ํ•ฉ๊ณ„๋ž‘ ์ˆ˜๋ก๊ณก ๊ด€๋ฆฌ๋ฅผ ๋”ฐ๋กœ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ํ•˜๋‚˜๋กœ ํ•ด์„œ 2๊ฐœ์”ฉ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ๊ตฐ์š”.. ํ 
๋•๋ถ„์ด sortWith ์„ ๋” ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

fun solution(genres: Array<String>, plays: IntArray): IntArray {
        val genresSum = linkedMapOf<String, Int>()
        val genreMusics = linkedMapOf<String, MutableList<Pair<Int, Int>>>()
        val answer = mutableListOf<Int>()
        
        for (i in genres.indices) {
            val genreName = genres[i]
            // ์ฒ˜์Œ ๋„ฃ๋Š”๊ฑฐ๋ฉด ์ดˆ๊ธฐํ™”
            if (genreMusics[genreName] == null) {
                genreMusics[genreName] = mutableListOf()
                genresSum[genreName] = 0
            }
            // ํ•ฉ๊ณ„ ์˜ฌ๋ฆฌ๊ธฐ + ์Œ์•… ์ถ”๊ฐ€ํ•˜๊ธฐ
            genresSum[genres[i]] = genresSum[genres[i]]!! + plays[i]
            genreMusics[genres[i]]!!.addMusic(i to plays[i])
        }
        // ํ•ฉ๊ณ„๋กœ ์ •๋ ฌ
        val sortedGenres = genresSum.toList().sortedByDescending { it.second }.toMap()

        // ์ •๋ ฌ๋œ ์žฅ๋ฅด ์ˆœ์„œ๋กœ index ๊ฐ’ ๊ฐ€์ ธ์™€์„œ ๋”ํ•˜๊ธฐ
        sortedGenres.keys.forEach {
            answer += genreMusics[it]!!.map { music -> music.first }
        }
        return answer.toIntArray()
    }

    // ์Œ์•… ์ถ”๊ฐ€ํ•  ๋•Œ
    // play ์ˆœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ, index ์ˆœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    // 3 ์ด์ƒ์ด๋ฉด ๋’ค์— ์ž๋ฅด๊ธฐ
    private fun MutableList<Pair<Int, Int>>.addMusic(music: Pair<Int, Int>) {
        add(music)
        sortWith(compareBy({ -it.second }, { it.first }))
        if (size >= 3) {
            removeLast()
        }
    }

@fnzksxl
Copy link
Member

fnzksxl commented Feb 25, 2024

๋ฌธ์ œ ์„ค๋ช… ์˜†์˜ ์ด๋ชจํ‹ฐ์ฝ˜์ด ๋ฉ”์ดํ”Œ ์ฃผ๋ฌธ์„œ ๊ฐ™์•„์„œ ์ข€ ํ ์นซํ–ˆ์Šต๋‹ˆ๋‹ค.

# 1. ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“ค๊ธฐ
for i in range(len(genres)):
    if genres[i] not in dict :
        dict[genres[i]] = [[plays[i], i]]
    else :
        dict[genres[i]].append([plays[i],i])

์ €๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€ํ•˜๋Š” ์ด ๋ถ€๋ถ„์„ set ์ž๋ฃŒํ˜•์œผ๋กœ ์žฅ๋ฅด๋ฅผ ํ•˜๋‚˜์”ฉ๋งŒ ๋‚จ๊ธฐ๊ฒŒ ํ•ด์„œ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ’€์ด ์ž˜ ๋ดค์–ด์š”!

def solution(genres, plays):
    answer = []
    dict_for_album = dict()
    
    # ์žฅ๋ฅด๋ฅผ set ์ž๋ฃŒํ˜•์œผ๋กœ ํ•˜๋‚˜์”ฉ๋งŒ ๋‚จ๊ฒจ๋†“๊ธฐ
    set_genres = set(genres)
    
    # ๋”•์…”๋„ˆ๋ฆฌ[์žฅ๋ฅด]๋กœ ์ดˆ๊ธฐํ™”
    for genre in set_genres:
        dict_for_album[genre] = [0,[]]
    
    # ์žฅ๋ฅด, ๋…ธ๋ž˜ ๋ณ„ ์žฌ์ƒ ์ˆ˜ ๊ธฐ๋ก
    for i in range(len(genres)):
        dict_for_album[genres[i]][0] += plays[i]
        dict_for_album[genres[i]][1].append((plays[i], i))
    
    # ์žฅ๋ฅด ๋ณ„ ์žฌ์ƒ ์ˆ˜, ์žฅ๋ฅด ๋‚ด ๋…ธ๋ž˜ ๋ณ„ ์žฌ์ƒ ์ˆ˜๋กœ ์ •๋ ฌ
    # ์žฅ๋ฅด ๋ณ„๋กœ ๋‘ ๊ฐœ์”ฉ ์ •๋‹ต์— ์ถ”๊ฐ€(ํ•˜๋‚˜์˜ ๋…ธ๋ž˜๋”๋ผ๋„ ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์„œ ์ž˜ ๋จ)
    for _ , lists in sorted(dict_for_album.items(), key=lambda x : -x[1][0]):
        for index in sorted(lists[1], key=lambda x : -x[0])[:2]:
            answer.append(index[1])
    
    return answer

@9kyo-hwang
Copy link

์ €์˜ ๊ฒฝ์šฐ ์žฅ๋ฅด๋ฅผ set์— ์ง‘์–ด๋„ฃ์–ด์„œ ์ข…๋ฅ˜ ๋ณ„๋กœ ํ•˜๋‚˜์”ฉ๋งŒ ํ•„ํ„ฐ๋งํ•œ ํ›„, ๊ทธ๊ฑฐ๋ฅผ ํ‚ค ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

playlist_by_genre = {genre : [] for genre in set(genres)}
for i, (genre, play) in enumerate(zip(genres, plays)):
    playlist_by_genre[genre].append((play, i))

๋‹ค๋ฅธ ๋ถ„ ์ฝ”๋“œ ๋ณด๋‹ˆ๊นŒ ๊ทน๋„๋กœ ์••์ถ•์‹œํ‚จ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ธธ๋ž˜ ์ €๋„ ๋“ค๊ณ ์™”์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹

sorted_genre = sorted(playlist_by_genre.keys(), key=lambda x: sum(map(lambda y: y[0], playlist_by_genre[x])), reverse=True)

์žฅ๋ฅด ๋ชฉ๋ก(x)์„ ์ •๋ ฌํ•˜๋Š”๋ฐ, ๊ฐ ์žฅ๋ฅด ๋ชฉ๋ก ๋ณ„ ๊ณก ์ •๋ณด(์žฌ์ƒ ์‹œ๊ฐ„, ์ธ๋ฑ์Šค) ๋ฆฌ์ŠคํŠธ(playlist_by_genre[x])์—์„œ play ์‹œ๊ฐ„์˜ ํ•ฉ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

for genre in sorted_genre:
    temp = [index for play, index in sorted(playlist_by_genre[genre], key=lambda x: (-x[0], x[1]))]
    answer += temp[:2]

๋งˆ์ง€๋ง‰์œผ๋กœ ์œ„์—์„œ ์ •๋ ฌํ•œ ์žฅ๋ฅด ์ˆœ์„œ๋Œ€๋กœ, ์žฅ๋ฅด ๋ณ„ ๊ณก ์ •๋ณด(์žฌ์ƒ์‹œ๊ฐ„, ์ธ๋ฑ์Šค)๋ฅผ ์ˆœํšŒํ•ด์„œ 1. ์žฌ์ƒ์‹œ๊ฐ„ ๋งŽ๊ณ (-x[0]) 2. ๋‚ฎ์€ ๊ณ ์œ ๋ฒˆํ˜ธ ์šฐ์„ ์œผ๋กœ(x[1]) ์•ž 2๊ฐœ๋งŒ ์ถ”์ถœํ•ด์„œ answer์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

def solution(genres, plays):
    playlist_by_genre = {genre : [] for genre in set(genres)}
    for i, (genre, play) in enumerate(zip(genres, plays)):
        playlist_by_genre[genre].append((play, i))
                                      
    sorted_genre = sorted(playlist_by_genre.keys(), key=lambda x: sum(map(lambda y: y[0], playlist_by_genre[x])), reverse=True)
    
    answer = []
    for genre in sorted_genre:
        temp = [index for play, index in sorted(playlist_by_genre[genre], key=lambda x: (-x[0], x[1]))]
        answer += temp[:2]
    return answer

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def solution(genres, plays):
    answer = []
    classified_plays = {genre : [] for genre in genres}
    genre_play_count = { genre : 0 for genre in genres}

    plays = list(enumerate(plays))
    for idx in range(len(genres)):
        genre_play_count[genres[idx]] = genre_play_count[genres[idx]] + plays[idx][1]
        classified_plays[genres[idx]].append(plays[idx])

    for classified_genre in classified_plays.keys() :
        classified_plays[classified_genre].sort(key=lambda x : -x[1])
    
    sorted_genre_play_count = sorted(genre_play_count, key=lambda x : -genre_play_count[x])
    
    limit = 2
    for sorted_genre in sorted_genre_play_count :
        answer.extend([idNum[0] for idNum in classified_plays[sorted_genre][:limit]])
    return answer

์•„์ง python ์˜ ํ•จ์ˆ˜๋“ค์„ ๋ณด๊ณ  ํ’€๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ’€์ด๋ฒ•์€ ํ˜ผ์ž์„œ ์ƒ๊ฐํ•ด ๋ƒˆ์–ด์š”! ์•„์‹ธ ๐Ÿฅณ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants