๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ „๋ ฅ๋ง์„ ๋‘˜๋กœ ๋‚˜๋ˆ„๊ธฐ

๐ŸŽฎinspirer9 2023. 2. 20. 00:34
728x90
๋ฐ˜์‘ํ˜•

์˜ˆ์ „์— C++๋กœ ํ’€๋‹ค๊ฐ€, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒ ๋‹ค ๋งž์ถ”๊ณ , ์ฑ„์ ๊ฒฐ๊ณผ์—์„œ ์ •ํ™•์„ฑ ๋ฌธ์ œ ์‹คํŒจํ•ด์„œ ํฌ๊ธฐํ–ˆ๋˜ ๋ฌธ์ œ๋‹ค. ์š”๋ฒˆ์— ํŒŒ์ด์ฌ์œผ๋กœ ๋‹ค์‹œ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค!

๋ฌธ์ œ ์„ค๋ช…

n๊ฐœ์˜ ์†ก์ „ํƒ‘์ด ์ „์„ ์„ ํ†ตํ•ด ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์ด ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ํ˜„์žฌ์˜ ์ „๋ ฅ๋ง ๋„คํŠธ์›Œํฌ๋ฅผ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ–๊ฒŒ ๋˜๋Š” ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ๋งž์ถ”๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜ n, ๊ทธ๋ฆฌ๊ณ  ์ „์„  ์ •๋ณด wires๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋น„์Šทํ•˜๋„๋ก ๋‘ ์ „๋ ฅ๋ง์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜์˜ ์ฐจ์ด(์ ˆ๋Œ€๊ฐ’)๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • n์€ 2 ์ด์ƒ 100 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • wires๋Š” ๊ธธ์ด๊ฐ€ n-1์ธ ์ •์ˆ˜ํ˜• 2์ฐจ์› ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
    • wires์˜ ๊ฐ ์›์†Œ๋Š” [v1, v2] 2๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์ „๋ ฅ๋ง์˜ v1๋ฒˆ ์†ก์ „ํƒ‘๊ณผ v2๋ฒˆ ์†ก์ „ํƒ‘์ด ์ „์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • 1 ≤ v1 < v2 ≤ n ์ž…๋‹ˆ๋‹ค.
    • ์ „๋ ฅ๋ง ๋„คํŠธ์›Œํฌ๊ฐ€ ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

n wires result
9 [[1,3],[2,3],[3,4],[4,5],[4,6],[4,7],[7,8],[7,9]] 3
4 [[1,2],[2,3],[3,4]] 0
7 [[1,2],[2,7],[3,7],[3,4],[4,5],[6,7]] 1

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • 4๋ฒˆ๊ณผ 7๋ฒˆ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ „์„ ์„ ๋Š์œผ๋ฉด ๋‘ ์ „๋ ฅ๋ง์€ ๊ฐ 6๊ฐœ์™€ 3๊ฐœ์˜ ์†ก์ „ํƒ‘์„ ๊ฐ€์ง€๋ฉฐ, ์ด๋ณด๋‹ค ๋” ๋น„์Šทํ•œ ๊ฐœ์ˆ˜๋กœ ์ „๋ ฅ๋ง์„ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ „์„ ์„ ๋Š์–ด๋„ ์ตœ์„ ์˜ ์ •๋‹ต์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • 2๋ฒˆ๊ณผ 3๋ฒˆ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ „์„ ์„ ๋Š์œผ๋ฉด ๋‘ ์ „๋ ฅ๋ง์ด ๋ชจ๋‘ 2๊ฐœ์˜ ์†ก์ „ํƒ‘์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉฐ, ์ด ๋ฐฉ๋ฒ•์ด ์ตœ์„ ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • 3๋ฒˆ๊ณผ 7๋ฒˆ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ „์„ ์„ ๋Š์œผ๋ฉด ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ๊ฐ 4๊ฐœ์™€ 3๊ฐœ์˜ ์†ก์ „ํƒ‘์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉฐ, ์ด ๋ฐฉ๋ฒ•์ด ์ตœ์„ ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ์˜ˆ์ „ C++ ํ’€์ด๋ฅผ ๋ณด๋ฉด, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ๋…ธ๊ฐ€๋‹ค์˜€๋‹ค.
#include <vector>
#include <algorithm>

using namespace std;

int solution(int n, vector<vector<int>> wires) {
    int answer = 9999999;

    vector<int> graph[n+1];
    vector<char> visited(n+1, ' ');

    sort(wires.begin(), wires.end());
    
    for(int i=0;i<wires.size();i++){
        graph[wires[i][0]].push_back(wires[i][1]);
        graph[wires[i][1]].push_back(wires[i][0]);
    }
    
    for(int i=1;i<=n;i++){
       sort(graph[i].begin(), graph[i].end());
    }
    
    for(int i=0;i<wires.size();i++){
        fill(visited.begin(), visited.end(), ' ');
        visited[wires[i][0]] = 'L';
        visited[wires[i][1]] = 'R';

        int left, right, sum = 0;

        while(sum < n){
            for(int z=1;z<=n;z++){
                for(int y=0;y<graph[z].size();y++){
                    if(visited[graph[z][y]] == ' ') visited[graph[z][y]] = visited[z];
                }
            }
            for(int z=n;z>0;z--){
                for(int y=0;y<graph[z].size();y++){
                    if(visited[graph[z][y]] == ' ') visited[graph[z][y]] = visited[z];
                }
            }
            
            left = 0;
            right = 0;
            for(int z=1;z<=n;z++){
                if(visited[z]=='L')left++;
                if(visited[z]=='R')right++;
            }
            sum = left + right;
            answer = min(answer, abs(left - right));
        }
    }

    return answer;
}
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ค ๋งž์ท„๋Š”๋ฐ...
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	9, [[1, 3], [2, 3], [3, 4], [4, 5], [4, 6], [4, 7], [7, 8], [7, 9]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	4, [[1, 2], [2, 3], [3, 4]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 3
์ž…๋ ฅ๊ฐ’ ใ€‰	7, [[1, 2], [2, 7], [3, 7], [3, 4], [4, 5], [6, 7]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	1
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 4
์ž…๋ ฅ๊ฐ’ ใ€‰	8, [[1, 2], [1, 3], [1, 4], [4, 5], [5, 6], [6, 7], [6, 8]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ์ฑ„์ ์—์„œ ์‹คํŒจํ•œ๋‹ค.
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.08ms, 4.27MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (2.64ms, 4.31MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.12ms, 4.15MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (0.13ms, 4.14MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (0.14ms, 4.33MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 3.66MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 4.26MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (0.04ms, 3.69MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 4.34MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (0.75ms, 4.31MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (0.72ms, 4.25MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	์‹คํŒจ (0.63ms, 4.28MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	์‹คํŒจ (0.72ms, 4.13MB)
  • ๊ทผ๋ฐ ๊ทธ ๋•Œ๋‚˜ ์ง€๊ธˆ์ด๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ์‹์€ ํฌ๊ฒŒ ๋‹ฌ๋ผ์ง„ ๊ฑด ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค. (๋ฌด์ง€์„ฑใ…‹ใ…‹ใ…‹)
  • ๊ทธ๋ฆฌ๊ณ  ์ด๊ฑฐ ์™œ ๋ชป ํ’€์—ˆ๋Š”๋ฐ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ”๋Š”์ง€ ๊ธฐ์–ต์ด ์•ˆ๋‚˜๋„ค...
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit์— ์žˆ๋Š” ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ 7๊ฐœ ์ค‘์— ์œ ์ผํ•˜๊ฒŒ ๋ชป ํ’€๊ณ  ๋„˜์–ด๊ฐ”๋˜ ๋ฌธ์ œ์˜€๋‹ค.

  • ๋ฌธ์ œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ œ๋Œ€๋กœ ์ดํ•ดํ•ด๋ณด๊ธฐ๋กœ...
  • ์ด๊ฑด ์ง„์งœ ๋ญ”๊ฐ€ ๊ผผ์ˆ˜ ์—†์ด ํ•˜๋‚˜์”ฉ ์ž˜๋ผ์„œ ํ•ด๋ด์•ผํ•˜๋Š” ๊ฑด๊ฐ€?
  • ์ผ๋‹จ ์‹œํ‚ค๋Š”๋Œ€๋กœ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์–ด๋ด„.
def solution(n, wires):
    answer = 100
    for i in range(n-1):
        print(i, wires[i],"์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================")
        heap = wires.copy()
        heap.pop(i)
        L = set()
        R = set()
        L.add(wires[i][0])
        R.add(wires[i][1])
        while heap:
            a, b = heap.pop(0)
            if a in L or b in L:
                L.add(a)
                L.add(b)
            elif a in R or b in R:
                R.add(a)
                R.add(b)
            else:
                heap.append([a, b])
        print("L:", L)
        print("R:", R)
        answer = min(abs(len(L)-len(R)), answer)
    return answer
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ชจ๋‘ ์ž˜ ํ’€๋ฆผ
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	9, [[1, 3], [2, 3], [3, 4], [4, 5], [4, 6], [4, 7], [7, 8], [7, 9]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 [1, 3] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1}
R: {2, 3, 4, 5, 6, 7, 8, 9}
1 [2, 3] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {2}
R: {1, 3, 4, 5, 6, 7, 8, 9}
2 [3, 4] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3}
R: {4, 5, 6, 7, 8, 9}
3 [4, 5] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 6, 7, 8, 9}
R: {5}
4 [4, 6] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 7, 8, 9}
R: {6}
5 [4, 7] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 6}
R: {8, 9, 7}
6 [7, 8] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 6, 7, 9}
R: {8}
7 [7, 9] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 6, 7, 8}
R: {9}

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	4, [[1, 2], [2, 3], [3, 4]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 [1, 2] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1}
R: {2, 3, 4}
1 [2, 3] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2}
R: {3, 4}
2 [3, 4] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3}
R: {4}

ํ…Œ์ŠคํŠธ 3
์ž…๋ ฅ๊ฐ’ ใ€‰	7, [[1, 2], [2, 7], [3, 7], [3, 4], [4, 5], [6, 7]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	1
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 [1, 2] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1}
R: {2, 3, 4, 5, 6, 7}
1 [2, 7] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2}
R: {3, 4, 5, 6, 7}
2 [3, 7] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {3, 4, 5}
R: {1, 2, 6, 7}
3 [3, 4] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 6, 7}
R: {4, 5}
4 [4, 5] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 6, 7}
R: {5}
5 [6, 7] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {6}
R: {1, 2, 3, 4, 5, 7}

ํ…Œ์ŠคํŠธ 4
์ž…๋ ฅ๊ฐ’ ใ€‰	8, [[1, 2], [1, 3], [1, 4], [4, 5], [5, 6], [6, 7], [6, 8]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 [1, 2] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 3, 4, 5, 6, 7, 8}
R: {2}
1 [1, 3] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 4, 5, 6, 7, 8}
R: {3}
2 [1, 4] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3}
R: {4, 5, 6, 7, 8}
3 [4, 5] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4}
R: {8, 5, 6, 7}
4 [5, 6] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5}
R: {8, 6, 7}
5 [6, 7] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 6, 8}
R: {7}
6 [6, 8] ์„ ๋Š์–ด๋ด…๋‹ˆ๋‹ค ==================
L: {1, 2, 3, 4, 5, 6, 7}
R: {8}
  • ์ตœ์ ํ™” ํ•ด์„œ ์ œ์ถœํ•ด๋ณธ๋‹ค.
  • ์„ฑ๊ณต! ์ง„์งœ ์™„์ „ ํƒ์ƒ‰ ๋ฌธ์ œ์˜€๊ตฌ๋‚˜...;;;
def solution(n, wires):
    answer = 100
    for i in range(n-1):
        heap = wires.copy()
        heap.pop(i)
        L, R = set(), set()
        L.add(wires[i][0])
        R.add(wires[i][1])
        while heap:
            a, b = heap.pop(0)
            if a in L or b in L:
                L.add(a)
                L.add(b)
            elif a in R or b in R:
                R.add(a)
                R.add(b)
            else:
                heap.append([a, b])
        answer = min(abs(len(L)-len(R)), answer)
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (2.16ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (35.27ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (16.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (4.78ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (10.80ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (8.34ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (8.90ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (9.38ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (8.16ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด... ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ๋Š๋ฆฌ๋‹ค.
def solution(n, wires):
    ans = n
    for sub in (wires[i+1:] + wires[:i] for i in range(len(wires))):
        s = set(sub[0])
        [s.update(v) for _ in sub for v in sub if set(v) & s]
        ans = min(ans, abs(2 * len(s) - n))
    return ans
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (312.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (269.78ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (221.52ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (220.82ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (272.53ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.85ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2.72ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (212.52ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (282.40ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (224.79ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (210.24ms, 10.2MB)
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์†๋„๋ฅผ ์กฐ๊ธˆ ๋จน์ง€๋งŒ, ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ณต์žกํ•ด์กŒ์„ ๋•Œ ๋น ๋ฅด๋‹ค.
  • def find(a)๋ž€๊ฑธ ๋งŒ๋“ค์–ด์„œ ํƒ์ƒ‰ํ•˜๊ณ  ํ•ฉ์นœ๋‹ค.
uf = []

def find(a):
    global uf
    if uf[a] < 0: return a
    uf[a] = find(uf[a])
    return uf[a]

def merge(a, b):
    global uf
    pa = find(a)
    pb = find(b)
    if pa == pb: return
    uf[pa] += uf[pb]
    uf[pb] = pa

def solution(n, wires):
    global uf
    answer = int(1e9)
    k = len(wires)
    for i in range(k):
        uf = [-1 for _ in range(n+1)]
        tmp = [wires[x] for x in range(k) if x != i]
        for a, b in tmp: merge(a, b)
        v = [x for x in uf[1:] if x < 0]
        answer = min(answer, abs(v[0]-v[1]))

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (7.58ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (8.56ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (8.71ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (4.35ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (5.30ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (5.35ms, 10MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (6.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (5.73ms, 10.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (5.07ms, 10.3MB)
  • ์ฐพ์•˜๋‹ค. ์ง„์งœ ๊ณ ์ˆ˜๋„ค... ์ด๊ฒŒ ์ œ์ผ ๋น ๋ฅด๋„ค?
  • ์ด๊ฑฐ ๋ญ์ง€? ์™ค์ผ€ ๋นจ๋ผ???
  • ๋‚ด๊ฑฐ ๋””ํ๋กœ ๋ฐ”๊ฟ”๋„ ์ด ๋ฐฉ์‹๋ณด๋‹ค ๋น ๋ฅด์ง„ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค. pop, push๋ฅผ ์•ˆ์“ฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์–ด์•ผ...
  • ์•”ํŠผ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์„œ ๊ธด๊ฐ€๋ฏผ๊ฐ€ ํ–ˆ๋Š”๋ฐ ๊ฐœ๋น ๋ฆ„.
def solution(n, wires):
    tree = [[] for _ in range(n + 1)]

    for a, b in wires:
        tree[a].append(b)
        tree[b].append(a)

    visited = [False] * (n + 1)
    child = [0] * (n + 1)

    """๊ฐ์ž์˜ ์ž์‹ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ"""
    def dfs(node):
        visited[node] = True

        for next in tree[node]:
            if not visited[next]:
                child[node] += dfs(next) + child[next]

        return 1

    dfs(1)
    result = n

    for c in child:
        result = min(result, abs(n - 2 * (c + 1)))

    return result
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.4MB)
  • ๊ทธ๋ฆฌ๊ณ  for next๋Š” ๋ญ์ง€? ์ด๋Ÿฐ ๊ตฌ๋ฌธ๋„ ์žˆ๊ตฌ๋‚˜... ๊ทธ๋ƒฅ ๋ณ€์ˆ˜์˜€๋‹ค. ใ…ก.ใ…ก;;;;;
    •  ์†์•˜๋„นใ…‹ ใ…‹ใ…‹ ใ…‹ ใ…‹
  • ๋ฐ˜๋ณต๋ฌธ ์ •๋ฆฌ ์ข€ ํ•ด๋†“์ž (for, while)
    • break : ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ
    • continue : ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ๋„˜๊ธฐ๊ณ , ์ดํ›„ ๋ฐ˜๋ณต์œผ๋กœ (๋ฐ˜๋ณต๋ฌธ ์œ ์ง€)
    • pass
  • iter, next ํ•จ์ˆ˜
    • iter๋Š” ๋ฐ˜๋ณต์„ ๋๋‚ผ ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ํŠน์ • ๊ฐ’์ด ๋‚˜์˜ฌ ๋•Œ ๋ฐ˜๋ณต์„ ๋๋ƒ…๋‹ˆ๋‹ค.
    • next๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•