-
Notifications
You must be signed in to change notification settings - Fork 2
[지현] 0722 #2
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
Merged
Merged
[지현] 0722 #2
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8199da7
ps: 백준 2011번 암호코드
NamJihyun99 3e333c7
refactor: 백준 2011번 암호코드
NamJihyun99 e28dcc5
ps: 백준 1504번 특정한 최단 경로
NamJihyun99 5cacce3
ps: 백준 1005번 ACMCraft - 위상 정렬 풀이 (824ms)
NamJihyun99 af199ba
ps: 프로그래머스 길 찾기 게임
NamJihyun99 ae21b95
ps: 백준 12919번 A와 B 2
NamJihyun99 e1fe90a
refactor: 백준 2011번 암호코드 - 불필요한 비교 연산 제거
NamJihyun99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import java.util.*; | ||
| import java.io.*; | ||
|
|
||
| // 백준 12919 - A와 B 2 (골드5) | ||
|
|
||
| class Main { | ||
|
|
||
| static String S, T; | ||
|
|
||
| static boolean solve(String str) { | ||
| if (str.length() == S.length()) { | ||
| return S.equals(str.toString()); | ||
| } | ||
| if (str.charAt(str.length()-1)=='A') { | ||
| if (solve(str.substring(0, str.length()-1))) return true; | ||
| } | ||
| if (str.charAt(0) == 'B') { | ||
| StringBuilder sb = new StringBuilder(str); | ||
| if (solve(sb.deleteCharAt(0).reverse().toString())) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | ||
| S = bf.readLine(); | ||
| T = bf.readLine(); | ||
| System.out.println(solve(T)? 1: 0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import java.util.*; | ||
| import java.io.*; | ||
|
|
||
| // 백준 2011 - 암호코드 (골드5) | ||
|
|
||
| class Main { | ||
|
|
||
| static final int MOD = 1_000_000; | ||
|
|
||
| static boolean isPositiveDigit(String str, int idx) { | ||
| return str.charAt(idx)>'0' && str.charAt(idx)<='9'; | ||
| } | ||
|
|
||
| static boolean canDecode(String str, int lastIdx) { | ||
| return str.charAt(lastIdx-1)=='1' || str.charAt(lastIdx-1)=='2' && str.charAt(lastIdx)<='6'; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | ||
| String input = bf.readLine(); | ||
| int[] counts = new int[input.length()+1]; | ||
| counts[0]=1; | ||
| for (int i=1; i<counts.length; i++) { | ||
| if (isPositiveDigit(input, i-1)) { | ||
| counts[i] += counts[i-1]; | ||
| } | ||
| if (i>=2 && canDecode(input, i-1)) { | ||
| counts[i] += counts[i-2]; | ||
| } | ||
| counts[i]%=MOD; | ||
| } | ||
| System.out.println(counts[input.length()]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import java.util.*; | ||
| import java.io.*; | ||
|
|
||
| // 백준 1005 - ACM Craft (골드3) 위상 정렬 | ||
|
|
||
| class Main { | ||
|
|
||
| static List<List<Integer>> graph; | ||
| static int[] costs, times, inDegree; | ||
| static int N; | ||
|
|
||
| static int solution(int destination) { | ||
| ArrayDeque<Integer> queue = new ArrayDeque<>(); | ||
| for (int i=1; i<=N; i++) { | ||
| if (inDegree[i]==0) { | ||
| queue.addLast(i); | ||
| times[i] = costs[i]; | ||
| } | ||
| } | ||
| while (! queue.isEmpty()) { | ||
| int now = queue.pollFirst(); | ||
| for (int next: graph.get(now)) { | ||
| inDegree[next]--; | ||
| if (inDegree[next]==0) queue.addLast(next); | ||
| times[next] = Math.max(times[next], times[now]+costs[next]); | ||
| } | ||
| } | ||
| return times[destination]; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | ||
| int T = Integer.parseInt(bf.readLine()); | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int t=0; t<T; t++) { | ||
| StringTokenizer st = new StringTokenizer(bf.readLine()); | ||
| N = Integer.parseInt(st.nextToken()); | ||
| int K = Integer.parseInt(st.nextToken()); | ||
| costs = new int[N+1]; | ||
| times = new int[N+1]; | ||
| inDegree = new int[N+1]; | ||
| graph = new ArrayList<>(); | ||
| st = new StringTokenizer(bf.readLine()); | ||
| graph.add(new ArrayList<>()); | ||
| for (int i=1; i<=N; i++) { | ||
| costs[i] = Integer.parseInt(st.nextToken()); | ||
| graph.add(new ArrayList<>()); | ||
| } | ||
| for (int i=0; i<K; i++) { | ||
| st = new StringTokenizer(bf.readLine()); | ||
| int dep = Integer.parseInt(st.nextToken()); | ||
| int dst = Integer.parseInt(st.nextToken()); | ||
| graph.get(dep).add(dst); | ||
| inDegree[dst]++; | ||
| } | ||
| sb.append(solution(Integer.parseInt(bf.readLine()))).append("\n"); | ||
| } | ||
| System.out.print(sb); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import java.util.*; | ||
| import java.io.*; | ||
|
|
||
| // 백준 1504 - 특정한 최단 경로 (골드4) | ||
|
|
||
| class Main { | ||
|
|
||
| static Map<Integer, List<int[]>> graph; | ||
| static int[] dist; | ||
| static int N; | ||
|
|
||
| static final int MAX = 200_000_001; | ||
|
|
||
| static class Edge implements Comparable<Edge>{ | ||
| int dest; | ||
| int value; | ||
|
|
||
| Edge(int dest, int value) { | ||
| this.dest = dest; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int compareTo(Edge e) { | ||
| return this.value-e.value; | ||
| } | ||
| } | ||
|
|
||
| private static void dijkstra(int departure) { | ||
| boolean[] visited = new boolean[N+1]; | ||
| dist = new int[N+1]; | ||
| Arrays.fill(dist, MAX); | ||
| PriorityQueue<Edge> pq = new PriorityQueue<>(); | ||
| pq.add(new Edge(departure, 0)); | ||
| dist[departure]=0; | ||
| while (!pq.isEmpty()) { | ||
| Edge now = pq.poll(); | ||
| if (visited[now.dest]) continue; | ||
| visited[now.dest] = true; | ||
| for (int[] next: graph.get(now.dest)) { | ||
| if (dist[next[0]] > dist[now.dest]+next[1]) { | ||
| dist[next[0]] = dist[now.dest]+next[1]; | ||
| pq.add(new Edge(next[0], dist[next[0]])); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | ||
| StringTokenizer st = new StringTokenizer(bf.readLine()); | ||
| N = Integer.parseInt(st.nextToken()); | ||
| int E = Integer.parseInt(st.nextToken()); | ||
| graph = new HashMap<>(); | ||
| for (int dep=1; dep<=N; dep++) { | ||
| graph.put(dep, new ArrayList<>()); | ||
| } | ||
| for (int i=0; i<E; i++) { | ||
| st = new StringTokenizer(bf.readLine()); | ||
| int x = Integer.parseInt(st.nextToken()); | ||
| int y = Integer.parseInt(st.nextToken()); | ||
| int v = Integer.parseInt(st.nextToken()); | ||
| graph.get(x).add(new int[]{y, v}); | ||
| graph.get(y).add(new int[]{x, v}); | ||
| } | ||
| st = new StringTokenizer(bf.readLine()); | ||
| int v1 = Integer.parseInt(st.nextToken()); | ||
| int v2 = Integer.parseInt(st.nextToken()); | ||
|
|
||
| dijkstra(v1); | ||
| int v1ToV2 = dist[v2]; | ||
|
|
||
| dijkstra(1); | ||
| int dstToV1 = dist[v1]; | ||
| int dstToV2 = dist[v2]; | ||
|
|
||
| dijkstra(N); | ||
| int v1ToN = dist[v1]; | ||
| int v2ToN = dist[v2]; | ||
|
|
||
| int answer = MAX * 3; | ||
| if (v1ToV2 != MAX && dstToV1 != MAX && v2ToN != MAX) { | ||
| answer = Math.min(answer, dstToV1 + v1ToV2 + v2ToN); | ||
| } | ||
| if (v1ToV2 != MAX && dstToV2 != MAX && v1ToN != MAX) { | ||
| answer = Math.min(answer, dstToV2 + v1ToV2 + v1ToN); | ||
| } | ||
| if (answer == MAX*3) { | ||
| answer = -1; | ||
| } | ||
| System.out.println(answer); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import java.util.*; | ||
|
|
||
| // 프로그래머스 - 길 찾기 게임 (Lv 3) | ||
|
|
||
| class Solution { | ||
| int[][] answer; | ||
| int idx; | ||
|
|
||
| public int[][] solution(int[][] nodeinfo) { | ||
| answer = new int[2][nodeinfo.length]; | ||
| List<Node> nodes = new ArrayList<>(); | ||
| for (int i=0; i<nodeinfo.length; i++) { | ||
| nodes.add(new Node(i+1, nodeinfo[i][0], nodeinfo[i][1])); | ||
| } | ||
| Collections.sort(nodes); | ||
| Node root = nodes.get(0); | ||
| for (int i=1; i<nodes.size(); i++) { | ||
| makeTree(root, nodes.get(i)); | ||
| } | ||
| idx=0; | ||
| preOrder(root); | ||
| idx=0; | ||
| postOrder(root); | ||
| return answer; | ||
| } | ||
|
|
||
| private void makeTree(Node parent, Node child) { | ||
| if (parent.x > child.x) { | ||
| if (parent.left == null) parent.left = child; | ||
| else makeTree(parent.left, child); | ||
| } else { | ||
| if (parent.right == null) parent.right = child; | ||
| else makeTree(parent.right, child); | ||
| } | ||
| } | ||
|
|
||
| private void preOrder(Node node) { | ||
| answer[0][idx++] = node.num; | ||
| if (node.left != null) preOrder(node.left); | ||
| if (node.right != null) preOrder(node.right); | ||
| } | ||
|
|
||
| private void postOrder(Node node) { | ||
| if (node.left != null) postOrder(node.left); | ||
| if (node.right != null) postOrder(node.right); | ||
| answer[1][idx++] = node.num; | ||
| } | ||
|
|
||
| static class Node implements Comparable<Node> { | ||
| int num; | ||
| int x; | ||
| int y; | ||
| Node left; | ||
| Node right; | ||
|
|
||
| Node(int num, int x, int y) { | ||
| this.num = num; | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public int compareTo(Node node) { | ||
| return this.y==node.y? this.x-node.x: node.y-this.y; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열에 idx를 밖으로 빼고 바로 넣어주셨네요 !!