#유형 : 트리, 자료구조

#난이도 : 골드 3

 

# 1) postOrder의 마지막 원소가 트리의 루트라는 특징을 이용하여 inOrder(좌우로 나뉜다는 특징)에서 
1. 첫번째 인덱스  ~  해당 인덱스-1  //// 2. 해당 인덱스+1  ~ 마지막 인덱스 로 분할 한다.

 

# 2) postOrder를 다음과 같이 분할

1. 후위 배열의 첫번째 인덱스 ~ 찾은인덱스 - 중위순회에서의 첫 인덱스 - 1
2. 후위 배열의 첫번째 인덱스 + 찾은인덱스 - 중위순회에서의 첫 인덱스 ~ 마지막 인덱스 -1 로 나누어준다.

 

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
package bj;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class p2263 {
    static int N;
    static int in[],post[],idx[];
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        in = new int[N+1];
        post = new int[N+1];
        idx = new int[N+1];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=1; i<=N; i++) {
            in[i] = Integer.parseInt(st.nextToken());
        }
        st = new StringTokenizer(br.readLine());
        for(int i=1; i<=N; i++) {
            post[i] = Integer.parseInt(st.nextToken());
        }
        for(int i=1; i<=N; i++)
            idx[in[i]] = i;
        
        
        preOrder(0, N, 0, N);
    }
    public static void preOrder(int in_begin, int in_end, int post_begin, int post_end) {
        if(in_begin > in_end || post_begin > post_end || post_end == 0)
            return;
        
        int root = post[post_end];
        System.out.print(root+" ");
        
        int left = idx[root] - in_begin;
                
        //Left
        preOrder(in_begin, idx[root] - 1, post_begin, post_begin + left -1);
        //right
        preOrder(idx[root] + 1, in_end, post_begin + left, post_end - 1);
    }
}
 
cs

#유형 : 트리, 자료구조

# 난이도 : 실버 1

# 자료구조 강의 듣던 시절로 돌아간 느낌이었다. 입력으로 들어오는 PreOrder 를 조건에 맞게 트리로 작성한다음 postOrder로 출력하면 된다.

 

# 현재 노드의 값보다 작으면 왼쪽으로 노드 삽입, 그 반대인 경우에는 오른쪽으로 삽입한다.(중복값없음).

 

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
package bj;
 
import java.io.IOException;
import java.util.Scanner;
 
public class p5639 {
    static int arr[] = new int[10001];
    public static void main(String[] args) throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Node root = new Node(N);
        while(sc.hasNext()) {
            try {
                N = sc.nextInt();
                root = insertNode(root, N);
            } catch (Exception e) {
                // TODO: handle exception
                break;
            }
            
        }
        postOrder(root);
    }
    public static class Node{
        Node left;
        Node right;
        int val;
        public Node(int v) {
            this.val = v;
        }
        
    }
    public static Node insertNode(Node node, int N) {
        Node current = null;
        if(node == null) {
            return new Node(N);
        }
        
        if(node.val > N) {
            current = insertNode(node.left, N);
            node.left = current;
        }else {
            current = insertNode(node.right, N);
            node.right = current;
        }
        return node;
    }
    
    public static void postOrder(Node node) {
        if(node != null) {
            postOrder(node.left);
            postOrder(node.right);
            System.out.println(node.val);
        }
    }
}
 
cs

1
2
-- SQL_SUM,MAX,MIN - 중복 제거하기
SELECT COUNT(DISTINCT NAME) AS count FROM ANIMAL_INS WHERE NAME IS NOT NULL
cs

1
2
-- SQL_SUM,MAX,MIN - 동물 수 구하기
SELECT COUNT(ANIMAL_TYPE) AS count FROM ANIMAL_INS
cs

 

1
2
-- SQL_SUM,MAX,MIN - 최솟값 구하기
SELECT MIN(DATETIMEFROM ANIMAL_INS
cs

1
2
-- SQL_SUM,MAX,MIN - 최댓값 구하기
SELECT MAX(DATETIME) AS '시간' FROM ANIMAL_INS
cs

# 유형 : 백트래킹

# 난이도 : 골드 V

 

 

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
package bj;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class p9663 {
    static int N,count;
    static int cols[];
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        cols = new int[16];
        batch(0);
        System.out.println(count);
    }
    
    public static boolean isPossible(int idx) {
        int tmp = 1;
        boolean rs = true;
        while(tmp<idx && rs) {
            // 같은 행이거나 또는 대각선의 관계를 가질 때 배치할 수 없다.
            if(cols[idx] == cols[tmp] || Math.abs(cols[idx]-cols[tmp]) == idx-tmp)
                rs = false;
            tmp++;
        }
        return rs;
    }
    
    public static void batch(int idx) {
        if(isPossible(idx)) {
            if(idx==N) {
                count++;
            }
            else {
                //행의 값을 넣어가며 배치가 가능한지 확인한다.
                for(int j=1; j<=N; j++) {
                    cols[idx + 1= j;
                    batch(idx+1);
                }
            }
        }
    }
    
}
 
cs

 

1
2
-- 상위 n개 레코드
SELECT NAME FROM ANIMAL_INS ORDER BY DATETIME ASC LIMIT 1
cs

+ Recent posts