Skip to content

Commit dbff211

Browse files
authored
Merge pull request #58 from solid-connection/main
merge
2 parents 330dd2f + 36c4978 commit dbff211

File tree

17 files changed

+843
-0
lines changed

17 files changed

+843
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(String s) {
5+
int answer = Integer.MAX_VALUE;
6+
int len = 1; //단위
7+
int N = s.length();
8+
9+
while (len <= N) {
10+
StringBuilder resultBuilder = new StringBuilder();
11+
StringBuilder wordBuilder = new StringBuilder();
12+
String prev = "";
13+
String word = "";
14+
int cnt = 0;
15+
for (int i = 0; i <= N; i++) {
16+
word = wordBuilder.toString();
17+
if (word.length() == len) {
18+
if (prev.equals("")) { //맨 처음일 경우 prev에 저장
19+
prev = word;
20+
cnt++;
21+
} else if (prev.equals(word)) { //연속된 단어일 경우 개수 카운트
22+
cnt++;
23+
} else { //새로운 단어일 경우 이전 단어 기록
24+
if (cnt <= 1) resultBuilder.append(prev);
25+
else resultBuilder.append(cnt + prev);
26+
prev = word;
27+
cnt = 1;
28+
}
29+
wordBuilder = new StringBuilder(); //초기화
30+
}
31+
if (i < N) wordBuilder.append(s.charAt(i));
32+
}
33+
34+
//마지막 처리
35+
if (cnt <= 1) resultBuilder.append(prev);
36+
else resultBuilder.append(cnt + prev);
37+
resultBuilder.append(wordBuilder.toString()); //남은 글자들
38+
39+
//정답 구하기
40+
answer = Math.min(resultBuilder.length(), answer);
41+
len++;
42+
}
43+
44+
return answer;
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package week05.BOJ_14002_가장긴증가하는부분수열4;
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class BOJ14002 {
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int N = Integer.parseInt(br.readLine());
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
int[] arr = new int[N];
13+
for (int i = 0; i < N; i++) {
14+
arr[i] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
int[] dp = new int[N];
18+
for (int i = 0; i < N; i++) {
19+
dp[i] = 1;
20+
for (int j = 0; j < i; j++) {
21+
if (arr[j] < arr[i]) {
22+
dp[i] = Math.max(dp[i], dp[j] + 1);
23+
}
24+
}
25+
}
26+
27+
StringBuilder answer = new StringBuilder();
28+
int max = 0;
29+
for (int i = 0; i < N; i++) {
30+
if (dp[i] > max) {
31+
max = dp[i];
32+
}
33+
}
34+
answer.append(max).append("\n");
35+
36+
Stack<Integer> stack = new Stack<>();
37+
for (int i = N - 1; i >= 0; i--) {
38+
if (dp[i] == max) {
39+
stack.push(arr[i]);
40+
max--;
41+
}
42+
}
43+
44+
while (!stack.isEmpty()) {
45+
answer.append(stack.pop()).append(" ");
46+
}
47+
48+
System.out.println(answer);
49+
}
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package week05.BOJ_1781_컵라면;
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
6+
class BOJ1781 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int N = Integer.parseInt(br.readLine());
10+
int[][] works = new int[N][2]; //데드라인, 컵라면 수
11+
for (int i = 0; i < N; i++) {
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
works[i][0] = Integer.parseInt(st.nextToken());
14+
works[i][1] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
Arrays.sort(works, (o1, o2) -> {
18+
if (o1[0] == o2[0]) return o2[1] - o1[1];
19+
return o1[0] - o2[0];
20+
});
21+
22+
PriorityQueue<Integer> pq = new PriorityQueue<>(); //푼 문제의 컵라면 수 저장
23+
for (int i = 0; i < N; i++) {
24+
25+
//pq 사이즈 == 푼 문제 수 == 사용한 시간과 데드라인 시간 비교
26+
if (pq.size() < works[i][0]) {
27+
pq.offer(works[i][1]);
28+
} else {
29+
if (pq.peek() < works[i][1]) { //기존에 풀었던 문제의 컵라면 수보다 크면 교체
30+
pq.poll();
31+
pq.offer(works[i][1]);
32+
}
33+
}
34+
}
35+
36+
//출력
37+
int answer = 0;
38+
while (!pq.isEmpty()) {
39+
answer += pq.poll();
40+
}
41+
System.out.println(answer);
42+
}
43+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package study.week05;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.PriorityQueue;
7+
8+
// 컵라면
9+
public class BOJ_1781 {
10+
11+
static class Problem implements Comparable<Problem> {
12+
13+
int deadLine;
14+
int cup;
15+
16+
public Problem(int deadLine, int cup) {
17+
this.deadLine = deadLine;
18+
this.cup = cup;
19+
}
20+
21+
@Override
22+
public int compareTo(Problem o) {
23+
if (this.deadLine == o.deadLine) {
24+
return o.cup - this.cup;
25+
}
26+
return this.deadLine - o.deadLine;
27+
}
28+
}
29+
30+
static PriorityQueue<Problem> pq;
31+
static int N;
32+
33+
public static void main(String[] args) throws IOException {
34+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
35+
36+
N = Integer.parseInt(br.readLine());
37+
pq = new PriorityQueue<>();
38+
39+
for (int i = 0; i < N; i++) {
40+
String[] s = br.readLine().split(" ");
41+
int deadLine = Integer.parseInt(s[0]);
42+
int cup = Integer.parseInt(s[1]);
43+
pq.add(new Problem(deadLine, cup));
44+
}
45+
46+
PriorityQueue<Integer> select = new PriorityQueue<>();
47+
48+
while(!pq.isEmpty()){
49+
Problem problem = pq.poll();
50+
int deadLine = problem.deadLine;
51+
int cup = problem.cup;
52+
53+
if(select.size() < deadLine){
54+
select.offer(cup);
55+
}else{
56+
if(!select.isEmpty() && select.peek() < cup){ // 만약 선택한 문제 중 가장 적은 컵라면보다 현재 추가하려는 problem 의 컵라면 개수가 더 많다면
57+
select.poll();
58+
select.offer(cup);
59+
}
60+
}
61+
}
62+
63+
long result = 0;
64+
for(int cup : select){
65+
result += cup;
66+
}
67+
68+
System.out.println(result);
69+
}
70+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package week05.BOJ_6236_용돈관리;
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class BOJ6236 {
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
int N = Integer.parseInt(st.nextToken());
12+
int M = Integer.parseInt(st.nextToken());
13+
int[] money = new int[N];
14+
int max = 0;
15+
for (int i = 0; i < N; i++) {
16+
money[i] = Integer.parseInt(br.readLine());
17+
max = Math.max(money[i], max); //최대 금액을 최솟값으로 설정
18+
}
19+
20+
int answer = 0;
21+
int left = max;
22+
int right = 10000 * 100000;
23+
while (left <= right) {
24+
int K = (left + right) / 2;
25+
26+
//결과 계산
27+
int result = getWithdrawCnt(money, K, M);
28+
29+
if (M >= result) {
30+
answer = K;
31+
right = K - 1;
32+
} else {
33+
left = K + 1;
34+
}
35+
}
36+
System.out.println(answer);
37+
}
38+
39+
static int getWithdrawCnt(int[] money, int K, int M) {
40+
int withdrawCnt = 1;
41+
int now = K;
42+
for (int i = 0; i < money.length; i++) {
43+
if (money[i] > now) { //모자라면 K원 인출
44+
withdrawCnt++;
45+
now = K;
46+
}
47+
now -= money[i];
48+
}
49+
return withdrawCnt;
50+
}
51+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package week06.BOJ_17070_파이프옮기기1;
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
import java.io.*;
6+
7+
class BOJ17070 {
8+
static final int WALL = 1;
9+
static int answer = 0;
10+
static int N;
11+
static int[][] house;
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
N = Integer.parseInt(br.readLine());
15+
house = new int[N][N];
16+
for (int i = 0; i < N; i++) {
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for (int j = 0; j < N; j++) {
19+
house[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
move(0, 1, 'H'); //0,1에서 시작
23+
System.out.println(answer);
24+
}
25+
26+
static void move(int x, int y, char dir) {
27+
//각 dir에 따라 갈 방법이 없으면 return
28+
if (dir == 'H' || dir == 'V') {
29+
if (!isPossible(x, y) || house[x][y] == WALL) {
30+
return;
31+
}
32+
} else {
33+
if (!isPossible(x, y) || !isPossible(x-1, y) || !isPossible(x, y-1) ||
34+
house[x][y] == WALL || house[x-1][y] == WALL || house[x][y-1] == WALL) {
35+
return;
36+
}
37+
}
38+
39+
if (x == N - 1 && y == N - 1) { //도착
40+
answer++;
41+
return;
42+
}
43+
44+
if (dir == 'H') { //가로 이동
45+
move(x, y+1, 'H'); //1번
46+
move(x+1, y+1, 'D'); //2번
47+
48+
} else if (dir == 'V') { //세로 이동
49+
move(x+1, y, 'V'); //1번
50+
move(x+1, y+1, 'D'); //2번
51+
52+
} else { //대각선 이동
53+
move(x, y+1, 'H'); //1번
54+
move(x+1, y, 'V'); //2번
55+
move(x+1, y+1, 'D'); //3번
56+
}
57+
58+
}
59+
60+
static boolean isPossible(int x, int y) {
61+
return x >= 0 && x < N && y >= 0 && y < N;
62+
}
63+
}

0 commit comments

Comments
 (0)