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

23-H0ngJu #227

Merged
merged 3 commits into from
Aug 21, 2024
Merged

23-H0ngJu #227

merged 3 commits into from
Aug 21, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Aug 10, 2024

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

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

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

45M

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

image

๋ฌธ์ œ ์š”์•ฝ
์žฌ์ƒ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์žฅ๋ฅด 2๊ฐœ๋ฅผ ๊ณจ๋ผ, ๊ฐ๊ฐ์˜ ์žฅ๋ฅด์—์„œ 2๊ฐœ์”ฉ ์„ ํƒํ•˜์—ฌ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์„ ๋งŒ๋“ค์–ด๋ผ@!




์ €๋ฒˆ ๋ฌธ์ œ์— ์ด์€ ํ•ด์‹œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š”๋Œ€๋กœ ์ฐจ๊ทผ์ฐจ๊ทผ ํ’€๋ฉด ํฐ ์–ด๋ ค์›€ ์—†์ด ํ’€ ์ˆ˜ ์žˆ์–ด์š”



์ผ๋‹จ ๊ฐ€์žฅ ๋จผ์ € ์•Œ์•„์•ผ ํ•  ์ •๋ณด๋Š” ์ด ์žฌ์ƒ ์ˆ˜๊ฐ€ ๋งŽ์€ ์žฅ๋ฅด 2๊ฐœ์ž…๋‹ˆ๋‹ค.


๋”ฐ๋ผ์„œ ๋จผ์ €,
(์ธ๋ฑ์Šค, ์žฅ๋ฅด)๋ฅผ ์•Œ๋ ค์ค„ ๋”•์…”๋„ˆ๋ฆฌ์ธ info๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ ,
(์žฌ์ƒ์ˆ˜ ํ•ฉ, ์žฅ๋ฅด)์˜ ์ •๋ณด๋ฅผ ๋‹ด์„ ๋”•์…”๋„ˆ๋ฆฌ dic์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.


dic = {}
info = {}

for i in range(len(genres)):
    g = genres[i]
    p = plays[i]

    info[i] = g #์ธ๋ฑ์Šค ์ €์žฅ
    
    if g in dic:
        dic[g] += p #์ด ์žฌ์ƒ์ˆ˜ ์ €์žฅ
    else:
        dic[g] = p

๊ทธ๋ฆฌ๊ณ  ์™„์„ฑ๋œ dic์—์„œ value๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฆฌ์ŠคํŠธ best_g๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด best_g์—๋Š” ์žฌ์ƒ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ (์žฅ๋ฅด, ์žฌ์ƒ์ˆ˜)๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

best_g = sorted(dic.items(), key=lambda x: x[1], reverse=True)

์ด์ œ, best_g์—์„œ ํ•˜๋‚˜์”ฉ ์žฅ๋ฅด๋ช…์„ ๊บผ๋‚ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ ์žฅ๋ฅด๋ช…์— ๋Œ€ํ•ด์„œ genre(์ฒ˜์Œ์— ์ฃผ์–ด์ง„ input)์™€ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐ™์œผ๋ฉด best_album์— ๋‹ด์Šต๋‹ˆ๋‹ค.


์ด๋•Œ, ํ•ด๋‹น ์žฅ๋ฅด ๋‚ด์—์„œ ๋‹ค์‹œ ์žฌ์ƒ ์ˆ˜๊ฐ€ ๋งŽ์€ 2๊ณก์„ ๋ฝ‘์•„์•ผ ํ•˜๋ฏ€๋กœ,
๊ทธ๋ƒฅ ๋ฒ„๋ธ” ์ •๋ ฌ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ best_album์˜ 1,2๋ฒˆ์งธ ์›์†Œ๋งŒ answer์— ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋์ด ๋‚ฉ๋‹ˆ๋‹ค.


for g, _ in best_g:
    if f == 2: break
    best_album = [(plays[i], i) for i in range(len(genres)) if genres[i] == g]
    for i in range(len(best_album)):
        for j in range(i+1, len(best_album)):
            if best_album[j][0] > best_album[i][0]: #์žฌ์ƒ์ˆ˜๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ
                best_album[i], best_album[j] = best_album[j], best_album[i]
    
    answer.extend([x[1] for x in best_album[:2]])
    f += 1



์‚ฌ์‹ค ์ฒ˜์Œ ์ฝ”๋“œ์—๋Š” ๊ฐ™์€ ์žฌ์ƒ์ˆ˜์ผ ๋•Œ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์ž‘์€ ๊ณก์ด ๋จผ์ € ์˜ค๋„๋ก ํ–ˆ๋Š”๋ฐ,,,
์ง€๊ธˆ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ ํ•ด๋‹น ์กฐ๊ฑด์ด ์—†๋„ค์š” ํ—ˆใ…“ใ…Ž;; ์ด ๋ฌธ์ œ ํ’€๊ธฐ ์ „์— ํ‘ผ ๋ฌธ์ œ๋ž‘ ์งฌ๋ฝ•๋๋‚˜ ๋ด…๋‹ˆ๋‘ฅ

๋ฌธ์ œ๋ฅผ ์ซŒ ๋” ์ œ๋Œ€๋กœ ์ฝ์–ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค....ใ…Ž

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

Copy link
Member

@tgyuuAn tgyuuAn 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):
    dict = {}
    dict_max = {}
    answer = []
    for genre in range(len(genres)):
        if genres[genre] not in dict:
            dict[genres[genre]] = [[genre,plays[genre]]]
        else:
            dict[genres[genre]].append([genre,plays[genre]])

    for genre in dict:
        tot =0
        for j in dict[genre]:
            tot += j[1]
        dict_max[genre]=tot
        dict[genre].sort(reverse=True, key=lambda x: x[1])
    
    dict_max = sorted(list(dict_max.items()), reverse = True, key = lambda x:x[1])
    
    for genre in dict_max:
        count = 0
        while dict[genre[0]]:
            if len(dict[genre[0]])==0 or count ==2:
                break
            else:
                answer.append(dict[genre[0]].pop(0)[0])
                count +=1
    return answer

ํ•ด์‹œ์—๋‹ค๊ฐ€ ์ •๋ ฌ์—๋‹ค๊ฐ€ ๊ตฌํ˜„์—๋‹ค๊ฐ€..

๋ฌธ์ œ ์ž์ฒด์˜ ๋‚œ์ด๋„๋Š” ์‰ฌ์šด๋ฐ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋ฅผ ์š”ํ•˜๋„ค์š”..!

์ฒ˜์Œ์— ์—ฐ์Šตํ•  ๋•Œ ์งฑ ์ข‹์€ ๋ฌธ์ œ์ธ๋“ฏ..!

Copy link

@InSange InSange left a comment

Choose a reason for hiding this comment

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

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

#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iostream>

using namespace std;

struct cmp{
    bool operator()(const pair<int, int>& a, const pair<int, int>& b)
    {
        if(a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    }
};

vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    unordered_map<string, int> album_data;
    unordered_map<string, priority_queue<pair<int, int>, vector<pair<int, int>>, cmp>> album_index;
    unordered_set<string> genre_data;
    priority_queue<pair<int, string>> pq;
    
    for(int i = 0; i < genres.size(); i++)
    {
        string genre = genres[i];
        int play = plays[i];
        
        album_data[genre] += play;
        album_index[genre].push({play, i});
        genre_data.insert(genre);
    }
    
    for(unordered_set<string>::const_iterator it = genre_data.begin(); it != genre_data.end(); ++it)
    {
        pq.push({album_data[*it], *it});
    }
    
    while(!pq.empty())
    {
        int cnt = 0;
        string genre = pq.top().second;
        while(cnt < 2 && !album_index[genre].empty())
        {
            answer.push_back(album_index[genre].top().second);
            album_index[genre].pop();
            cnt++;
        }
        pq.pop();
    }
    
    return answer;
}

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์—

  1. [์žฅ๋ฅด : <์ด ์žฌ์ƒ ํšŸ์ˆ˜, <์žฌ์ƒ ํšŸ์ˆ˜, ๊ณ ์œ  ๋ฒˆํ˜ธ> ๋ฆฌ์ŠคํŠธ>] ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ธฐ๋ก
  2. ๋”•์…”๋„ˆ๋ฆฌ์˜ <์ด ์žฌ์ƒ ํšŸ์ˆ˜, <์žฌ์ƒ ํšŸ์ˆ˜, ๊ณ ์œ  ๋ฒˆํ˜ธ> ๋ฆฌ์ŠคํŠธ> Value ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ด์„œ ์ด ์žฌ์ƒ ํšŸ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
  3. ์ถ”์ถœํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ <์ด ์žฌ์ƒ ํšŸ์ˆ˜, <์žฌ์ƒ ํšŸ์ˆ˜, ๊ณ ์œ  ๋ฒˆํ˜ธ> ๋ฆฌ์ŠคํŠธ>์˜ <์žฌ์ƒ ํšŸ์ˆ˜, ๊ณ ์œ  ๋ฒˆํ˜ธ> ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ์ƒ ํšŸ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ -> ๊ณ ์œ  ๋ฒˆํ˜ธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  4. ์ •๋ ฌํ•œ ๋ฆฌ์ŠคํŠธ์—์„œ 2๊ฐœ์”ฉ Id ์ถ”์ถœ

์‹์œผ๋กœ ํ’€์—ˆ์—ˆ๋„ค์š”... ๋ญ”๊ฐ€ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

vector<int> solution(vector<string> Genres, vector<int> Plays) 
{
    using FSongs = pair<int, vector<pair<int, int>>>;
    unordered_map<string, FSongs> Albums;
    
    for(int i = 0; i < Genres.size(); ++i)
    {
        auto& [TotalPlay, Songs] = Albums[Genres[i]];
        
        TotalPlay += Plays[i];
        Songs.emplace_back(Plays[i], i);
    }
    
    vector<FSongs> SongsList(Albums.size());
    transform(Albums.begin(), Albums.end(), SongsList.begin(), 
              [](auto const& Album)
              {
                  return Album.second;
              });
    sort(SongsList.begin(), SongsList.end(), greater<>());  // TotalPlay ๋‚ด๋ฆผ์ฐจ์ˆœ
    
    vector<int> Answer;
    for(auto& [TotalPlay, Songs] : SongsList)
    {
        sort(Songs.begin(), Songs.end(), [](auto const& Lhs, auto const& Rhs)
             {
                 return Lhs.first == Rhs.first ? Lhs.second < Rhs.second : Lhs.first > Rhs.first;
             });
        
        int Count = 0;
        for(const auto& [Play, Id] : Songs)
        {
            if(Count == 2) break;
            
            Answer.emplace_back(Id);
            Count++;
        }
    }
    
    return Answer;
}

์ด๋ฒˆ์— ๋‹ค์‹œ ํ’€์–ด๋ดค๋Š”๋ฐ,

  1. [์žฅ๋ฅด, ์žฌ์ƒ ํšŸ์ˆ˜, ๊ณ ์œ  ๋ฒˆํ˜ธ]๋ฅผ ๊ฐ€์ง€๋Š” FAlbum ํด๋ž˜์Šค ์ƒ์„ฑ. ์žฌ์ƒ ํšŸ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋˜๊ฒŒ ์„ค์ •
  2. [์žฅ๋ฅด : ์ด ์žฌ์ƒ ํšŸ์ˆ˜] ๋”•์…”๋„ˆ๋ฆฌ, FAlbum ๋ฆฌ์ŠคํŠธ 2๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ๊ฐ ๊ธฐ๋ก
  3. [์žฅ๋ฅด : ์ด ์žฌ์ƒ ํšŸ์ˆ˜] ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜, ์ด ์žฌ์ƒ ํšŸ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  4. FAlbum ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ(์žฌ์ƒ ํšŸ์ˆ˜ ๊ธฐ์ค€) ์ •๋ ฌ
  5. ์ •๋ ฌ๋œ ์žฅ๋ฅด ๋ณ„ ์ด ์žฌ์ƒํšŸ์ˆ˜ ๋ฆฌ์ŠคํŠธ์™€ FAlbum ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•ด ๊ฐ ์žฅ๋ฅด ๋ณ„ ๋…ธ๋ž˜ ๊ณ ์œ  ๋ฒˆํ˜ธ 2๊ฐœ์”ฉ ์ €์žฅ

์‹์œผ๋กœ๋„ ํ’€๋ฆฌ๋„ค์š”. ์กฐ๊ธˆ? ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ตฌ์š”...

์ „์ฒด ์ฝ”๋“œ

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

struct FAlbum
{
    string Genre;
    int Play;
    int Id;
    
    FAlbum(string InGenre, int InPlay, int InId)
        : Genre(std::move(InGenre))
        , Play(InPlay)
        , Id(InId)
        {}
    
    bool operator<(const FAlbum& Other) const
    {
        return Play < Other.Play;
    }
    bool operator<=(const FAlbum& Other) const
    {
        return Play <= Other.Play;
    }
    bool operator>(const FAlbum& Other) const
    {
        return Play > Other.Play;
    }
    bool operator>=(const FAlbum& Other) const
    {
        return Play >= Other.Play;
    }
    bool operator==(const FAlbum& Other) const
    {
        return Play == Other.Play;
    }
    bool operator!=(const FAlbum& Other) const
    {
        return Play != Other.Play;
    }
};

vector<int> solution(vector<string> Genres, vector<int> Plays) 
{
    vector<FAlbum> Albums;
    unordered_map<string, int> PlaysbyGenre;
    for(int i = 0; i < Genres.size(); ++i)
    {
        PlaysbyGenre[Genres[i]] += Plays[i];
        Albums.emplace_back(Genres[i], Plays[i], i);
    }
    
    vector<pair<string, int>> PlaysbyGenreList(PlaysbyGenre.begin(), PlaysbyGenre.end());
    
    sort(PlaysbyGenreList.begin(), PlaysbyGenreList.end(), 
         [](auto const& Lhs, auto const& Rhs)
         {
             return Lhs.second > Rhs.second;
         });
    
    sort(Albums.begin(), Albums.end(), greater<>());
    
    vector<int> Answer;
    for(const auto& [Genre, Play] : PlaysbyGenreList)
    {
        int Count = 0;
        for(const auto& Album : Albums)
        {
            if(Genre == Album.Genre)
            {
                Count++;
                Answer.emplace_back(Album.Id);
            }
            
            if(Count == 2)
            {
                break;
            }
        }
    }
    
    return Answer;
}

@H0ngJu H0ngJu merged commit 81f4b15 into main Aug 21, 2024
@H0ngJu H0ngJu deleted the 23-H0ngJu branch August 21, 2024 03:44
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.

4 participants