Notice
Recent Posts
Recent Comments
- Today
- Total
01-11 01:45
Tags
- μλ°μμ μ
- μ‘Έμ μν
- MST
- OOP
- BFS
- PS
- νλ‘κ·Έλλ¨Έμ€
- λ°±μλ
- Graph
- java
- CS
- μλ£κ΅¬μ‘°
- spring
- Algorithm
- dp
- ꡬν
- λ°±μ€
- tree
- pytorch
- database
- λ€μ΅μ€νΈλΌ
- 벨λ§ν¬λ
- 그리λ
- μΈν΄
- μμμ λ ¬
- array
- λ¬Έλ²
- λ°μ΄ν°λ² μ΄μ€
- μλ°
- leetcode
Link
Partially Committed
[λ°±μ€ 1436/10815/11054/16139/16928] μλ° λ³Έλ¬Έ
π₯ Algorithm || λ¬Έμ νμ΄/PS
[λ°±μ€ 1436/10815/11054/16139/16928] μλ°
WonderJay 2023. 2. 16. 14:30728x90
λ°μν
SMALL
(title: [λ°±μ€ 1436/10815/11054/16139/16928] μλ°)
A. μνκ°λ BOJ 1436
https://www.acmicpc.net/problem/1436
λμ΄λ | Silver 5 |
νμ΄ μκ° | 10 λΆ |
λΆλ₯ | ꡬν |
μκ°λ³΅μ‘λ | O(N) |
곡κ°λ³΅μ‘λ | . |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine());
int cnt = 0; long ans = 1; String answer = "";
while(cnt!=n){
String str = String.valueOf(ans);
if(str.contains("666")){
cnt++;
answer = str;
}
ans++;
}
bw.write(answer+"\n");
bw.flush();
bw.close();
}
}
B. μ«μμΉ΄λ BOJ 10815
https://www.acmicpc.net/problem/10815
λμ΄λ | Silver 5 |
νμ΄ μκ° | 5λΆ |
λΆλ₯ | Binary Search |
μκ°λ³΅μ‘λ | O(nlogn) |
곡κ°λ³΅μ‘λ | O(n) |
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine());
int[] card = new int[n];
stk = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
card[i] = Integer.parseInt(stk.nextToken());
}
Arrays.sort(card);
int m = Integer.parseInt(br.readLine());
stk = new StringTokenizer(br.readLine());
for(int i = 0 ; i < m ; i++){
int target= Integer.parseInt(stk.nextToken());
if(binary_search(card, target)!=-1)
bw.write("1 ");
else bw.write("0 ");
}
bw.flush();
bw.close();
}
private static int binary_search(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
int mid = 0;
int midVal = 0;
while (left <= right) {
mid = (left + right) / 2;
midVal = arr[mid];
if(midVal < target){
left = mid+1;
} else if(midVal > target){
right = mid-1;
} else { // midVal == target
return mid;
}
}
return -1;
}
}
C. κ°μ₯ κΈ΄ λ°μ΄ν λ λΆλΆ μμ΄ BOJ 11054
https://www.acmicpc.net/problem/11054
λμ΄λ | Gold 4 |
νμ΄ μκ° | 1μκ° |
λΆλ₯ | DP, LIS |
μκ°λ³΅μ‘λ | O(N^2) |
곡κ°λ³΅μ‘λ | O(1000*2) |
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine()); // μμ΄μ ν¬κΈ°κ° μ£Όμ΄μ§λ€.
int[] arr = new int[n+1];
int[] dp = new int[n+1];
for (int i = 0; i <= n; i++) {
dp[i] = 1;
}
stk = new StringTokenizer(br.readLine());
for (int i = 1; i <= n; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
int max = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int[] revdp = new int[n+1];
for (int i = 0; i <= n; i++) {
revdp[i] = 1;
}
max = 0;
for (int i = n; i >= 0; i--) {
for (int j = n; j > i; j--) {
if (arr[j] < arr[i]) {
revdp[i] = Math.max(revdp[i], revdp[j] + 1);
}
}
}
int ans = 0;
for(int i = 1; i<=n; i++){
ans = Math.max(ans, dp[i]+revdp[i]);
}
bw.write((ans-1)+"\n");
bw.flush();
bw.close();
}
}
D. μΈκ°-μ»΄ν¨ν° μνΈμμ© BOJ 16139
https://www.acmicpc.net/problem/16139
λμ΄λ | Silver 1 |
νμ΄ μκ° | 30 λΆ |
λΆλ₯ | λμ ν© |
μκ°λ³΅μ‘λ | O(N*26) ~= O(N) |
곡κ°λ³΅μ‘λ | O(26*200001) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
String input = br.readLine();
int [][] dp = new int[26][200001];
int [][] arr = new int[26][200001];
for(int i = 0 ; i < input.length(); i++){
int idx = input.charAt(i)-'a';
arr[idx][i]++;
}
for(int i = 0; i<26; i++){
dp[i][0] = arr[i][0];
for(int j = 1; j<=input.length(); j++){
dp[i][j] = dp[i][j-1] + arr[i][j-1];
}
}
int q = Integer.parseInt(br.readLine());
while(q>0){
q--;
stk = new StringTokenizer(br.readLine());
String st = stk.nextToken();
int idx = st.charAt(0)-'a';
int left = Integer.parseInt(stk.nextToken());
int right = Integer.parseInt(stk.nextToken());
int sum = dp[idx][right+1] - dp[idx][left];
bw.write(sum+"\n");
}
bw.flush();
bw.close();
}
}
E. λ±κ³Ό μ¬λ€λ¦¬ κ²μ BOJ 16928
https://www.acmicpc.net/problem/16928
λμ΄λ | Gold 5 |
νμ΄ μκ° | 50λΆ |
λΆλ₯ | κ·Έλν νμ(DFS/BFS) |
μκ°λ³΅μ‘λ | O(V+E=200) |
곡κ°λ³΅μ‘λ | . |
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int[] info;
static int[] dist;
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
info = new int[101];
dist = new int[101];
stk = new StringTokenizer(br.readLine());
int ladder = Integer.parseInt(stk.nextToken());
int snake = Integer.parseInt(stk.nextToken());
for (int i = 0; i < ladder + snake; i++) {
stk = new StringTokenizer(br.readLine());
int start = Integer.parseInt(stk.nextToken());
int end = Integer.parseInt(stk.nextToken());
info[start] = end;
}
Queue<Node> q = new LinkedList<>();
q.add(new Node(1, 0));
while (!q.isEmpty()) {
Node now_node = q.poll();
int pos = now_node.pos;
int cnt = now_node.cnt;
for (int i = 1; i <= 6; i++) {
int next_pos = pos+i;
if(next_pos==100){
// λμ°©μ§μ μ λλ¬ν κ²½μ°
System.out.println(cnt+1);
return;
} else if(next_pos<100){
while(info[next_pos]!=0){
// μ°κ²°λ μ§μ μ νμ
// μμΌλ©΄ κ±°κΈ°λ‘ μ΄λ
next_pos = info[next_pos];
}
if(dist[next_pos]==0){ // μ²μ λ°©λ¬Έν κ³³μ΄λ©΄
q.add(new Node(next_pos, cnt+1));
dist[next_pos] = 1; // λ°©λ¬Έ
}
}
}
}
// bw.flush();
// bw.close();
}
static class Node {
int pos;
int cnt;
public Node(int pos, int cnt) {
this.pos = pos;
this.cnt = cnt;
}
}
}
728x90
λ°μν
LIST
'π₯ Algorithm || λ¬Έμ νμ΄ > PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€ 5568/2559/1504/11066] μλ° (0) | 2023.02.19 |
---|---|
[λ°±μ€ 18870/2565/2470/4195] μλ° (0) | 2023.02.17 |
[λ°±μ€ 7568/1904/1018/25682/2110] μλ° (1) | 2023.02.15 |
[λ°±μ€ 2798/2231/14889/2606] μλ° (0) | 2023.02.14 |
[λ°±μ€ 2580/14888/13305/10844] JAVA (0) | 2023.02.13 |
Comments