Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Week 5 #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
33 changes: 33 additions & 0 deletions src/baekjoon/BOJ1149.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1149 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] cost = new int[N][3];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 3; j++) {
cost[i][j] = Integer.parseInt(st.nextToken());
}
}
int [][] dp = new int[N][3];
dp[0][0]=cost[0][0];
dp[0][1]=cost[0][1];
dp[0][2]=cost[0][2];

for (int i=1;i<N;i++){
dp[i][0]=Math.min(dp[i-1][1],dp[i-1][2])+cost[i][0];
dp[i][1]=Math.min(dp[i-1][0],dp[i-1][2])+cost[i][1];
dp[i][2]=Math.min(dp[i-1][0],dp[i-1][1])+cost[i][2];
}

System.out.println(Math.min(dp[N-1][0] , Math.min(dp[N-1][1], dp[N-1][2])));
}
}
29 changes: 29 additions & 0 deletions src/baekjoon/BOJ13750.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ13750 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int [] A = new int[N];
int [] low = new int[N];
int [] high = new int[N];
for (int i = 0; i < N; i++) {
A[i] = Integer.parseInt(br.readLine());
low[i] = 1;
high[i] = 1;
}
for (int k = 0; k < N; k++) {
for (int j = 0; j < k; j++) {
if (A[j] > A[k])
low[k] = Math.max(low[k], high[j] + 1);
if (A[j] < A[k])
high[k] = Math.max(high[k], low[j] + 1);
}
}
System.out.println(Math.max(low[N-1], high[N-1]));
}
}
49 changes: 49 additions & 0 deletions src/baekjoon/BOJ1520.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1520 {
static int N, M;
static int[][] map;
static int[][] dp;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, -1, 1};

static int dfs(int x, int y) {
dp[x][y] = 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < M && ny < N && map[x][y] > map[nx][ny]) {
if (dp[nx][ny] == -1) {
dp[x][y] += dfs(nx, ny);
} else {
dp[x][y] += dp[nx][ny];
}
}
}
return dp[x][y];
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
map = new int[M][N];
dp = new int[M][N];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
dp[i][j] = -1;
}
}
dp[M - 1][N - 1] = 1;
dfs(0, 0);
System.out.println(dp[0][0]);
}
}
44 changes: 44 additions & 0 deletions src/baekjoon/BOJ15961.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ15961 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
int [] visited = new int[d+1];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
int count = 0;
for (int i = 0; i < k; i++) {
if (visited[arr[i]] == 0){
count++;
}
visited[arr[i]]++;
}
int result = count;
for (int i = 0; i < N; i++) {
if (visited[c] == 0){
result = Math.max(result, count+1);
} else {
result = Math.max(result, count);
}
if (--visited[arr[i]] == 0){
count--;
}
if (visited[arr[(i+k)%N]]++ == 0){
count++;
}
}
System.out.println(result);
}
}
24 changes: 24 additions & 0 deletions src/baekjoon/BOJ1912.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1912 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int [] dp = new int[N+1];
StringTokenizer st = new StringTokenizer(br.readLine());
dp[0] = 0;
int max = Integer.MIN_VALUE;
for (int i = 1; i <= N ; i++) {
dp[i] = Integer.parseInt(st.nextToken());
dp[i] = Math.max(dp[i-1] + dp[i], dp[i]);
if (dp[i] > max)
max = dp[i];
}
System.out.println(max);
}
}
55 changes: 55 additions & 0 deletions src/baekjoon/BOJ1937.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1937 {
static int [][] map;
static int [][] dp;
static int N;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int answer = Integer.MIN_VALUE;

static int dfs(int x, int y){
dp[x][y]=1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N && map[x][y] < map[nx][ny]){
if (dp[nx][ny] == -1){
dp[x][y] = Math.max(dfs(nx, ny)+1, dp[x][y]);
} else {
dp[x][y] = Math.max(dp[nx][ny]+1, dp[x][y]);
}
}
}
answer = Math.max(dp[x][y], answer);
return dp[x][y];
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st;
map = new int[N][N];
dp = new int[N][N];
for (int i = 0; i <N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
dp[i][j] = -1;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (dp[i][j]==-1){
dfs(i, j);
}
}
}
System.out.println(answer);
}
}
74 changes: 74 additions & 0 deletions src/baekjoon/BOJ20437.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class BOJ20437 {

static List<Integer>[] characters = new ArrayList[28];

private static int getShortestLength(int chIdx, int K) {
int i = 0;
int j = K-1;
int minLength = Integer.MAX_VALUE;
while (j < characters[chIdx].size()){
if (minLength > (characters[chIdx].get(j) - characters[chIdx].get(i)) + 1){
minLength = characters[chIdx].get(j) - characters[chIdx].get(i) + 1;
}
i++;j++;
}
return minLength;
}

private static int getLongestLength(int chIdx, int K) {
int i = 0;
int j = K-1;
int maxLength= Integer.MIN_VALUE;
while (j < characters[chIdx].size()){
if (maxLength < (characters[chIdx].get(j) - characters[chIdx].get(i)) +1){
maxLength = characters[chIdx].get(j) - characters[chIdx].get(i) + 1;
}
i++;j++;
}
return maxLength;
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int t = 0; t < T; t++) {
String W = br.readLine();
int K = Integer.parseInt(br.readLine());
for (int i = 0; i < 28; i++) {
characters[i] = new ArrayList<>();
}
for (int i = 0; i < W.length(); i++) {
characters[W.charAt(i) - 'a'].add(i);
}
int ans1 = Integer.MAX_VALUE;
int ans2 = Integer.MIN_VALUE;
for (int i = 0; i < 28; i++) {
if (characters[i].size() >= K) {
int result1 = getShortestLength(i, K);
int result2 = getLongestLength(i, K);
if (ans1 > result1) {
ans1 = result1;
}
if (ans2 < result2){
ans2 = result2;
}
}
}
if (ans1 == Integer.MAX_VALUE) {
sb.append(-1).append("\n");
} else {
sb.append(ans1).append(" ").append(ans2).append("\n");
}
}
System.out.print(sb);
}
}
66 changes: 66 additions & 0 deletions src/baekjoon/BOJ2239.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

public class BOJ2239 {

static int [][] arr = new int[9][9];
static boolean [][] row = new boolean[9][9];
static boolean [][] col = new boolean[9][9];
static boolean [][] square = new boolean[9][9];
static StringBuilder sb = new StringBuilder();

private static void sudoku(int cnt){
if (cnt == 81){
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sb.append(arr[i][j]);
}
sb.append("\n");
}
System.out.print(sb);
System.exit(0);
}
int i = cnt / 9;
int j = cnt % 9;
if (arr[i][j]==0){
for (int k = 0; k < 9; k++) {
if (!row[i][k] && !col[j][k] && !square[(i/3)*3+(j/3)][k]){
arr[i][j] = k+1;
row[i][k] = true;
col[j][k] = true;
square[(i/3)*3+(j/3)][k] = true;
sudoku(cnt+1);
arr[i][j] = 0;
row[i][k] = false;
col[j][k] = false;
square[(i/3)*3+(j/3)][k] = false;
}
}
} else {
sudoku(cnt+1);
}

}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 9; i++) {
String input = br.readLine();
for (int j = 0; j < 9; j++) {
arr[i][j] = input.charAt(j)-'0';
if (arr[i][j] != 0){
row[i][arr[i][j]-1] = true;
col[j][arr[i][j]-1] = true;
square[(i/3)*3+(j/3)][arr[i][j]-1] = true;
}
}
}
sudoku(0);
}
}

Loading