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

5 mjj111 #192

Merged
merged 3 commits into from
Aug 11, 2024
Merged

5 mjj111 #192

merged 3 commits into from
Aug 11, 2024

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Jun 4, 2024

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

https://school.programmers.co.kr/learn/courses/30/lessons/258709
์ฃผ์‚ฌ์œ„ ๊ณ ๋ฅด๊ธฐ

image

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

1์‹œ๊ฐ„ 20๋ถ„

โœจ ์ฝ”๋“œ ์„ค๋ช…

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ๊ฑฐ์‹œ์ ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด,
์ฃผ์‚ฌ์œ„ ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ  ์ดˆ๊ธฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„,
choiceDice ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ตœ์ ์˜ ๊ทธ๋ฃน์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ answer ๋ฐฐ์—ด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ choiceDice ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์–ด๋–ป๊ฒŒ ์ตœ์ ์˜ ๊ทธ๋ฃน์„ ์ฐพ๋Š”๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

choiceDice ๋ฉ”์„œ๋“œ

์žฌ๊ท€์ ์œผ๋กœ ์ฃผ์‚ฌ์œ„๋ฅผ ์„ ํƒํ•˜์—ฌ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
๊ฐ ์กฐํ•ฉ์— ๋Œ€ํ•ด calculateWinningPercent ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์Šน๋ฆฌ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ , ์ตœ๋Œ€ ์Šน๋ฆฌ ํ™•๋ฅ ์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

    public static void choiceDice(int depth, int s) {
        if (depth == N / 2) {
            int winning = calculateWinningPercent();
            if (max < winning) {
                max = winning;
                for (int i = 0; i < choice.size(); i++) {
                    answer[i] = choice.get(i) + 1;
                }
            }
            return;
        }
        
        for (int i = s; i < N; i++) {
            choice.add(i);
            choiceDice(depth + 1, i + 1);
            choice.remove(choice.size() - 1);
        }
    }

calculateWinningPercent ๋ฉ”์„œ๋“œ

ํ˜„์žฌ ์„ ํƒ๋œ ์กฐํ•ฉ์— ๋Œ€ํ•ด ๋‘ ๊ทธ๋ฃน(arrA, arrB)์˜ ์ˆซ์ž ํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฃน B๋ฅผ ์ •๋ ฌํ•œ ํ›„, ๊ทธ๋ฃน A์˜ ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ๊ทธ๋ฃน B์—์„œ ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์ž‘์€ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„ ์Šน๋ฆฌ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

    private static int calculateWinningPercent() {
        int count = 0;

        makeArrAB(); 

        Collections.sort(arrB);
        for (int i = 0; i < arrA.size(); i++) {
            // ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ arrB์— number ๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๊ฐ€ ๋ช‡๊ฐœ ์žˆ๋Š”์ง€ ๊ตฌํ•œ๋‹ค.
            int number = arrA.get(i);

            int left = 0, right = arrB.size() - 1;

            int index = Integer.MIN_VALUE;
            while (left <= right) {
                int middle = (left + right) / 2;

                if (arrB.get(middle) < number) { // ์กฐ๊ฑด ๋งŒ์กฑ: number ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•จ!! ๊ฐ™์•„๋„ ์•ˆ๋จ.
                    left = middle + 1;
                    index = Math.max(index, middle);
                } else {
                    right = middle - 1;
                }
            }
            if (index != Integer.MIN_VALUE) {
                count += index + 1;
            }
        }
        return count;
    }

makeArrAB ๋ฉ”์„œ๋“œ

ํ˜„์žฌ ์„ ํƒ๋œ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‘ ๊ทธ๋ฃน(arrA, arrB)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
๊ฐ๊ฐ์˜ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ˆซ์ž ํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

    private static int calculateWinningPercent() {
        int count = 0;

        makeArrAB(); 

        Collections.sort(arrB);
        for (int i = 0; i < arrA.size(); i++) {
            // ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ arrB์— number ๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๊ฐ€ ๋ช‡๊ฐœ ์žˆ๋Š”์ง€ ๊ตฌํ•œ๋‹ค.
            int number = arrA.get(i);

            int left = 0, right = arrB.size() - 1;

            int index = Integer.MIN_VALUE;
            while (left <= right) {
                int middle = (left + right) / 2;

                if (arrB.get(middle) < number) { // ์กฐ๊ฑด ๋งŒ์กฑ: number ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•จ!! ๊ฐ™์•„๋„ ์•ˆ๋จ.
                    left = middle + 1;
                    index = Math.max(index, middle);
                } else {
                    right = middle - 1;
                }
            }
            if (index != Integer.MIN_VALUE) {
                count += index + 1;
            }
        }
        return count;
    }

makeArr ๋ฉ”์„œ๋“œ

์ฃผ์–ด์ง„ ์ฃผ์‚ฌ์œ„ ๋ฐฐ์—ด์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ˆซ์ž ํ•ฉ์„ ์žฌ๊ท€์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

    public static void makeArr(int depth, int[][] dice, int sum, List<Integer> arr) {
        if (depth == N / 2) {
            arr.add(sum);
            return;
        }
        for (int i = 0; i < 6; i++) {
            int newSum = sum + dice[depth][i];
            makeArr(depth + 1, dice, newSum, arr);
        }
    }

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ & ํ•˜์†Œ์—ฐ

์š”์ฆ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ธฐ์ถœ ๋ฌธ์ œ์—์„œ ์ด๋ถ„ํƒ์ƒ‰์ด ๋‚˜์˜ค๋ฉด ํ•ญ์ƒ ์กฐํ•ฉ ๊ด€๋ จ๋œ ๋ณตํ•ฉ ๋ฌธ์ œ๊ฐ€ ์ž์ฃผ ๋‚˜์˜ค๋„ค์š”.
์ถ”์„ธ๊ฐ€ ์—ฌ๋Ÿฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์„ ์š”๊ตฌํ•˜๋‚˜ ๋ด…๋‹ˆ๋‹ค.
์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ์ข€ ์ฐพ์•„๋‹ค๋…€์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿค”
+
์ˆœ์ฐจ์ ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•ด๊ฐ€๋ฉด์„œ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ, ์ค‘๊ฐ„์ค‘๊ฐ„ ๋กœ์ง์ด ์ž˜ ๊ตฌํ˜„์ด ์•ˆ๋˜๋„ค์š” ใ… 
์š”์ฆ˜ ๊ฐœ๋ฐœ๋งŒํ•ด์„œ ๊ทธ๋Ÿฐ์ง€ ํ•ญ์ƒ ๋ง๋ž‘๋ง๋ž‘ํ•œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก
์ด๋Ÿฐ ๊ตฌํ˜„ ๋ฌธ์ œ ์œ„์ฃผ๋กœ ๊ณ„์† ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค

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.

ํ—ฃํ—ˆ ์˜ˆ์ „์— ์ œ๊ฐ€ ํ’€์—ˆ๋˜ ๋ฌธ์ œ... ์ •๋ง ๋จธ๋ฆฌ ์‹ธ๋งค๊ฐ€๋ฉด์„œ ํ’€์—ˆ์—ˆ๋Š”๋ฐ์š”...
๋ˆˆ๋ฌผ๊ฒจ์šด ์ €์˜ ๋˜ฅ๊ผฌ์‡ผ๋ฅผ ๋ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค...

@9kyo-hwang 9kyo-hwang removed the request for review from Dolchae July 25, 2024 08:11
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.

๋Œ€๋žต 2์‹œ๊ฐ„ ์ข€ ๋„˜๊ฒŒ ๊ณ ๋ฏผํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์ง€์ €๋ถ„ํ•˜๋„ค์š” ...
ํ…Œ์ผ€๋„ 8๊ฐœ ์ •๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚ฌ์ง€๋งŒ ์šฐ์„  ์˜ฌ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

from itertools import combinations, product

def solution(dice):
    n = len(dice)
    list_comb = list(combinations(dice, n//2))
    dice_num = list(combinations([p for p in range(1, n+1)], n//2))
    result = [0] * len(list_comb)

    for a in range(len(list_comb)):
        b = [x for x in dice if not x in list_comb[a]]
        A_comb = list(product(*list_comb[a]))
        B_comb = list(product(*b))
        for i in range(len(A_comb)):
            for j in range(len(B_comb)):
                if sum(A_comb[i]) > sum(B_comb[j]):
                    result[a] += 1

    answer = dice_num[result.index(max(result))]
    return answer

๋ญ”๊ฐ€ ๋” ๊น”๋”ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์•„์ง์€ ์—ญ๋ถ€์กฑ์ด๋„ค์š”. ๋” ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค!!
์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค~!

@9kyo-hwang 9kyo-hwang removed the request for review from gjsk132 August 11, 2024 12:59
@9kyo-hwang 9kyo-hwang merged commit 498a7e5 into main Aug 11, 2024
8 checks passed
@9kyo-hwang 9kyo-hwang deleted the origin/5-mjj111 branch August 11, 2024 12:59
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