문제 >> 알파벳

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
class pair {
    int now;
    int weight;
    public pair(int now, int weight) {
        super();
        this.now = now;
        this.weight = weight;
    }
}
 
public class Main {
    static int n,m;
    static boolean[][] map;
 
    static int[] rst;
    static Scanner sc = new Scanner(System.in);
 
    static int bfs(int start) {
        boolean[] visited = new boolean[n+1];
        Queue<pair> q = new LinkedList<pair>();
        visited[start]=true;
        q.add(new pair(start,0));
        int score=0;
        
        while(!q.isEmpty()) {
            int now = q.peek().now;
            int weight = q.peek().weight;
            score+=weight;
            q.poll();
            
            for(int i=1;i<=n;i++) {
                if(map[now][i] && !visited[i]) {
                    visited[i]=true;
                    q.add(new pair(i,weight+1));
                }
            }
        }
        
        return score;
    }
    
    public static void main(String[] args) {
        n = sc.nextInt(); // 유저의 수
        m = sc.nextInt(); // 관계 수
        
        map = new boolean[n+1][n+1];
        rst = new int[n+1];
        
        for(int i=0;i<m;i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            
            map[a][b] = true;
            map[b][a] = true;
        }
        
        int min=Integer.MAX_VALUE;
        int ans = 0;
        for(int i=1;i<=n;i++) {
            
            rst[i] = bfs(i);
            
            if(rst[i]<min) {
                min = rst[i];
                ans = i;
            }
        }
        
        System.out.println(ans);
        
    }//end of main
}
 
cs

문제 >> 세로읽기

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
class pair {
    int now;
    int weight;
    public pair(int now, int weight) {
        super();
        this.now = now;
        this.weight = weight;
    }
}
 
public class Main {
    static int n,m;
    static boolean[][] map;
 
    static int[] rst;
    static Scanner sc = new Scanner(System.in);
 
    static int bfs(int start) {
        boolean[] visited = new boolean[n+1];
        Queue<pair> q = new LinkedList<pair>();
        visited[start]=true;
        q.add(new pair(start,0));
        int score=0;
        
        while(!q.isEmpty()) {
            int now = q.peek().now;
            int weight = q.peek().weight;
            score+=weight;
            q.poll();
            
            for(int i=1;i<=n;i++) {
                if(map[now][i] && !visited[i]) {
                    visited[i]=true;
                    q.add(new pair(i,weight+1));
                }
            }
        }
        
        return score;
    }
    
    public static void main(String[] args) {
        n = sc.nextInt(); // 유저의 수
        m = sc.nextInt(); // 관계 수
        
        map = new boolean[n+1][n+1];
        rst = new int[n+1];
        
        for(int i=0;i<m;i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            
            map[a][b] = true;
            map[b][a] = true;
        }
        
        int min=Integer.MAX_VALUE;
        int ans = 0;
        for(int i=1;i<=n;i++) {
            
            rst[i] = bfs(i);
            
            if(rst[i]<min) {
                min = rst[i];
                ans = i;
            }
        }
        
        System.out.println(ans);
        
    }//end of main
}
 
cs

'Algorithm Problem Solving' 카테고리의 다른 글

[BOJ] 10819 : 차이를 최대로  (0) 2019.02.08
[BOJ] 1987 : 알파벳  (0) 2019.02.08
[BOJ] 6588 : 골드바흐의 추측  (0) 2019.02.08
[BOJ]1389: 케빈 베이컨의 6단계 법칙  (0) 2019.02.08
[정올] 1809 : 탑  (0) 2019.01.22

문제 >> 골드바흐의 추측

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
class pair {
    int now;
    int weight;
    public pair(int now, int weight) {
        super();
        this.now = now;
        this.weight = weight;
    }
}
 
public class Main {
    static int n,m;
    static boolean[][] map;
 
    static int[] rst;
    static Scanner sc = new Scanner(System.in);
 
    static int bfs(int start) {
        boolean[] visited = new boolean[n+1];
        Queue<pair> q = new LinkedList<pair>();
        visited[start]=true;
        q.add(new pair(start,0));
        int score=0;
        
        while(!q.isEmpty()) {
            int now = q.peek().now;
            int weight = q.peek().weight;
            score+=weight;
            q.poll();
            
            for(int i=1;i<=n;i++) {
                if(map[now][i] && !visited[i]) {
                    visited[i]=true;
                    q.add(new pair(i,weight+1));
                }
            }
        }
        
        return score;
    }
    
    public static void main(String[] args) {
        n = sc.nextInt(); // 유저의 수
        m = sc.nextInt(); // 관계 수
        
        map = new boolean[n+1][n+1];
        rst = new int[n+1];
        
        for(int i=0;i<m;i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            
            map[a][b] = true;
            map[b][a] = true;
        }
        
        int min=Integer.MAX_VALUE;
        int ans = 0;
        for(int i=1;i<=n;i++) {
            
            rst[i] = bfs(i);
            
            if(rst[i]<min) {
                min = rst[i];
                ans = i;
            }
        }
        
        System.out.println(ans);
        
    }//end of main
}
 
cs

'Algorithm Problem Solving' 카테고리의 다른 글

[BOJ] 1987 : 알파벳  (0) 2019.02.08
[BOJ] 10798 : 세로읽기  (0) 2019.02.08
[BOJ]1389: 케빈 베이컨의 6단계 법칙  (0) 2019.02.08
[정올] 1809 : 탑  (0) 2019.01.22
5432. 쇠막대기 자르기  (0) 2019.01.15

문제 >>  케빈 베이컨의 6단계 법칙

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
class pair {
    int now;
    int weight;
    public pair(int now, int weight) {
        super();
        this.now = now;
        this.weight = weight;
    }
}
 
public class Main {
    static int n,m;
    static boolean[][] map;
 
    static int[] rst;
    static Scanner sc = new Scanner(System.in);
 
    static int bfs(int start) {
        boolean[] visited = new boolean[n+1];
        Queue<pair> q = new LinkedList<pair>();
        visited[start]=true;
        q.add(new pair(start,0));
        int score=0;
        
        while(!q.isEmpty()) {
            int now = q.peek().now;
            int weight = q.peek().weight;
            score+=weight;
            q.poll();
            
            for(int i=1;i<=n;i++) {
                if(map[now][i] && !visited[i]) {
                    visited[i]=true;
                    q.add(new pair(i,weight+1));
                }
            }
        }
        
        return score;
    }
    
    public static void main(String[] args) {
        n = sc.nextInt(); // 유저의 수
        m = sc.nextInt(); // 관계 수
        
        map = new boolean[n+1][n+1];
        rst = new int[n+1];
        
        for(int i=0;i<m;i++) {
            int a=sc.nextInt();
            int b=sc.nextInt();
            
            map[a][b] = true;
            map[b][a] = true;
        }
        
        int min=Integer.MAX_VALUE;
        int ans = 0;
        for(int i=1;i<=n;i++) {
            
            rst[i] = bfs(i);
            
            if(rst[i]<min) {
                min = rst[i];
                ans = i;
            }
        }
        
        System.out.println(ans);
        
    }//end of main
}
 
cs


언이 마지막날 ..💧

'Diary' 카테고리의 다른 글

역삼 상도  (5) 2019.05.30
역삼동 맛있는 제주  (2) 2019.03.01
역삼 민들레떡볶이  (3) 2019.01.30
역삼 호타루  (2) 2019.01.25
역삼 전봇대  (2) 2019.01.23



존맛탱 또가야딩 🥳🥳🥳🥳

'Diary' 카테고리의 다른 글

역삼동 맛있는 제주  (2) 2019.03.01
선릉 착한고기  (4) 2019.01.31
역삼 호타루  (2) 2019.01.25
역삼 전봇대  (2) 2019.01.23
역삼 광역시맥주8220  (1) 2019.01.12
MergeSort

MergeSort

merge sort에 대한 이미지 검색결과

 

  • 시간 복잡도 O(n lon n)

 

  • list를 이용해 구현하기

 

  • 배열을 이용해 구현하기

 

'Computer Science > Data Structure' 카테고리의 다른 글

해시테이블 Hashtable  (2) 2019.05.06
[Java] 배열과 List / Set / Map  (0) 2019.01.22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
import java.util.Arrays;
import java.util.LinkedList;
 
public class Z21_InsertionSort {
//    public static void main(String[] args) {
//        int[] arr= {6,4,8,5,7,2,9,3,0,1};
//        
//        for(int i=1;i<arr.length;i++) {
//            //삽입할 위치를 찾아 넣기
//            for(int j=i-1;j>=0;j--) {
//                if(arr[j]>arr[j+1]) { //뒤쪽에서 앞으로
//                    int temp = arr[j+1];
//                    arr[j+1]= arr[j];
//                    arr[j]=temp;
//                }
//            }
//        }
//        
//        for(int i=1;i<arr.length;i++) {
//            //삽입할 위치를 찾아 넣기
//            int num=arr[i];
//            int j;
//            for(j=i-1;j>=0;j--) {
//                if(arr[j]>num) { //뒤쪽에서 앞으로
//
//                    arr[j+1]= arr[j];
//
//                } else {
//                    break; //성능을 높일 수 있다.
//                }
//            }
//            arr[j+1] = num;
//        }
//        System.out.println(Arrays.toString(arr));
//    }
    
    public static void main(String[] args) {
        //Linked List 사용하기
        //Swap(쉬프트)작업을 안하는 것으로 성능을 개선
        
        int[] arr= {6,4,8,5,7,2,9,3,0,1};
        LinkedList<Integer> ll = new LinkedList<Integer>();
        
        for(int i=0;i<arr.length;i++) {
            int idx=0;
            for(idx = 0; idx<ll.size();idx++) { //정렬된 데이터에 삽입할 위치 찾기.
                if(arr[i]<ll.get(idx)) {
                    break//삽입은 밖에서.
                    //0번째에서는 데이터가 없으니 해당 포문을 돌지않아 데이터가 들어가지 않는다.
                }
            }
            ll.add(idx,arr[i]); //ll의 idx번째 자리에 arr[i]를 삽입
        }
        System.out.println(ll);
    }
}
 
cs

'Computer Science > Algorithms' 카테고리의 다른 글

[java] Dijkstra 다익스트라 알고리즘  (0) 2019.02.19
Kruskal 크루스칼 알고리즘  (0) 2019.02.18
DisjointSets  (0) 2019.02.18
Permutation 순열  (0) 2019.01.17
Dijkstra 최단경로 알고리즘  (2) 2019.01.08

+ Recent posts