From 85e556790de8a9f1d967556b2c293664e4ddae79 Mon Sep 17 00:00:00 2001 From: kimsejeong Date: Tue, 20 Feb 2024 01:13:35 +0900 Subject: [PATCH] [week-05-dp] 25215, 2533 --- week05-dp/clean2001/.gitkeep | 0 week05-dp/clean2001/B25215.java | 34 ++++++++++++++++++++++++ week05-dp/clean2001/B2533.java | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) delete mode 100644 week05-dp/clean2001/.gitkeep create mode 100644 week05-dp/clean2001/B25215.java create mode 100644 week05-dp/clean2001/B2533.java diff --git a/week05-dp/clean2001/.gitkeep b/week05-dp/clean2001/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/week05-dp/clean2001/B25215.java b/week05-dp/clean2001/B25215.java new file mode 100644 index 0000000..d9359f6 --- /dev/null +++ b/week05-dp/clean2001/B25215.java @@ -0,0 +1,34 @@ +// 백준 25215. 타이핑 +import java.io.*; +import java.util.*; + +class B25215 { + static String s; + static int dp[][]; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + s = br.readLine().trim(); + dp = new int[2][s.length()+1]; + + s = " " + s; // 인덱스를 맞춰주기 위해 (문자열의 시작을 1부터) + + dp[0][0] = 0; // 0행 -> 마름모 비활성화 + dp[1][0] = 1; // 1행 -> 마름모 활성화 + + for(int i=1; i= 'a' && s.charAt(i) <= 'z') { // 소문자이면 + dp[0][i] = Math.min(dp[0][i-1] + 1, dp[1][i-1]+2); + dp[1][i] = dp[1][i-1] + 2; // 활성화를 유지 + } else { // 대문자이면 + dp[1][i] = Math.min(dp[1][i-1] + 1, dp[0][i-1]+2); + dp[0][i] = dp[0][i-1] + 2; // 비활유지 + } + } + + int ans = Math.min(dp[0][s.length()-1], dp[1][s.length()-1]); + + System.out.println(ans); + } + +} \ No newline at end of file diff --git a/week05-dp/clean2001/B2533.java b/week05-dp/clean2001/B2533.java new file mode 100644 index 0000000..3464f64 --- /dev/null +++ b/week05-dp/clean2001/B2533.java @@ -0,0 +1,46 @@ +// 2533. 사회망서비스(SNS) +import java.io.*; +import java.util.*; + +class B2533 { + static int N; + static List> adj; + static int[][] dp; // 행: 현재 노드가 얼리어답터인지 아닌지, 열: 현재 노드 번호, 값: 현재 노드부터 시작해서 리프까지 얼리어답터 최소 수 + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + adj = new ArrayList<>(); + dp = new int[2][N+1]; + + for(int i=0; i<=N; ++i) { + adj.add(new ArrayList<>()); + } + + for(int i=0; i nexts = adj.get(cur); + for(int next : nexts) { + if(next == prev) continue; + + dfs(next, cur); + dp[0][cur] += dp[1][next]; // cur가 얼리어가 아니므로, next가 무조건 얼리어야만 함 + dp[1][cur] += Math.min(dp[1][next], dp[0][next]); // cur가 얼리어이므로, next는 뭐든 상관없음 + } + } +} \ No newline at end of file