Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/algorithm/kwangwoo/week8/boj/q13398/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package algorithm.kwangwoo.week8.boj.q13398;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int[] arr = new int[n];
int[][] dp = new int[2][n];

for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

int max = arr[0];
dp[0][0] = arr[0];
dp[1][0] = arr[0];

// dp[0][i] : 처음부터 i까지 모든 수를 더했을 때의 최대값 (max(이전까지의 최대값+자기자신 vs 자기자신))
// dp[1][i] : 자기자신을 제거(앞에서 제거한 적X)하거나 안하거나(앞에서 제거한적 O)

for (int i = 1; i < n; i++) {
// 이전까지 합 + 자신 vs 자신
dp[0][i] = Math.max(dp[0][i - 1] + arr[i], arr[i]);
// 이전까지의 합(자신 제외) vs 이전에 한번 제외했던 합 + 자신
dp[1][i] = Math.max(dp[0][i - 1], dp[1][i - 1] + arr[i]);

// 제일 큰 값
max = Math.max(max, Math.max(dp[0][i], dp[1][i]));
}

System.out.println(max);

sc.close();
}
}
77 changes: 77 additions & 0 deletions src/algorithm/kwangwoo/week8/boj/q14226/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package algorithm.kwangwoo.week8.boj.q14226;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/*
visited[클리보드 개수][스크린 개수] 로 나눠서 진행

목표값을 초과한 후 지워서 값을 맞추는 경우는 없는 것인가?



*/

public class Main {
static int S;

static boolean[][] visited;

static class emoticon {
int clipboard = 0;
int screen = 0;
int sec = 0;

emoticon(int clipboard, int screen, int sec){
this.clipboard = clipboard;
this.screen = screen;
this.sec = sec;
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

S = sc.nextInt();
// [클립보드][스크린]
// 범위가 S+1 이면 충분한게 맞나..?
visited = new boolean[S + 1][S + 1];

bfs(S);

sc.close();
}

static void bfs(int target) {
Queue<emoticon> q = new LinkedList<>();
q.offer(new emoticon(0, 1, 0));
visited[0][1] = true;

while (!q.isEmpty()) {
emoticon cur = q.poll();

// 목표에 도달하면 끝
if (cur.screen == target) {
System.out.println(cur.sec);
return;
}

// 1. 화면에 있는 이모티콘을 모두 복사해서 클립보드에 저장한다.
q.offer(new emoticon(cur.screen, cur.screen, cur.sec + 1));

// 2. 클립보드에 있는 모든 이모티콘을 화면에 붙여넣기 한다.
// cur.screen + cur.clipboard <= target 이어야하는 이유는?
if (cur.clipboard != 0 && cur.screen + cur.clipboard <= target && !visited[cur.clipboard][cur.screen + cur.clipboard]) {
q.offer(new emoticon(cur.clipboard, cur.screen + cur.clipboard, cur.sec + 1));
visited[cur.clipboard][cur.screen + cur.clipboard] = true;
}

// 3. 화면에 있는 이모티콘 중 하나를 삭제한다.
if (1 <= cur.screen && !visited[cur.clipboard][cur.screen - 1]) {
q.offer(new emoticon(cur.clipboard, cur.screen - 1, cur.sec + 1));
visited[cur.clipboard][cur.screen - 1] = true;
}
}
}
}
47 changes: 47 additions & 0 deletions src/algorithm/kwangwoo/week8/boj/q2811/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package algorithm.kwangwoo.week8.boj.q2811;

import java.util.Scanner;
import java.util.Stack;

public class Main {

static int N;
static int K;
static int[] arrInputInt;
static int count = 0;
static Stack<Integer> stack = new Stack<>();

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);

N = sc.nextInt();
K = sc.nextInt();
String input = sc.next();
arrInputInt = new int[N];

// 입력숫자의 각 자리를 배열에 넣음
for (int i = 0; i < input.length(); i++) {
arrInputInt[i] = input.charAt(i) - '0';
}

// 다음 자리수가 더 크다면 빼는게 더 이득임!
for (int i = 0; i < arrInputInt.length; i++) {
// 뺄 숫자가 남아있고(count<K), stack이 비어있지 않으며, "stack의 top이 현재 자리수보다 작다면"
while (count < K && !stack.isEmpty() && stack.peek() < arrInputInt[i]) {
// 스택에 있던 기존 값을 빼버림
stack.pop();
count++;
}
// 해당 자릿수를 스택에 push
stack.push(arrInputInt[i]);
}

// 출력
for (int i = 0; i < N - K; i++) {
// 스택 요소를 bottom부터 출력
System.out.print(stack.elementAt(i));
}

sc.close();
}
}