Skip to content

Commit

Permalink
Merge pull request #22 from HYU-PS-STUDY/clean2001
Browse files Browse the repository at this point in the history
[week-05-dp] 25215, 2533
  • Loading branch information
clean2001 authored Feb 22, 2024
2 parents 309ba31 + 85e5567 commit 1374266
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Empty file removed week05-dp/clean2001/.gitkeep
Empty file.
34 changes: 34 additions & 0 deletions week05-dp/clean2001/B25215.java
Original file line number Diff line number Diff line change
@@ -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<s.length(); ++i) {
if(s.charAt(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);
}

}
46 changes: 46 additions & 0 deletions week05-dp/clean2001/B2533.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 2533. 사회망서비스(SNS)
import java.io.*;
import java.util.*;

class B2533 {
static int N;
static List<List<Integer>> 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<N-1; ++i) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

adj.get(a).add(b);
adj.get(b).add(a);
}

dfs(1, 0);
System.out.println(Math.min(dp[0][1], dp[1][1]));
}

static void dfs(int cur, int prev) {
dp[0][cur] = 0;
dp[1][cur] = 1;

List<Integer> 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는 뭐든 상관없음
}
}
}

0 comments on commit 1374266

Please sign in to comment.