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-mjj111 #212

Merged
merged 1 commit into from
Sep 21, 2024
Merged

11-mjj111 #212

merged 1 commit into from
Sep 21, 2024

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Aug 5, 2024

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

๋นˆ๋„์ •๋ ฌ

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

10 ๋ถ„

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

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ์ˆซ์ž์˜ ์ถœํ˜„ ๋นˆ๋„, ์ธ๋ฑ์Šค ์˜ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ €๋Š” ์ด ๋ฌธ์ œ์—์„œ Map์„ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š”๋ฐ ์ˆ˜์›”ํ•˜๊ฒŒ ๋’€๊ณ 
(๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธดํ–ˆ์œผ๋‚˜ count์˜ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ๋ฐฐ์ œ ํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Ž)

        for (int i = 0; i < N; i++) {
            int now = Integer.parseInt(st.nextToken());
            FreNumber nowFreNumber = numbers.getOrDefault(now, new FreNumber(i, now));
            nowFreNumber.plusFre();
            numbers.put(now, nowFreNumber);
        }

๋ชจ๋‘ ๊ฐฑ์‹ ๋œ ๊ฐ์ฒด๋ฅผ List์— ๋‹ด์— ์ •๋ ฌ ํ›„ ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

        List<FreNumber> answer = new ArrayList<>(numbers.values());
        Collections.sort(answer);

        for (FreNumber f : answer) {
            for (int i = 0; i < f.fre; i++) {
                System.out.print(f.number + " ");
            }
        }

์ •๋ ฌ ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด์— ๊ตฌํ˜„ํ•ด๋†จ์Šต๋‹ˆ๋‹ค.

    static private class FreNumber implements Comparable<FreNumber> {
        int number;
        int index;
        int fre = 0;

        public FreNumber(int index, int number) {
            this.index = index;
            this.number = number;
        }

        public void plusFre() {
            fre++;
        }

        @Override
        public int compareTo(FreNumber op) {
            if (this.fre == op.fre) {
                return Integer.compare(this.index, op.index);
            }
            return Integer.compare(op.fre, this.fre);
        }
    }

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

ํ˜„์žฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋‹ค์‹œ ๊ฐœ๋…์„ ์žก๋Š๋ผ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ์˜ฌ๋ฆฌ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..!
๋‹ค์Œ์—๋Š” ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@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.

๋ฌธ์ œ ๋Œ€์ถฉ๋ณด๊ณ  "๋นˆ๋„์ˆœ -> ์ˆซ์ž์ˆœ"์ด๋„ค~ ํ–ˆ๋Š”๋ฐ "๋“ฑ์žฅ ์ˆœ์„œ"์˜€๊ตฐ์š”...
์–ด์–ด ๊ฑฐ๋ฆฌ๋‹ค ์† ๊ผฌ์ด๊ณ  ๋‹ค์‹œ ์งฐ๋„ค์š” ใ…‹ใ…‹

์ œ ๊ฒฝ์šฐ์—” <์ˆซ์ž(key), <๋“ฑ์žฅ ์ˆœ์„œ, ๋“ฑ์žฅ ๋นˆ๋„>(value)>๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์˜ฎ๊ธธ ๋•Œ ์ˆซ์ž ์ •๋ณด๋„ ํ•จ๊ป˜ ์˜ฎ๊ธฐ๋Š” ์‹์œผ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด value์— ์ถ”๊ฐ€๋กœ ์ˆซ์ž ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง€๋”๋ผ๊ตฌ์š”.

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, C; cin >> N >> C;
    unordered_map<int, pair<int, int>> Counts;
    for(int i = 0; i < N; ++i)
    {
        int n; cin >> n;
        if(Counts.count(n) == 0)
        {
            Counts[n] = {i, 0};
        }
        
        Counts[n].second++;
    }
    
    vector<pair<int, pair<int, int>>> Result(Counts.begin(), Counts.end());
    sort(Result.begin(), Result.end(), [&](const auto& Lhs, const auto& Rhs)
    {
        const auto& [LhsOrder, LhsCount] = Lhs.second;
        const auto& [RhsOrder, RhsCount] = Rhs.second;
        
        if(LhsCount == RhsCount) return LhsOrder < RhsOrder;
        return LhsCount > RhsCount;
    });
    
    for(const auto& [Number, Info] : Result)
    {
        const auto& [Order, Count] = Info;
        for(int i = 0; i < Count; ++i)
        {
            cout << Number << " ";
        }
    }

    return 0;
}

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

์ €๋Š” ์ˆซ์ž๋“ค์„ num_arr์— ์ž…๋ ฅ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•˜๊ณ , cnt_arr์— ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ key๊ฐ’์—” ์ˆซ์ž๋ฅผ, value๊ฐ’์—” ์ž…๋ ฅ๋œ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜์—ฌ์„œ value๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์ถœ๋ ฅํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!
์ฝ”๋“œ๋งŒ ๊ฐ„๋žตํ•˜๊ฒŒ ๋‚จ๊ธฐ๊ฒ ์Šต๋‹ˆ๋‹ค!
๋Šฆ๊ฒŒ ๋ฆฌ๋ทฐํ•ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค... ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!

input = open(0).readline

N, C = map(int, input().split())

num_arr = list(map(int, input().split()))
cnt_arr = {}

for n in num_arr:
    if not n in cnt_arr:
        cnt_arr[n] = 1
    else:
        cnt_arr[n] += 1

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

for key in cnt_arr:
    for j in range(key[1]):
        print(key[0], end=" ")

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.

3 participants