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

26-9kyo-hwang #104

Merged
merged 1 commit into from
Feb 15, 2024
Merged

26-9kyo-hwang #104

merged 1 commit into from
Feb 15, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Feb 11, 2024

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

2024 KAKAO WINTER INTERSHIP ๋ฌธ์ œ 3: ์ฃผ์‚ฌ์œ„ ๊ณ ๋ฅด๊ธฐ

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

2์‹œ๊ฐ„ + a?

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

1. ์ดํ•ด

n๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋ฅผ A, B๊ฐ€ ๊ฐ๊ฐ n/2๊ฐœ์”ฉ ๋‚˜๋ˆ  ๊ฐ–๋Š”๋‹ค. ๊ฐ๊ฐ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๋ชจ๋‘ ๊ตด๋ ค, ๋‚˜์˜จ ์ˆ˜๋“ค์„ ํ•ฉํ•ด ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•œ๋‹ค. ๋‘˜ ์ค‘ ํฐ ์ชฝ์ด ์Šน๋ฆฌํ•œ๋‹ค. ์ด ๋•Œ A๊ฐ€ ์Šน๋ฆฌํ•  ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ์ฃผ์‚ฌ์œ„ ์กฐํ•ฉ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

๋‹ค์Œ์€ n = 4์ธ ์˜ˆ์‹œ์ด๋‹ค.

์ฃผ์‚ฌ์œ„ ๊ตฌ์„ฑ
#1 [1, 2, 3, 4, 5, 6]
#2 [3, 3, 3, 3, 4, 4]
#3 [1, 3, 3, 4, 4, 4]
#4 [1, 1, 4, 4, 5, 5]
  • ์˜ˆ๋ฅผ ๋“ค์–ด A๊ฐ€ ์ฃผ์‚ฌ์œ„ 1๋ฒˆ๊ณผ 2๋ฒˆ์„ ๊ฐ€์ ธ๊ฐ„ ๋’ค 6, 3์„ ๊ตด๋ฆฌ๊ณ , B๊ฐ€ ์ฃผ์‚ฌ์œ„ 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ๊ฐ€์ ธ๊ฐ„ ๋’ค 4, 1์„ ๊ตด๋ฆฐ๋‹ค๋ฉด A์˜ ์Šน๋ฆฌ์ด๋‹ค. (6 + 3 > 4 + 1)

A๊ฐ€ ๊ฐ€์ ธ๊ฐ€๋Š” ์ฃผ์‚ฌ์œ„ ์กฐํ•ฉ์— ๋”ฐ๋ผ, ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ 1296๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ŠนํŒจ ๊ฒฐ๊ณผ๋ฅผ ์„ธ์–ด๋ณด๋ฉด ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™๋‹ค.

A์˜ ์ฃผ์‚ฌ์œ„ ์Šน ๋ฌด ํŒจ
#1, #2 596 196 504
#1, #3 560 176 560
#1, #4 616 184 496
#2, #3 496 184 616
#2, #4 560 176 560
#3, #4 504 196 596

A๊ฐ€ ์Šน๋ฆฌํ•  ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์•„์ง€๊ธฐ ์œ„ํ•ด์„  ์ฃผ์‚ฌ์œ„ 1๋ฒˆ๊ณผ 4๋ฒˆ์„ ๊ฐ€์ ธ๊ฐ€์•ผ ํ•œ๋‹ค.

2. ์„ค๋ช…

๏ฟฝํฌ๊ฒŒ 2๊ฐ€์ง€๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. 1. A๊ฐ€ ๊ฐ€์ ธ๊ฐˆ ์ฃผ์‚ฌ์œ„ ์„ ํƒ 2. ๊ฐ€์ ธ๊ฐ„ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๊ฒฐ๊ณผ ์„ธ๊ธฐ

1. A๊ฐ€ ๊ฐ€์ ธ๊ฐˆ ์ฃผ์‚ฌ์œ„ ์„ ํƒ

์ฃผ์–ด์ง€๋Š” ์ฃผ์‚ฌ์œ„ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 10๊ฐœ๋ฐ–์— ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, A๊ฐ€ ๊ฐ€์ ธ๊ฐˆ ์ฃผ์‚ฌ์œ„๋ฅผ ์„ ํƒํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ์ฐพ๋Š”๋‹ค.
n๊ฐœ์˜ ์ฃผ์‚ฌ์œ„ ์ค‘ n/2๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ธ๋ฐ, n์ด ์ตœ๋Œ“๊ฐ’์ผ ๋•Œ 10๊ฐœ์˜ ์ฃผ์‚ฌ์œ„ ์ค‘ 5๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋Š” 252์ด๋ฏ€๋กœ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•˜๋‹ค.

def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))  # ์ฃผ์‚ฌ์œ„ ๋ฒˆํ˜ธ๋“ค์„ ๊ธฐ๋กํ•ด๋‘” set
    for A in combinations(range(n), n // 2):  # itertools์˜ combinations๋ฅผ ์ด์šฉํ•ด (0 ~ n-1) ์ธ๋ฑ์Šค n๊ฐœ ์ค‘ n//2๊ฐœ๋ฅผ ์„ ํƒ
        B: Set[int] = dice_indices.difference(A)  # B๋Š” A๊ฐ€ ๊ณ ๋ฅธ ์ธ๋ฑ์Šค์˜ ๋‚˜๋จธ์ง€
        ...

2. ๊ฐ€์ ธ๊ฐ„ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๊ฒฐ๊ณผ ์„ธ๊ธฐ

์ด ๋ถ€๋ถ„์ด ๋ฌธ์ œ๋‹ค. ๊ฐ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ด A๊ฐ€ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋ฉด 6^n์ด๋ฏ€๋กœ ์ตœ๋Œ€ 252 * 6^10 = 15,237,476,352๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ ํ„ฐ์ง„๋‹ค.
์ด ๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ•  ์ง€ ๊ฐ์„ ๋ชป์žก์•„์„œ ์‹œ๊ฐ„์„ ์™•์™• ๋‚ ๋ ค๋ฒ„๋ ธ๊ณ ... ์งˆ๋ฌธํ•˜๊ธฐ ๊ฒŒ์‹œํŒ์˜ ๊ธ€์„ ๋ณด๊ณ ๋‚˜์„œ์•ผ ํ•ด๊ฒฐํ–ˆ๋‹ค ํ‘ํ‘

๋ฐฉ๋ฒ•์€ A, B๋ฅผ ๊ฐ๊ฐ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
๋จผ์ € A์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๊ฒฐ๊ณผ๋งŒ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ด 6^(n/2) ๊ฐ€์ง€์— ๋Œ€ํ•ด ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ์„ ์ €์žฅํ•ด ๋‘๊ณ , ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ B์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๊ฒฐ๊ณผ 6^(n/2) ๊ฐ€์ง€์— ๋Œ€ํ•ด ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ์„ ์ €์žฅํ•ด ๋‘”๋‹ค.

def get_count_of_each_combo(dices, indices):
    num_combos: Dict[int, int] = defaultdict(int)
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))  # i๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„ j๋ฒˆ์งธ ๋ˆˆ
        num_combos[sum_combo] += 1
    return sorted(num_combos.items())

indices๊ฐ€ A, B๊ฐ€ ๊ฐ๊ฐ ๊ณ ๋ฅธ ์ฃผ์‚ฌ์œ„๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ์ด๋‹ค. itertools์˜ product๋ฅผ ์ด์šฉํ•ด ์ฃผ์‚ฌ์œ„ ๋ˆˆ(0 ~ 5) ์ˆœ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
indices์™€ pips๋ฅผ ๋™์‹œ์— ์ˆœํšŒํ•ด i๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„์˜ j๋ฒˆ์งธ ๋ˆˆ์„ ๋ชจ๋‘ ๊ตฌํ•ด ๊ทธ๊ฒƒ์˜ ํ•ฉ์„ dictionary์— countingํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด num_combos๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์—๋Š” ์ตœ์ข…์ ์œผ๋กœ {ํ•ฉ1: ๊ฐœ์ˆ˜, ํ•ฉ2: ๊ฐœ์ˆ˜, ...}์™€ ๊ฐ™์ด ์ €์žฅ๋œ๋‹ค.
1๋ฒˆ | [1, 2, 3, 4, 5, 6], 2๋ฒˆ | [3, 3, 3, 3, 4, 4] <- ์ด 2๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ณจ๋ž๋‹ค๊ณ  ํ•˜๋ฉด {4: 4, 5: 6, 6: 6, 7: 6, 8: 6, 9: 6, 10: 2}๊ฐ€ ์ €์žฅ๋œ๋‹ค.
์ด๋ฅผ key, ์ฆ‰ ํ•ฉ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ A, B์— ๋Œ€ํ•ด ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ–ˆ๋‹ค๋ฉด, A๊ฐ€ ๊ฐ€์ง„ ์ฃผ์‚ฌ์œ„๋กœ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฐ ํ•ฉ x์— ๋Œ€ํ•ด, x๋ณด๋‹ค ์ž‘์€ B๊ฐ€ ๊ฐ€์ง„ ์ฃผ์‚ฌ์œ„๋กœ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฐ ํ•ฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

        A_combos: List[int] = get_count_of_each_combo(dice, A)
        B_combos: List[int] = get_count_of_each_combo(dice, B)
        
        win_cnt: int = 0
        for A_combo_sum, A_combo_cnt in A_combos:
            for B_combo_sum, B_combo_cnt in B_combos:
                if B_combo_sum >= A_combo_sum:
                    break
                    
                win_cnt += A_combo_cnt * B_combo_cnt

๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” 6^(n/2) = m์ด๋ผ ํ•  ๋•Œ, ์ •๋ ฌ ๊ณผ์ •์ด ์žˆ์œผ๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” $O(mlogm)$์ด๋‹ค. ์ตœ๋Œ€๋Š” n = 10์ผ ๋•Œ์ด๋ฏ€๋กœ 6^5 = 7,776์ด๊ณ , ์ด๋Š” ๊ฐ€๋Šฅํ•œ ์ฃผ์‚ฌ์œ„ ์กฐํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’์ธ 252๋ฅผ ๊ณฑํ•ด๋„ ์ถฉ๋ถ„ํžˆ ๋น ๋ฅด๋‹ค.

3. ์ „์ฒด ์ฝ”๋“œ

  • python
from itertools import combinations, product
from collections import defaultdict


def get_count_of_each_combo(dices, indices):
    num_combos: Dict[int, int] = defaultdict(int)
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))  # i๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„ j๋ฒˆ์งธ ๋ˆˆ
        num_combos[sum_combo] += 1
    return sorted(num_combos.items())


def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))
    max_win_cnt: int = 0
    for A in combinations(range(n), n // 2):
        B: Set[int] = dice_indices.difference(A)
        
        A_combos: List[Tuple[int, int]] = get_count_of_each_combo(dice, A)
        B_combos: List[Tuple[int, int]] = get_count_of_each_combo(dice, B)
        
        win_cnt: int = 0
        for A_combo_sum, A_combo_cnt in A_combos:
            for B_combo_sum, B_combo_cnt in B_combos:
                if B_combo_sum >= A_combo_sum:
                    break
                    
                win_cnt += A_combo_cnt * B_combo_cnt
        
        if win_cnt > max_win_cnt:
            max_win_cnt = win_cnt
            answer = A
        
    return [i + 1 for i in answer]
  • c++
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

void CountEachComboSums(const vector<vector<int>>& Dices, const vector<int>& SelectedDices, unordered_map<int, int>& DiceComboMap, int ComboSum = 0, int DiceIndex = 0)
{
    if(DiceIndex == SelectedDices.size())
    {
        DiceComboMap[ComboSum] += 1;
        return;
    }
    
    for(const int& DicePip : Dices[SelectedDices[DiceIndex]])
    {
        CountEachComboSums(Dices, SelectedDices, DiceComboMap, ComboSum + DicePip, DiceIndex + 1);
    }
}

vector<int> solution(vector<vector<int>> Dice) 
{
    const int NumDice = Dice.size();
    vector<bool> IsADice(NumDice / 2, false);
    IsADice.insert(IsADice.end(), NumDice / 2, true);
    
    int MaxCntOfAWins = 0;
    vector<int> Answer;
    do 
    {
        vector<int> A, B;
        for(int i = 0; i < NumDice; ++i)
        {
            IsADice[i] ? A.emplace_back(i) : B.emplace_back(i);
        }
        
        unordered_map<int, int> AComboMap, BComboMap;
        CountEachComboSums(Dice, A, AComboMap);
        CountEachComboSums(Dice, B, BComboMap);
        
        vector<pair<int, int>> AComboVector(AComboMap.begin(), AComboMap.end());
        vector<pair<int, int>> BComboVector(BComboMap.begin(), BComboMap.end());
        
        sort(AComboVector.begin(), AComboVector.end());
        sort(BComboVector.begin(), BComboVector.end());
        
        int CntOfAWins = 0;
        for(const auto& [AComboSum, AComboCnt] : AComboVector)
        {
            for(const auto& [BComboSum, BComboCnt] : BComboVector)
            {
                if(BComboSum >= AComboSum)
                {
                    break;
                }
                
                CntOfAWins += AComboCnt * BComboCnt;
            }
        }
        
        if(CntOfAWins > MaxCntOfAWins)
        {
            MaxCntOfAWins = CntOfAWins;
            Answer = A;
        }
    } while(next_permutation(IsADice.begin(), IsADice.end()));
    
    transform(Answer.begin(), Answer.end(), Answer.begin(), [](int& i) { return i + 1; });
    return Answer;
}

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

์ž๊ธฐ ์ง์ „์— PR์„ ์จ์„œ ์„ค๋ช…์ด ๋ถ€์‹คํ•œ ์  ์–‘ํ•ดํ•ด์ฃผ์„ธ์š”... ใ…œ
์งˆ๋ฌธ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๋ถ€๊ฐ€ ์„ค๋ช… ๊ณ ๋ด‰๋ฐฅ์œผ๋กœ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

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.

image

image

๋•๋ถ„์— ์ •๋ง ๊ณ ๋ฏผ, ์‚ฝ์งˆ ๋งŽ์ดํ–ˆ๊ณ ์š”

๋„์ €ํžˆ ์•„๋‹ˆ๋‹ค ์‹ถ์–ด์„œ PR ๋ดค์”๋‹ˆ๋‹ค.



PR์„ ๋ณด๋‹ˆ๊นŒ ์ œ๊ฐ€ ์ฐฉ๊ฐํ•˜๊ณ  ์žˆ๋–ค ๋ถ€๋ถ„์ด

์ €๋Š” $10C5$๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ์–ด์ฐจํ”ผ ์ƒ๋Œ€๋ฐฉ์€ ์ž๋™์ ์œผ๋กœ ๋‚˜๋จธ์ง€ 5๊ฐœ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜๋Š”๋ฐ,

๊ทธ๊ฑธ ๊ฐ„๊ณผํ•˜๊ณ  252๊ฐœ์˜ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ,

A๊บผ 1๊ฐœ ์ œ์™ธํ•˜๊ณ  B๊ฐ€ 251๊ฐœ์˜ ๊ฒฝ์šฐ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ฐฉ๊ฐํ•ด์„œ ๊ณ„์† $252 X 251$์ด ์ „์ฒด ๊ฒฝ์šฐ๋ผ๊ณ  ์ฐฉ๊ฐํ–ˆ๋„ค์š”...

๐Ÿ™ƒ๐Ÿ™ƒ๐Ÿ™ƒ๐Ÿ™ƒ๐Ÿ™ƒ๐Ÿ™ƒ

๋„ˆ๋ฌด ์šฐ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค..




์ €๋„ PR์ฒ˜๋Ÿผ ์ ‘๊ทผ์„ ํ• ๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ,




image




์œ„ ๋ถ€๋ถ„์—์„œ ๊ฐ€์žฅ worst ์ผ€์ด์Šค ์ผ ๊ฒฝ์šฐ,

$6^5 = 7739$๊ฐœ์ด๋ฏ€๋กœ

$7739 * 7739 = 60,466,176$ (์•ฝ 6์ฒœ๋งŒ) ๋ฒˆ์„ ์ˆœํšŒํ•˜์ง€ ์•Š๋‚˜์š”?

๊ทธ๋Ÿฌ๋ฉด $10C5$ ์ธ 252์™€ ๊ณฑํ–ˆ์„ ๋•Œ ํƒ€์ž„์•„์›ƒ์ด ๋‚˜์ง€ ์•Š๋‚˜์š”?




ํ˜น์‹œ ์ œ๊ฐ€ ์ฐฉ๊ฐํ•˜๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์ฐ์–ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

@9kyo-hwang
Copy link
Collaborator Author

๊ฐ€์žฅ worst ์ผ€์ด์Šค ์ผ ๊ฒฝ์šฐ,

65=7739๊ฐœ์ด๋ฏ€๋กœ

7739โˆ—7739=60,466,176 (์•ฝ 6์ฒœ๋งŒ) ๋ฒˆ์„ ์ˆœํšŒํ•˜์ง€ ์•Š๋‚˜์š”?

๊ทธ๋Ÿฌ๋ฉด 10C5 ์ธ 252์™€ ๊ณฑํ–ˆ์„ ๋•Œ ํƒ€์ž„์•„์›ƒ์ด ๋‚˜์ง€ ์•Š๋‚˜์š”?

ํ˜น์‹œ ์ œ๊ฐ€ ์ฐฉ๊ฐํ•˜๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์ฐ์–ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ง€๊ธˆ ์ฝ”๋“œ๋กœ๋Š” ์ •ํ™•ํžˆ ์‹œ๊ฐ„ ๊ณ„์‚ฐ์ด ํž˜๋“ค์–ด์„œ, [๊ฐ ์กฐํ•ฉ ๋ณ„ ํ•ฉ, ํ•ฉ์ด ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜] dictionary๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  [๊ฐ ์กฐํ•ฉ ๋ณ„ ํ•ฉ1, ํ•ฉ2, ...] ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ทธ๋ƒฅ ์ญ‰ ๋‚˜์—ดํ•œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•จ.
๊ฐ€์žฅ ๋งŽ์€ n = 10, ์ฆ‰ ๊ฐ๊ฐ ์ฃผ์‚ฌ์œ„๋ฅผ 5๊ฐœ์”ฉ ๋“ค๊ณ ๊ฐ„ ๊ฒฝ์šฐ๋กœ ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, A, B๋Š” ๊ฐ๊ฐ ๊ธธ์ด๊ฐ€ 7739์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์Œ. ์ด ๋•Œ A๊ฐ€ B์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ์šฐ์Šนํ–ˆ๋Š” ์ง€๋Š” ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ฒ€์‚ฌํ•จ. ๊ทธ๋Ÿฌ๋ฉด 7739 * 7739๊ฐ€ ์•„๋‹Œ $7739 log(7739)$๊ฐ€ ๋ผ์„œ 7739 * 3.88868484666... = ์•ฝ 31,000์ด ์•ˆ๋จ. ๊ทธ๋ž˜์„œ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•จ

from itertools import combinations, product
from collections import defaultdict
from typing import *
from bisect import bisect_left


def get_list_of_combo_sum(dices, indices):
    list_combos: List[int] = []
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))  # i๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„ j๋ฒˆ์งธ ๋ˆˆ
        list_combos.append(sum_combo)
    return sorted(list_combos)


def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))
    max_win_cnt: int = 0
    for A in combinations(range(n), n // 2):
        B: Set[int] = dice_indices.difference(A)
        
        A_list: List[int] = get_list_of_combo_sum(dice, A)
        B_list: List[int] = get_list_of_combo_sum(dice, B)
        
        win_cnt: int = 0
        for A_combo_sum in A_list:
            win_cnt += bisect_left(B_list, A_combo_sum)
        
        if win_cnt > max_win_cnt:
            max_win_cnt = win_cnt
            answer = A
        
    return [i + 1 for i in answer]

๊ทธ ์ฝ”๋“œ๊ฐ€ ์ด๊ฑด๋ฐ,
image
๋นก์„ผ ํ…Œ์ผ€ ํ†ต๊ณผ ์‹œ๊ฐ„์ด ์ด๋งŒํผ ๊ฑธ๋ฆผ.

๊ทธ๋ฆฌ๊ณ  ๋ณธ๋ฌธ์— ์ฝ”๋“œ๋กœ๋Š”
image
์ด๋งŒํผ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ. 1์ดˆ ๊ฐ€๊นŒ์ด ์ฐจ์ด๋‚˜๋Š” ๊ฑธ ๋ณด๋ฉด ๊ฐ ์กฐํ•ฉ ๋ณ„ ํ•ฉ์„ ์นด์šดํŒ…ํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฒŒ ์ƒ๊ฐ๋ณด๋‹ค ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๋ฅผ ๋งŽ์ด ์••์ถ•ํ•ด์ฃผ๋‚˜ ๋ด„.

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 13, 2024

์ด ๋•Œ A๊ฐ€ B์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ์šฐ์Šนํ–ˆ๋Š” ์ง€๋Š” ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ฒ€์‚ฌํ•จ.

์ด ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜์–ด์š”.................. ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๊ฑฐ์ฃ 

@9kyo-hwang
Copy link
Collaborator Author

์ด ๋•Œ A๊ฐ€ B์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ์šฐ์Šนํ–ˆ๋Š” ์ง€๋Š” ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ฒ€์‚ฌํ•จ.

์ด ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜์–ด์š”.................. ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๊ฑฐ์ฃ 

1๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ธฐ์ค€์œผ๋กœ, A๊ฐ€ 1๋ฒˆ/2๋ฒˆ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ฐ€์ ธ๊ฐ”๊ณ  B๊ฐ€ 3๋ฒˆ/4๋ฒˆ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ฐ€์ ธ๊ฐ”์„ ๋•Œ ๋Œ“๊ธ€์˜ get_list_of_combo_sum(dices, indices) ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด A_list์™€ B_list์—๋Š” ๊ฐ๊ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์Œ.

A: [4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10]
B: [2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9]

๋ง ๊ทธ๋ž˜๋„ ๋ชจ๋“  ๋ˆˆ ์กฐํ•ฉ์— ๋Œ€ํ•œ ํ•ฉ์ด ๋‹ค ๋“ค์–ด๊ฐ€์žˆ๋Š” 1์ฐจ์› ๋ฆฌ์ŠคํŠธ์ž„.

์ด์ œ ๊ฐ A ์›์†Œ x์— ๋Œ€ํ•ด B_list์—์„œ x๋ณด๋‹ค ํฐ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ๋˜๋Š”๋ฐ,

win_cnt: int = 0
for A_combo_sum in A_list:
    win_cnt += bisect_left(B_list, A_combo_sum)

์ด๋ฅผ bisect_left๋ผ๋Š” ์ด์ง„ํƒ์ƒ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์œ„์น˜๋ฅผ ์ฐพ์•„๋ƒ„.
์„ ํ˜• ํƒ์ƒ‰์ด๋ผ๋ฉด 0๋ฒˆ ์›์†Œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ํ™•์ธํ•ด์„œ x๋ณด๋‹ค ํฐ ์›์†Œ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ counting์„ ํ•˜๊ฒ ์ง€๋งŒ, ์ด์ง„ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด x๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ์›์†Œ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„๋‚ด๋ฉด ๊ทธ ์ธ๋ฑ์Šค๊ฐ€ ๊ณง x๋ณด๋‹ค ์ž‘์€ ์›์†Œ ๊ฐœ์ˆ˜์ด๋‹ˆ๊นŒ ๊ทธ๊ฑฐ๋ฅผ ๋”ํ•˜๋ฉด ๋จ.

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

๋ญ”๊ฐ€ ์ฝ”๋“œ ์งœ๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ๊ฐ ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ์—์„œ๋ถ€ํ„ฐ ์‹œ๊ฐ„ ํ„ฐ์ง€๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ์ƒ๊ฐ ์•ˆ ๋‚˜์„œ ์–ด์ฉŒ์ง€ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ PR์„ ๋ดค์Šต๋‹ˆ๋‹ค...
PR์„ ๋ณด๊ณ ๋‚˜์„œ์•ผ ํ•˜๋‚˜๋‘˜์”ฉ ์ดํ•ด๊ฐ€๋„ค์š”...

์ฝ”๋“œ ์ฝ๋‹ค๊ฐ€ ๋ญ”๊ฐ€ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์ด๊ฒƒ์ €๊ฒƒ ์ƒ๊ฐ๋‚˜์„œ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋“ค๊ณ ๊ฐ€์„œ ์ด๊ฒƒ์ €๊ฒƒ ์‹คํ—˜ํ–ˆ์Šต๋‹ˆ๋‹ค

image

๊ฒฐ๋ก ์ ์œผ๋กœ... ๊ฝค๋‚˜ ์„ฑ๊ณต์ ์ธ ๊ฒƒ ๊ฐ™์•„์š”

๋ˆ„์ ํ•ฉ

๋ญ”๊ฐ€ ์ค‘๊ฐ„์— for ๋ฐ˜๋ณต๋ฌธ 2๋ฒˆ ์“ฐ๋Š” ๋ถ€๋ถ„์„ ๋ˆ„์ ํ•ฉ์œผ๋กœ ํ•˜๋ฉด ๋” ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๋“ค์ง€ ์•Š์„๊นŒํ•ด์„œ ์‹œ๋„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.
B_combo๋ฅผ ๋ˆ„์ ํ•ฉ์„ ์‚ฌ์šฉํ•ด์„œ ํŠน์ • ๋ˆˆ์˜ ํ•ฉ ์ดํ•˜์ธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
( ๋ˆ„์ ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด ์ค„ ๊ฒฝ์šฐ์—๋Š” ๊ตณ์ด ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์ผ ํ•„์š”๊ฐ€ ์—†์–ด์„œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋‚จ๊ฒจ๋’€์Šต๋‹ˆ๋‹ค. )
A_combo_sum์ด 5์ผ ๋•Œ, ๋ˆ„์ ํ•ฉ์œผ๋กœ ๋ฏธ๋ฆฌ ๊ตฌํ•œ 4 ์ดํ•˜์ธ ์ˆ˜๋ฅผ ๋ฐ”๋กœ ๊ณฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

from itertools import combinations, product
from collections import defaultdict, deque


def get_count_of_each_combo(dices, indices):
    num_combos: Dict[int, int] = defaultdict(int)
    
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))
        num_combos[sum_combo] += 1
        
    return num_combos

# ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ
def change_sort_list(combo):
    return sorted(combo.items())

# ๋ˆ„์ ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
def make_prefix_sum(combo, cnt):
    num_combos, limit = combo, max(combo.keys())
    for i in range(2,cnt):
        num_combos[i] += num_combos[i-1]
    
    return num_combos
    

def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))
    max_win_cnt: int = 0
    for A in combinations(range(n), n // 2):
        B: Set[int] = dice_indices.difference(A)
        
	# A๋Š” ์›๋ž˜ ํ˜•์‹์˜ ๋ฆฌ์ŠคํŠธ๋กœ, B๋ฅผ ๋ˆ„์ ํ•ฉ์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
        A_combos: List[Tuple[int, int]] = change_sort_list(get_count_of_each_combo(dice, A))
        B_combos: Dict[int, int] = make_prefix_sum(get_count_of_each_combo(dice, B), A_combos[-1][0])
        win_cnt: int = 0
	
	# ๋ˆ„์ ํ•ฉ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”๋กœ ์ ‘๊ทผํ•˜๊ธฐ
        for A_combo_sum, A_combo_cnt in A_combos:
            win_cnt += A_combo_cnt * B_combos[A_combo_sum-1]
        
        if win_cnt > max_win_cnt:
            max_win_cnt = win_cnt
            answer = A
        
    return [i + 1 for i in answer]
์กฐํ•ฉ์˜ ์ ˆ๋ฐ˜
`A`๊ฐ€ [1, 4]์ผ ๋•Œ๋Š” `A`๊ฐ€ [2, 3]์ผ ๋•Œ์˜ `B`์˜ ๊ฒฐ๊ณผ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์กฐํ•ฉ์„ ์ ˆ๋ฐ˜๋งŒ ํ•ด์ค˜๋„ ๊ฒฐ๊ณผ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  combinations์—์„œ ์ •๋ฐ˜๋Œ€๊ฐ€ ๋˜๋Š” ์ธ๋ฑ์Šค๋Š” ๊ฒฐ๊ณผ๋“ค์„ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ–ˆ์„ ๋•Œ, ์ค‘์•™์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜๋Œ€ํŽธ์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, ์ ˆ๋ฐ˜๋งŒ ํƒ์ƒ‰ํ•ด์ค˜๋„ ์ถฉ๋ถ„ํžˆ ๊ฒฐ๊ณผ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!
( ์ถ”๊ฐ€๋กœ B_win_cnt๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฌด์Šน๋ถ€์ธ ๊ฒฝ์šฐ๋ฅผ ๋นผ๊ณ , ๋งˆ์ง€๋ง‰์— A_win_cnt๋ฅผ ๋นผ์คฌ์Šต๋‹ˆ๋‹ค.

from itertools import combinations, product
from collections import defaultdict


def get_count_of_each_combo(dices, indices):
    num_combos: Dict[int, int] = defaultdict(int)
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))
        num_combos[sum_combo] += 1
    return sorted(num_combos.items())


def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))
    max_win_cnt: int = 0
    for A in combinations(range(n), n // 2):
        
	# ์ ˆ๋ฐ˜์ด ์ง€๋‚œ ์œ„์น˜์—์„œ ๋ฐ˜๋ณต์„ ์ค‘๋‹จ
        if A[0]:
            break
        
        B: Set[int] = dice_indices.difference(A)
        
        A_combos: List[Tuple[int, int]] = get_count_of_each_combo(dice, A)
        B_combos: List[Tuple[int, int]] = get_count_of_each_combo(dice, B)
        
        A_win_cnt: int = 0
	
	# ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์ ธ์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ด ๊ฒฝ์šฐ์˜ ์ˆ˜
        B_win_cnt: int = 6**n
        
        for A_combo_sum, A_combo_cnt in A_combos:
            for B_combo_sum, B_combo_cnt in B_combos:
		
                if B_combo_sum >= A_combo_sum:
		    # ๋งŒ์•ฝ ๋ฌด์Šน๋ถ€๋ผ๋ฉด B๊ฐ€ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ์—์„œ ๋นผ์ค€๋‹ค.
                    B_win_cnt -= (A_combo_cnt * B_combo_cnt) if B_combo_sum == A_combo_sum else 0
                    break
                
                A_win_cnt += A_combo_cnt * B_combo_cnt
        
	# ๋งˆ์ง€๋ง‰์œผ๋กœ A๊ฐ€ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋นผ์ค€๋‹ค.
        B_win_cnt -= A_win_cnt
        
	# A์˜ ๊ฒฐ๊ณผ์™€ B์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•ด์ค€๋‹ค.
        if A_win_cnt > max_win_cnt:
            max_win_cnt = A_win_cnt
            answer = A
            
        if B_win_cnt > max_win_cnt:
            max_win_cnt = B_win_cnt
            answer = B
        
    return [i + 1 for i in answer]
๋ˆ„์ ํ•ฉ + ์กฐํ•ฉ์˜ ์ ˆ๋ฐ˜
์œ„์—๊บผ 2๊ฐœ ์„ž์€ ํ˜ผ์ข…์ž…๋‹ˆ๋‹ค.
from itertools import combinations, product
from collections import defaultdict, deque


def get_count_of_each_combo(dices, indices):
    num_combos: Dict[int, int] = defaultdict(int)
    
    for pips in product(range(6), repeat=len(indices)):
        sum_combo: int = sum(dices[i][j] for i, j in zip(indices, pips))  # i๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„ j๋ฒˆ์งธ ๋ˆˆ
        num_combos[sum_combo] += 1
        
    return num_combos

def change_sort_list(combo):
    return sorted(combo.items())

def make_prefix_sum(combo, cnt):
    num_combos, limit = combo, max(combo.keys())
    for i in range(2,cnt+1):
        num_combos[i] += num_combos[i-1]
    
    return num_combos
    

def solution(dice):
    answer: List[int] = []
    
    n: int = len(dice)
    dice_indices: Set[int] = set(range(n))
    max_win_cnt: int = 0
    for A in combinations(range(n), n // 2):
        
        if A[0]:
            break
        
        B: Set[int] = dice_indices.difference(A)
        
        A_combos: List[Tuple[int, int]] = change_sort_list(get_count_of_each_combo(dice, A))
        B_combos: Dict[int, int] = make_prefix_sum(get_count_of_each_combo(dice, B), A_combos[-1][0])
        
        A_win_cnt: int = 0
        B_win_cnt: int = 6**n
        for A_combo_sum, A_combo_cnt in A_combos:
            A_win_cnt += A_combo_cnt * B_combos[A_combo_sum-1]
            B_win_cnt -= A_combo_cnt * abs(B_combos[A_combo_sum] - B_combos[A_combo_sum-1])
            
        B_win_cnt -= A_win_cnt
            
        if A_win_cnt > max_win_cnt:
            max_win_cnt = A_win_cnt
            answer = A
        
        if B_win_cnt > max_win_cnt:
            max_win_cnt = B_win_cnt
            answer = B
        
    return [i + 1 for i in answer]

์—ด์‹ฌํžˆ ์”น๊ณ  ๋œฏ๊ณ  ๋ง›๋ณธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๊ทผ๋ฐ ๋ˆ„์ ํ•ฉ ์ฝ”๋“œ๋Š” ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ ธ์„œ ์˜คํžˆ๋ ค ๋ณด๊ธฐ ์•ˆ ์ข‹์€ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋„ค์š”... ๐Ÿค”

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 14, 2024

๋ญ”๊ฐ€ ์ฝ”๋“œ ์งœ๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ๊ฐ ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ์—์„œ๋ถ€ํ„ฐ ์‹œ๊ฐ„ ํ„ฐ์ง€๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ์ƒ๊ฐ ์•ˆ ๋‚˜์„œ ์–ด์ฉŒ์ง€ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ PR์„ ๋ดค์Šต๋‹ˆ๋‹ค... PR์„ ๋ณด๊ณ ๋‚˜์„œ์•ผ ํ•˜๋‚˜๋‘˜์”ฉ ์ดํ•ด๊ฐ€๋„ค์š”...

์ฝ”๋“œ ์ฝ๋‹ค๊ฐ€ ๋ญ”๊ฐ€ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์ด๊ฒƒ์ €๊ฒƒ ์ƒ๊ฐ๋‚˜์„œ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋“ค๊ณ ๊ฐ€์„œ ์ด๊ฒƒ์ €๊ฒƒ ์‹คํ—˜ํ–ˆ์Šต๋‹ˆ๋‹ค

image

๊ฒฐ๋ก ์ ์œผ๋กœ... ๊ฝค๋‚˜ ์„ฑ๊ณต์ ์ธ ๊ฒƒ ๊ฐ™์•„์š”

๋ˆ„์ ํ•ฉ
์กฐํ•ฉ์˜ ์ ˆ๋ฐ˜
๋ˆ„์ ํ•ฉ + ์กฐํ•ฉ์˜ ์ ˆ๋ฐ˜
์—ด์‹ฌํžˆ ์”น๊ณ  ๋œฏ๊ณ  ๋ง›๋ณธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ๋ˆ„์ ํ•ฉ ์ฝ”๋“œ๋Š” ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ ธ์„œ ์˜คํžˆ๋ ค ๋ณด๊ธฐ ์•ˆ ์ข‹์€ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋„ค์š”... ๐Ÿค”

๊ตํ™ฉ์ฟ  ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

Copy link

@LJEDD2 LJEDD2 left a comment

Choose a reason for hiding this comment

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 3 ๋ฌธ์ œ๋Š” ์ฒ˜์Œ ๋ณด๋Š”๋ฐ ๋‚œ์ด๋„๊ฐ€ ์ •๋ง ์‚ฌ์•…ํ•˜๋„ค์š” ^-^
์ ‘๊ทผ๋ฒ• ์ž์ฒด๋ฅผ ๋– ์˜ฌ๋ฆฌ๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์•„์„œ ์œ„์˜ ๊ต‡์ˆ˜๋ถ„๋“ค์˜ PR์„ ๋ณด๊ณ  ์ฐจ๊ทผ์ฐจ๊ทผ ๊ณต๋ถ€๋ฅผ ํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‹ค๋“ค ์–ด๋–ป๊ฒŒ ๋ฌธ์ œ์— ์ ‘๊ทผํ•˜๊ณ  ํ•ด๊ฒฐํ•˜๋Š”์ง€ ์—ฟ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์ •๋ง ์œ ์ตํ•˜๋„ค์š” :>
๋‚˜์˜๋‹˜์˜ ์‹œ๊ฐ„ ์ค„์ด๊ธฐ ๊ณผ์ •๋„ ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค ๋ถ€๋Ÿฝ๋„ค์š” ๐Ÿ˜ญ

์ด์ง„ํƒ์ƒ‰๊ณผ ์กฐํ•ฉ ๋ˆ„์ ํ•ฉ์„ ํ™œ์šฉํ•˜๋Š” ๊ฒŒ ์•„์ง ์ต์ˆ™ํ•˜์ง€๊ฐ€ ์•Š์€๋ฐ ๊ณต๋ถ€๋ฅผ ์กฐ๊ธˆ ๋” ๊นŠ๊ฒŒ ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค
์ข‹์€ ๋ฌธ์ œ ์บ„์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Ž๐Ÿ’ก

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 15, 2024

์ด ๋•Œ A๊ฐ€ B์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ์šฐ์Šนํ–ˆ๋Š” ์ง€๋Š” ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ฒ€์‚ฌํ•จ.

์ด ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜์–ด์š”.................. ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๊ฑฐ์ฃ 

1๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ธฐ์ค€์œผ๋กœ, A๊ฐ€ 1๋ฒˆ/2๋ฒˆ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ฐ€์ ธ๊ฐ”๊ณ  B๊ฐ€ 3๋ฒˆ/4๋ฒˆ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ฐ€์ ธ๊ฐ”์„ ๋•Œ ๋Œ“๊ธ€์˜ get_list_of_combo_sum(dices, indices) ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด A_list์™€ B_list์—๋Š” ๊ฐ๊ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์Œ.


A: [4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10]

B: [2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9]

๋ง ๊ทธ๋ž˜๋„ ๋ชจ๋“  ๋ˆˆ ์กฐํ•ฉ์— ๋Œ€ํ•œ ํ•ฉ์ด ๋‹ค ๋“ค์–ด๊ฐ€์žˆ๋Š” 1์ฐจ์› ๋ฆฌ์ŠคํŠธ์ž„.

์ด์ œ ๊ฐ A ์›์†Œ x์— ๋Œ€ํ•ด B_list์—์„œ x๋ณด๋‹ค ํฐ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ๋˜๋Š”๋ฐ,

win_cnt: int = 0

for A_combo_sum in A_list:

    win_cnt += bisect_left(B_list, A_combo_sum)

์ด๋ฅผ bisect_left๋ผ๋Š” ์ด์ง„ํƒ์ƒ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์œ„์น˜๋ฅผ ์ฐพ์•„๋ƒ„.

์„ ํ˜• ํƒ์ƒ‰์ด๋ผ๋ฉด 0๋ฒˆ ์›์†Œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ํ™•์ธํ•ด์„œ x๋ณด๋‹ค ํฐ ์›์†Œ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ counting์„ ํ•˜๊ฒ ์ง€๋งŒ, ์ด์ง„ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด x๋ณด๋‹ค ํฐ ์ฒซ ๋ฒˆ์งธ ์›์†Œ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„๋‚ด๋ฉด ๊ทธ ์ธ๋ฑ์Šค๊ฐ€ ๊ณง x๋ณด๋‹ค ์ž‘์€ ์›์†Œ ๊ฐœ์ˆ˜์ด๋‹ˆ๊นŒ ๊ทธ๊ฑฐ๋ฅผ ๋”ํ•˜๋ฉด ๋จ.

์™€์šฐ ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค ๐Ÿซก๐Ÿซก๐Ÿซก

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.

from itertools import combinations, product

def get_dice_sum_list(dice, idxes):
    sum_list = []
    
    for sum_idxes in product(range(6), repeat=len(idxes)):
        accumulate = 0

        for pick_dice_idx, sum_idx in zip(idxes,sum_idxes):
            accumulate += dice[pick_dice_idx][sum_idx]

        sum_list.append(accumulate)
    return sorted(sum_list)

def get_a_win_count(a_dice_sum_list, b_dice_sum_list):
    total_win_count = 0

    for a_element in a_dice_sum_list:
        left, right = 0, len(a_dice_sum_list)
        win_count = 0

        while left+1 < right:
            mid = (left+right)//2
            if check(a_element, b_dice_sum_list[mid]):
                left = mid
                win_count = mid

            else: right = mid

        total_win_count += win_count+1

    return total_win_count

def check(a,b):
    if a>b: return True
    return False

def solution(dice):
    n = len(dice)

    max_win_count = -1
    max_win_indexes = []
    for a_dice_idxes in combinations(range(n),n//2):
        b_dice_idxes = []
        for b_dice_idx in range(n):
            if b_dice_idx in a_dice_idxes: continue

            b_dice_idxes.append(b_dice_idx)


        a_dice_sum_list = get_dice_sum_list(dice, a_dice_idxes)
        b_dice_sum_list = get_dice_sum_list(dice, b_dice_idxes)

        win_count = get_a_win_count(a_dice_sum_list, b_dice_sum_list)

        if win_count > max_win_count: 
            max_win_count = win_count
            max_win_indexes = a_dice_idxes

    return list(map(lambda x : x+1,max_win_indexes))

dice = [[1, 2, 3, 4, 5, 6], [3, 3, 3, 3, 4, 4], [1, 3, 3, 4, 4, 4], [1, 1, 4, 4, 5, 5]]
print(solution(dice))

์•„์ด๊ณ  ............

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

์ €๋„ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 3 ๋ฌธ์ œ๋Š” ์ฒ˜์Œ ๋ณด๋Š”๋ฐ ์ •๋ง ์ •๋ง ์‰ฝ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค...๐Ÿ˜ญ๐Ÿ˜ญ
๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์ƒ๊ฐํ•ด๋‚ด๋Š”๋ฐ ํ•˜๋ฃจ๊ฐ€ ๊ฑธ๋ ค์„œ ๊ฒฐ๊ตญ ๊ตํ™ฉ๋‹˜์˜ PR์„ ๋ณด๊ณ  ์ฒœ์ฒœํžˆ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผํ•˜๋ฉด์„œ ์ดํ•ดํ–ˆ์–ด์š”๐Ÿฅน
๋„ˆ๋ฌด๋„ˆ๋ฌด ๋Œ€๋‹จํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!๐Ÿ‘

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

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.

6 participants