문제 제목 : 가장 긴 증가하는 부분 수열

난이도 : 하

문제 유형 : 동적 프로그래밍(DP), LIS

추천 풀이 시간 : 30분 (못하면 2배 60분)

TIP : 가장 적은 경우의 수부터 계산을 해본 후 패턴을 찾아 점화식을 세우자@@

점화식이란? 이웃하는 두개의 항 사이에 성립하는 관계를 나타낸 관계식 예를들어 dp[n + 2] = dp[n + 1] + dp[n + 2]

https://www.acmicpc.net/problem/11053

 

11053번: 가장 긴 증가하는 부분 수열

수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = 0;
        ArrayList<Integer> a = new ArrayList<Integer>();
        int[] dp = new int[1000];
        
        for (int i = 0; i < n; i++) {
            a.add(sc.nextInt());
        }
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (a.get(i) > a.get(j)) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            result = Math.max(result, dp[i]);
        }  

        System.out.println(result);
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 기타리스트  (0) 2023.01.23
[알고리즘-DP] LCS  (0) 2023.01.21
[알고리즘-DP] 평범한 배낭  (0) 2023.01.21
[알고리즘-DP] 01타일  (0) 2023.01.21
[알고리즘-DP] 파도반 수열  (0) 2023.01.20

문제 제목 : 평범한 배낭

난이도 : 하

문제 유형 : 동적 프로그래밍(DP)

추천 풀이 시간 : 30분 (못하면 2배 60분)

TIP : 가장 적은 경우의 수부터 계산을 해본 후 패턴을 찾아 점화식을 세우자@@

점화식이란? 이웃하는 두개의 항 사이에 성립하는 관계를 나타낸 관계식 예를들어 dp[n + 2] = dp[n + 1] + dp[n + 2]

https://www.acmicpc.net/problem/12865

 

12865번: 평범한 배낭

첫 줄에 물품의 수 N(1 ≤ N ≤ 100)과 준서가 버틸 수 있는 무게 K(1 ≤ K ≤ 100,000)가 주어진다. 두 번째 줄부터 N개의 줄에 거쳐 각 물건의 무게 W(1 ≤ W ≤ 100,000)와 해당 물건의 가치 V(0 ≤ V ≤ 1,000)

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();    
        int[][] dp = new int[n + 1][k + 1];
        
        for (int i = 1; i < n + 1; i++) {
            int w = sc.nextInt();
            int v = sc.nextInt();
            
            for (int j = 1; j < k + 1; j++) {
                if (j < w) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w] + v);
                }
            }
        }
        
        System.out.println(dp[n][k]);
    }
}

 

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] LCS  (0) 2023.01.21
[알고리즘-DP] 가장 긴 증가하는 부분 수열  (0) 2023.01.21
[알고리즘-DP] 01타일  (0) 2023.01.21
[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20

문제 제목 : 01타일

난이도 : 하

문제 유형 : 동적 프로그래밍(DP)

추천 풀이 시간 : 20분 (못하면 2배 40분)

TIP : 가장 적은 경우의 수부터 계산을 해본 후 패턴을 찾아 점화식을 세우자@@

점화식이란? 이웃하는 두개의 항 사이에 성립하는 관계를 나타낸 관계식 예를들어 dp[n + 2] = dp[n + 1] + dp[n + 2]

https://www.acmicpc.net/problem/1904

 

1904번: 01타일

지원이에게 2진 수열을 가르쳐 주기 위해, 지원이 아버지는 그에게 타일들을 선물해주셨다. 그리고 이 각각의 타일들은 0 또는 1이 쓰여 있는 낱장의 타일들이다. 어느 날 짓궂은 동주가 지원이

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        long[] dp = new long[1000001];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i < n + 1; i++) {
            dp[i] = (dp[i - 1] + dp[i - 2]) % 15746;
        }  
        
        System.out.println(dp[n]);
    }
}

문제 제목 : 파도반 수열

난이도 : 하

문제 유형 : 동적 프로그래밍(DP)

추천 풀이 시간 : 20분 (못하면 2배 40분)

TIP : 가장 적은 경우의 수부터 계산을 해본 후 패턴을 찾아 점화식을 세우자@@

점화식이란? 이웃하는 두개의 항 사이에 성립하는 관계를 나타낸 관계식 예를들어 dp[n + 2] = dp[n + 1] + dp[n + 2]

https://www.acmicpc.net/problem/9461

 

9461번: 파도반 수열

오른쪽 그림과 같이 삼각형이 나선 모양으로 놓여져 있다. 첫 삼각형은 정삼각형으로 변의 길이는 1이다. 그 다음에는 다음과 같은 과정으로 정삼각형을 계속 추가한다. 나선에서 가장 긴 변의

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        
        long[] dp = new long[101];
        for (int i = 1; i < 101; i++) {
            if (i <= 3) {
                dp[i] = 1;
            } else if (i <= 5) {
                dp[i] = 2;
            } else {
                dp[i] = dp[i - 1] + dp[i - 5];
            }
        }  
        
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            System.out.println(dp[n]);
        }
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 평범한 배낭  (0) 2023.01.21
[알고리즘-DP] 01타일  (0) 2023.01.21
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-큐] 프린터 큐  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20

문제 제목 : 2×n 타일링

난이도 : 하

문제 유형 : 동적 프로그래밍(DP)

추천 풀이 시간 : 20분 (못하면 2배 40분)

TIP : 가장 적은 경우의 수부터 계산을 해본 후 패턴을 찾아 점화식을 세우자@@

점화식이란? 이웃하는 두개의 항 사이에 성립하는 관계를 나타낸 관계식 예를들어 dp[n + 2] = dp[n + 1] + dp[n + 2]

https://www.acmicpc.net/problem/11726

 

11726번: 2×n 타일링

2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오. 아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] dp = new int[1001];
        dp[0] = 0;
        dp[1] = 1;
        dp[2] = 2;
        
        for (int i = 3; i < n + 1; i++) {
            dp[i] = (dp[i - 1] + dp[i - 2]) % 10007;
        }
        
        System.out.println(dp[n]);
    }
}

 

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 01타일  (0) 2023.01.21
[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[자료구조-큐] 프린터 큐  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20
[자료구조-배열] 블랙잭  (0) 2023.01.19

문제 제목 : 프린터 큐

난이도 : 하

문제 유형 : 큐, 구현, 그리디

추천 풀이 시간 : 25분 (못하면 2배 50분)

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

 

import java.util.*;

class Docs {
    private int index;
    private int priority;
    
    public Docs(int index, int priority) {
        this.index = index;
        this.priority = priority;
    }
    
    public int getIndex() {
        return this.index;
    }
    
    public int getPriority() {
        return this.priority;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        int n;
        int m;
       
        for (int i = 0; i < tc; i++) {
            n = sc.nextInt();
            m = sc.nextInt();
            
            Queue<Docs> docsQueue = new LinkedList<Docs>();
            Queue<Integer> prioQueue = new PriorityQueue<Integer>();
            
            for (int j = 0; j < n; j++) {
                int priority = sc.nextInt();
                docsQueue.add(new Docs(j, priority));
                prioQueue.add(-priority);
            }
            
            int cnt = 0;
            while (true) {
                Docs docs = docsQueue.poll();
                if (docs.getPriority() >= -prioQueue.peek()) {
                    cnt++;
                    prioQueue.poll();
                
                    if (docs.getIndex() == m) {
                        System.out.println(cnt);
                        break;
                    }
                } else {
                    docsQueue.add(new Docs(docs.getIndex(), docs.getPriority()));
                }
            }
        }
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20
[자료구조-배열] 블랙잭  (0) 2023.01.19
[자료구조-배열] 음계  (0) 2023.01.19

문제 제목 : 스택 수열

난이도 : 하

문제 유형 : 스택, 그리디

추천 풀이 시간 : 30분 (못하면 2배 60분)

https://www.acmicpc.net/problem/1874

 

1874번: 스택 수열

1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.

www.acmicpc.net

 

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        ArrayList<Character> result = new ArrayList<>();
        int n = Integer.parseInt(br.readLine());
        Stack<Integer> stack = new Stack<>();
        int num = 1;
        for (int i = 0; i < n; i++) {
            int data = Integer.parseInt(br.readLine());
            while (num <= data) {
                stack.push(num++);
                result.add('+');
            }
            if (stack.peek() == data) {
                stack.pop();
                result.add('-');
            } else {
                System.out.println("NO");
                return;
            }
        }
        
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-큐] 프린터 큐  (0) 2023.01.20
[자료구조-배열] 블랙잭  (0) 2023.01.19
[자료구조-배열] 음계  (0) 2023.01.19

문제 제목 : 블랙잭

난이도 : 하

문제 유형 : 배열, 완전 탐색

추천 풀이 시간 : 20분 (못하면 2배 40분)

https://www.acmicpc.net/problem/2798

 

2798번: 블랙잭

첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        
        int result = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                for (int k = j + 1; k < arr.length; k++) {
                    int sum = arr[i] + arr[j] + arr[k];
                    if (sum <= m) {
                        result = Math.max(result, sum);
                    }
                }
            }
        }
        
        System.out.println(result);
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-큐] 프린터 큐  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20
[자료구조-배열] 음계  (0) 2023.01.19

+ Recent posts