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

22-H0ngJu #225

Merged
merged 2 commits into from
Aug 21, 2024
Merged

22-H0ngJu #225

merged 2 commits into from
Aug 21, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Aug 6, 2024

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

์˜์ƒ

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

์‹œ๊ฐ„ ์žฌ๋Š”๊ฑธ ๊นŒ๋จน์Œ .. ์•„๋งˆ 1.5H?

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

image

๋ฌธ์ œ ์š”์•ฝ
์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜ ๊ตฌํ•˜๋ผ! (๋‹จ, ์ ์–ด๋„ ํ•˜๋‚˜ ์ด์ƒ์˜ ์˜์ƒ์„ ์„ ํƒํ•ด์•ผ ํ•จ)


๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฐ”๋กœ ๋– ์˜ฌ๋ฆฐ๊ฑด,
์ผ๋‹จ ์ž…๋ ฅ๋ฐ›์€ clothes์— ๋Œ€ํ•˜์—ฌ dictionaryํ™” ํ•˜๊ณ ,
๊ฐ ์ข…๋ฅ˜๋ณ„๋กœ ์กฐํ•ฉ์„ ๊ตฌํ•˜๊ณ , ํ•ด๋‹น ์กฐํ•ฉ(์„ ํƒ๋œ ์ข…๋ฅ˜)์—์„œ ๊ฐ ์˜์ƒ ์ˆ˜๋ฅผ ๊ณฑํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.


๊ฐ„๋‹จํžˆ ์˜ˆ์‹œ๋ฅผ ๋“ค์ž๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

image


์œ„ ๋กœ์ง์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
c_dict๋Š” clothes๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฐ”๊พผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

for r in range(len(c_dict)): # nCr์—์„œ r์€ dictionary ๊ธธ์ด๋งŒํผ ๊ฐ€๋Šฅ
    nCr = list(itertools.combinations(c_dict,r+1)) # ์กฐํ•ฉ ์ฐพ๊ธฐ r์ด 1์ด๋ฉด, nCr์˜ ๋ฐ˜ํ™˜๊ฐ’์˜ ์˜ˆ๋Š” (์–ผ๊ตด, ์ƒ์˜)
    for n in range(len(nCr)): # ์ฐพ์€ ์กฐํ•ฉ์— ๋Œ€ํ•˜์—ฌ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
        check = 1
        for k in range(len(nCr[n])): # ๊ฐ ์ข…๋ฅ˜์— ๋Œ€ํ•˜์—ฌ ์˜์ƒ ์ˆ˜ ๊ณฑํ•˜๊ธฐ
            check *= c_dict[nCr[n][k]] # r = 1, ์ด๊ณ  nCr์˜ ์ฒซ๋ฒˆ์งธ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ (์–ผ๊ตด, ์ƒ์˜)์ด๋ฉด, check์˜ ์ตœ์ข…๊ฐ’์€ 5*4์ž„
        answer += check # ์ตœ์ข… ๋‹ต์ธ answer์— ๋”ํ•˜๊ธฐ



์–ด์งธ์ €์งธ ํ’€๊ธดํ–ˆ์ง€๋งŒ ๊ต‰~~~~์žฅํžˆ ๋บ‘๋บ‘ ๋Œ์•„๊ฐ„๊ฑธ ๋Š๋‚„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋‘๋ฒˆ์งธ for๋ฌธ์„ ์ œ์™ธํ•˜๋”๋ผ๋„ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n^2)์ด๋‹ˆ๊นŒ์š” ,,,



์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ ์ œ์ถœํ•˜๋‹ˆ๊นŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 1์— ๋Œ€ํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๊ณ ์š”

๊ทธ๋ž˜์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ ๋”ํ•˜๊ธฐ ๋ณด๋‹ค, ์—ฌ์ง‘ํ•ฉ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.


์ฆ‰,
(์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜) - (์–ด๋–ค ์ข…๋ฅ˜์˜ ์˜์ƒ๋„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜)
๊ฐ€ ์—ฌ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•œ ์‹์ž…๋‹ˆ๋‹ค.


์œ„์—์„œ ๋“  ์˜ˆ์‹œ๋กœ ์„ค๋ช…ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

image

๋‹ค์‹œ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

for c in c_dict.values():
    answer *= (c+1)

answer -= 1



2๋‹ฌ๋งŒ์— ์•Œ๊ณ  ๋ฌธ์ œํ‘ธ๋‹ˆ๊นŒ ์ƒˆ๋กญ๋„ค์š” ..ใ…Žใ…Ž

์•„๋ž˜๋Š” ์ „์ฒด์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

import itertools

clothes = [["yellow_hat", "headgear"], 
           ["blue_sunglasses", "eyewear"], 
           ["green_turban", "headgear"],
           ["test1", "sample"],
           ["test2", "sample"],
           ]
c_dict = {}
answer = 1

for info in range(len(clothes)):
    if not clothes[info][1] in c_dict:
        c_dict[clothes[info][1]] = 1
    else:
        c_dict[clothes[info][1]] += 1

for c in c_dict.values():
    answer *= (c+1)

answer -= 1

print(answer)

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

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 collections import defaultdict

def solution(clothes):
    closet = defaultdict(list)
    answer = 1
    
    for cloth in clothes:
        cloth_detail, cloth_type = cloth[0], cloth[1]
    
        closet[cloth_type].append(cloth_detail)
        
    for cloth_type in closet:
        answer *=(len(closet[cloth_type])+1)
        
    return answer-1

๊ณ ์ƒํ–ˆ์–ด์œ  ํ™์ฅฌ๋ฅด์ƒ ์›ฐ์ปด๋ฐฑ์•Œ๊ณ ๋ฆฌ๋“œ๋ฏธ~~~~~~~~~

Comment on lines +6 to +11
clothes = [["yellow_hat", "headgear"],
["blue_sunglasses", "eyewear"],
["green_turban", "headgear"],
["test1", "sample"],
["test2", "sample"],
]
Copy link
Member

Choose a reason for hiding this comment

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

๋กœ๊ทธ๊นŒ์ง€ ๋‚จ๊ฒจ์ฃผ์‹œ๋Š” ใ…Žใ„ทใ„ทใ„ทใ„ท

else:
c_dict[clothes[info][1]] += 1

# answer = collections.Counter(c_dict)
Copy link
Member

Choose a reason for hiding this comment

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

์ตœ์‹ ๊ธฐ์ˆ  ํ”์  ใ…Žใ„ทใ„ทใ„ทใ„ทใ„ทใ„ทใ„ทใ„ทใ„ท

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.

์ €๋„ ๊ฐ ์ข…๋ฅ˜๋ณ„๋กœ ๋งต์—๋‹ค๊ฐ€ ์ง‘์–ด ๋„ฃ์€ ํ›„ ๊ฐ๊ฐ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•ด์„œ ์ผ์ผ์ด ๋‹ค ๋”ํ•˜๋Š” ์‹์œผ๋กœ ํ’€์—ˆ๋”๋‹ˆ ํƒ€์ž„ ์ดˆ๊ณผ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋”๊ตฐ์š”... ์กฐํ•ฉ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ๊ณฑํ–ˆ๋”๋‹ˆ ์ €๋„ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

int solution(vector<vector<string>> clothes) {
    map<string, vector<string>> m;
    vector<string> v;
    int answer = 1;
    
    for(auto cloth : clothes)
    {
        m[cloth[1]].push_back(cloth[0]);
        if(m[cloth[1]].size() == 1) v.push_back(cloth[1]);
    }
    
    for(int i = 0; i < v.size(); i++)
    {
        answer *= (m[v[i]].size()+1);
    }

    return answer - 1;
}

Copy link

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋„ ๋”ํ•ด์ค˜์„œ ์ „์ฒด๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ์— ์•„๋ฌด๊ฒƒ๋„ ์ž…์ง€ ์•Š์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋บ€ ํ’€์ด ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค! ๋ฌธ์ œ๋ฅผ ์ฝ๋‹ค๊ฐ€ ์ดํ•ดํ•˜๋Š”๋ฐ ์ข€ ๊ฑธ๋ ธ์–ด์š”. ์˜ค๋žœ๋งŒ์— ์กฐํ•ฉ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ˆ ๋จธ๋ฆฌ๊ฐ€ ๋ตํ–ˆ์Šต๋‹ˆ๋‹ค.. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ๋Š” ๋ฐฑ์ค€๊ณผ ๋‹ค๋ฅด๊ฒŒ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ํ†ต๊ณผ์œจ๋„ ์ž˜ ๋ณด์—ฌ์ฃผ๋„ค์š” ! ์ด์   ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋ฅผ ์ข€ ํ’€์–ด๋ด์•ผ๊ฒ ์Šด๋‹ˆ๋‹คใ…Žใ…Ž

@H0ngJu H0ngJu merged commit 883f980 into main Aug 21, 2024
@H0ngJu H0ngJu deleted the 22-H0ngJu branch August 21, 2024 03:43
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