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

8-kjs254 #30

Merged
merged 2 commits into from
Mar 18, 2024
Merged

8-kjs254 #30

merged 2 commits into from
Mar 18, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Mar 8, 2024

πŸ”— 문제 링크

νŠœν”Œ

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h

✨ μˆ˜λ„ μ½”λ“œ

1. 문제 해석

μ›μ†Œμ˜ κ°œμˆ˜κ°€ n개이고, μ€‘λ³΅λ˜λŠ” μ›μ†Œκ°€ μ—†λŠ” νŠœν”Œ (a1, a2, a3, ..., an)이 μ£Όμ–΄μ§ˆ λ•Œ(단, a1, a2, ..., an은 μžμ—°μˆ˜), n-νŠœν”Œμ€ λ‹€μŒκ³Ό 같이 집합 기호 '{', '}'λ₯Ό μ΄μš©ν•΄ ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

{{a1}, {a1, a2}, {a1, a2, a3}, {a1, a2, a3, a4}, ... {a1, a2, a3, a4, ..., an}}

νŠΉμ • νŠœν”Œμ„ ν‘œν˜„ν•˜λŠ” 집합이 λ‹΄κΈ΄ λ¬Έμžμ—΄ sκ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ, sκ°€ ν‘œν˜„ν•˜λŠ” νŠœν”Œμ„ 배열에 λ‹΄μ•„ return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•΄μ£Όμ„Έμš”.

예λ₯Ό λ“€μ–΄ νŠœν”Œμ΄ (2, 1, 3, 4)인 κ²½μš°λŠ” n-νŠœν”Œμ΄ λ‹€μŒκ³Ό 같이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

{{2}, {2,1}, {2,1,3}, {2,1,3,4}}

κ·Έλƒ₯ 보면 νŠœν”Œμ„ μŠ¬λΌμ΄μ‹±ν•œ λ‹¨μˆœν•œ μ§‘ν•©μ΄μ§€λ§Œ 이 λ•Œ μ§‘ν•©μ˜ μ›μ†ŒλŠ” μˆœμ„œκ°€ λ°”λ€Œμ–΄λ„ μƒκ΄€μ—†μœΌλ‹ˆ λ‹€μŒκ³Ό 같이 λ‹€λ₯΄κ²Œ ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

{{2}, {1,2}, {1,2,3}, {1,2,3,4}}

λ”°λΌμ„œ 각 μ§‘ν•©μ˜ κ°œμˆ˜μ™€ μš”μ†Œλ₯Ό 잘 μ •λ ¬ν•˜λŠ” 것이 이 문제의 관건

인쀄 μ•Œμ•˜μœΌλ‚˜ 해닡은 κ°€κΉŒμ΄μ— μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

image

결과적으둜 각 수의 λΉˆλ„μˆ˜λ₯Ό λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜λ©΄ λ°”λ‘œ 닡을 좜λ ₯ ν•  수 μžˆλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

어쩐지 μž…λ ₯이 λͺ¨λ‘ λ¬Έμžμ—΄λ‘œ μ£Όμ–΄μ Έ μžˆμ–΄ μ–΄λ–»κ²Œ 손을 댈 지 λ‚œκ°ν–ˆμŠ΅λ‹ˆλ‹€..

λ”°λΌμ„œ λ‹€μŒκ³Ό 같이 μˆ˜λ„μ½”λ“œλ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.

2. μˆ˜λ„μ½”λ“œ

  1. {숫자 : λΉˆλ„μˆ˜}λ₯Ό 가진 λ”•μ…”λ„ˆλ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

  2. λΉˆλ„μˆ˜μ— 따라 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•©λ‹ˆλ‹€.

3, μ •λ ¬ν•œ λ”•μ…”λ„ˆλ¦¬μ—μ„œ key 값을 λ‚˜μ—΄ν•©λ‹ˆλ‹€.

전체 μ½”λ“œ

def solution(s):
    answer = []
    dic = {}
    s = s.replace('{','').replace('}','').split(',')
    
    for n in s:
        n = int(n)
        if n not in dic:
            dic[n] = 1
        else:
            dic[n]+= 1
    
    dic_sorted = sorted(dic.items(), key=lambda x:x[1], reverse=True)
                        
    for d in dic_sorted:
        answer.append(d[0])
        
    return answer

λ”•μ…”λ„ˆλ¦¬λ₯Ό keyκ°’μ˜ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•˜λŠ”λ° 어렀움이 μžˆμ–΄ 쑰금 μ•Œμ•„λ³΄λ‹ˆ 파이썬 λ”•μ…”λ„ˆλ¦¬ μ •λ ¬μ—μ„œ μžμ„Ένžˆ μ„€λͺ…이 λ˜μ–΄ μžˆμ–΄ λ‚¨κ²¨λ΄…λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ”•μ…”λ„ˆλ¦¬ 정렬에 λŒ€ν•΄μ„œ ꡬ체적으둜 이해할 수 μžˆλŠ” κΈ°νšŒμ˜€μŠ΅λ‹ˆλ‹€.

λ˜ν•œ λ“±μž” 밑이 μ–΄λ‘‘λ‹€λŠ”κ±Έ μ œλŒ€λ‘œ μ•Œ 게 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μš”μ¦˜ PR이 자꾸 λŠ¦μ–΄μ§€κ³  μžˆμ–΄ μ£„μ†‘ν•©λ‹ˆλ‹€..
λ‹€λ₯Έ 일듀이 λ§Žμ•„ μ‹œκ°„ μ—¬μœ κ°€ λ„ˆλ¬΄ λΆ€μ‘±ν•˜μ§€λ§Œ κ±΄μ„±μœΌλ‘œ PR을 μž‘μ„±ν•˜κ³  싢지 μ•Šμ•„ 자꾸만 미루게 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. ν•˜λ£¨λΌλ„ 빨리 λ”°λΌμž‘λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

μš”μ¦˜ μ½”ν…Œμ—μ„œ IDE ν—ˆμš©μ„ μ•ˆν•΄μ£ΌλŠ”κ³³λ„ μžˆλ‹€κ³  λ“€μ–΄μ„œ ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ λ¬Έμ œλŠ” IDE없이 κ°œλ°œν•΄λ³΄κ³€ ν•˜λŠ”λ° 이 λ¬Έμ œλŠ” μ—„μ²­ μ–΄λ ΅λ„€μš”.... (javaλΌμ„œ 더 μ–΄λ €μ› λ˜κ²ƒλ„ μžˆμŠ΅λ‹ˆλ‹€.)

μ „ λ”•μ…”λ„ˆλ¦¬(맡)λ₯Ό μ •λ ¬ν•  생각은 λͺ»ν•˜κ³  λ”°λ‘œ 클래슀λ₯Ό ν•˜λ‚˜ λ§Œλ“€μ–΄μ„œ λ¦¬μŠ€νŠΈμ—λ‹€ 집어놓고 정렬방법을 λžŒλ‹€λ‘œ μ „λ‹¬ν•΄μ£ΌλŠ” λ°©λ²•μœΌλ‘œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.

μ•„λž˜λŠ” μ œκ°€ ν•΄κ²°ν•œ μ½”λ“œμΈλ° 쒋은 μ½”λ“œλŠ” μ•„λ‹ˆλΌ ν•΄μ„ν•˜μ§„ λ§ˆμ‹œκ³  μ΄λ ‡κ²Œ λ”λŸ½κ²Œ ν‘Ό λ†ˆλ„ μžˆκ΅¬λ‚˜~ μ •λ„λ‘œ λ΄μ£Όμ‹œλ©΄ 될 것 κ°™μŠ΅λ‹ˆλ‹€.

import java.util.ArrayList;
import java.util.Comparator;

public class P_νŠœν”Œ {
}

class Solution {
    public int[] solution(String s) {
        s = s.substring(1, s.length() - 1);
        boolean bracket = false;

        Nodes nodes = new Nodes();

        int number = 0;
        for (int i = 0; i < s.length(); i++) {
            char now = s.charAt(i);
            if (now == '{') {
                bracket = true;
                continue;
            } else if (now == '}') {
                bracket = false;
                continue;
            }

            if (bracket) {
                if (now != ',') {
                    number *= 10;
                    number += Character.getNumericValue(now);
                } else {
                    Node node = nodes.getNode(number);
                    if (node == null) nodes.putNode(number);
                    else node.plusCount();
                }
            }
        }
        nodes.sort();


        int[] answer = new int[nodes.nodeList.size()];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = nodes.nodeList.get(i).id;
        }
        return answer;
    }
}

class Nodes {
    ArrayList<Node> nodeList = new ArrayList<>();

    public Node getNode(int id) {
        for (Node node : nodeList) {
            if (node.id == id) {
                return node;
            }
        }

        return null;
    }

    public void putNode(int id) {
        this.nodeList.add(new Node(id));
    }

    public void sort() {
        nodeList.sort(new Comparator<>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.count - o2.count;
            }
        });
    }
}

class Node {
    int id;
    int count = 1;

    public void plusCount() {
        this.count++;
    }

    public Node(int id) {
        this.id = id;
    }
}

뭐가 μ—„μ²­ κΈΈμ£ ? ν•˜ν•³.. λ°€μ΄λΌμ„œ 머리가 잘 μ•ˆλŒμ•„κ°‘λ‹ˆλ‹€.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

해법은 κ½€ κ°„λ‹¨ν•˜μ§€λ§Œ, 개인적으둠 μž…μΆœλ ₯ κ΅¬ν˜„ μͺ½μ—μ„œ 생각보닀 κΉŒλ‹€λ‘œμš΄ 문제인 것 κ°™μŠ΅λ‹ˆλ‹€. μ½”λ“œλ§Œ 보면 정말 간단해 λ³΄μ΄λ„€μš”.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

ν™•μ‹€νžˆ λ¬Έμžμ—΄ μ²˜λ¦¬μ— μžˆμ–΄μ„œ Python이 되게 쒋은거 κ°™κΈ΄ ν•©λ‹ˆλ‹€!!
λ¬Όλ‘  λ‹€λ₯Έ μ–Έμ–΄μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆκ² μ§€λ§Œ μ €λŠ” 개인적으둜 Python이 되게 직관적이라고 생각을 ν•˜λŠ”λ°, 이λ₯Ό λͺ…ν™•ν•˜κ²Œ λ³΄μ—¬μ£ΌλŠ” μ½”λ“œμΈ 것 κ°™μŠ΅λ‹ˆλ‹€.

λ”•μ…”λ„ˆλ¦¬λ₯Ό μ΄μš©ν•˜λŠ” 쒋은 ν’€μ΄μ˜€λ˜κ±° κ°™μŠ΅λ‹ˆλ‹€.
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.😁

@kjs254 kjs254 merged commit 22050f6 into AlgoLeadMe:main Mar 18, 2024
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