-
Notifications
You must be signed in to change notification settings - Fork 3
week2/Vryez11 #10
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
week2/Vryez11 #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package week2.괄호회전하기.Vryez11; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.Queue; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 괄호 회전하기 | ||
| * | ||
| * 문제 난이도: Lv2 | ||
| * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/76502 | ||
| * 풀이 시간: 30분 | ||
| * 풀이 근거: 일단 괄호 검사 함수를 먼저 만듬 -> Queue로 쉽게 앞에 있는 것을 빼서 뒤에 넣을 수 있겠다고 생각 | ||
| */ | ||
|
|
||
| public int solution(String s) { | ||
|
|
||
| Queue<Character> queue = new ArrayDeque<>(); | ||
| for (int i = 0; i < s.length(); i++) { | ||
| queue.add(s.charAt(i)); | ||
| } | ||
| int ans = 0; | ||
|
|
||
| for (int i = 0; i < s.length() - 1; i++) { | ||
|
|
||
| if (isValidBracket(queue)) { | ||
| ans++; | ||
| } | ||
|
|
||
| queue.add(queue.poll()); | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
|
|
||
| private boolean isValidBracket(Queue<Character> queue) { | ||
|
|
||
| Deque<Character> stack = new ArrayDeque<>(); | ||
|
|
||
| for (char bracket : queue) { | ||
|
|
||
| if (bracket == '[' || bracket == '(' || bracket == '{') { | ||
| stack.push(bracket); | ||
| continue; | ||
| } | ||
|
|
||
| if (stack.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| Character next = stack.peek(); | ||
|
|
||
| if (bracket == ']' && next != '[') { | ||
| return false; | ||
| } | ||
| if (bracket == ')' && next != '(') { | ||
| return false; | ||
| } | ||
| if (bracket == '}' && next != '{') { | ||
| return false; | ||
| } | ||
|
|
||
| stack.pop(); | ||
| } | ||
|
|
||
| return stack.isEmpty(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package week2.기지국설치.Vryez11; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 기지국 설치 | ||
| * | ||
| * 문제 난이도: Lv.3 | ||
| * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12979 | ||
| * 풀이 시간: 30분 | ||
| * 풀이 근거: 처음에는 boolean[]로 풀이로 시간 초과 -> 문제 다시 보니, n의 값이 2억 이하 -> 이건 수학적으로 접근해야 된다. -> seq 연속된 것만 구해서 나눗셈 더하면 되겠네 | ||
| */ | ||
|
|
||
| public int solution(int n, int[] stations, int w) { | ||
|
|
||
| int pre = 1, ans = 0; | ||
| int range = (2 * w + 1); | ||
|
|
||
| for (int station : stations) { | ||
|
|
||
| int seq = station - w - pre; | ||
| ans += getCount(seq, range); | ||
| pre = station + w + 1; | ||
| } | ||
|
|
||
| return ans + getCount(n - pre + 1, range); | ||
| } | ||
|
|
||
| private int getCount(int seq, int range) { | ||
| if (seq <= 0) return 0; | ||
| return (seq + range - 1) / range; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package week2.멀리뛰기.Vreyz11; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 멀리 뛰기 | ||
| * | ||
| * 문제 난이도 : Lv. 2 | ||
| * 문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/12914 | ||
| * 풀이 시간 : 10분 | ||
| * 풀이 근거 : n-1에서 1로 뛰면 n / n-2에서 2로 뛰면 n / 따라서 n - 1, n - 2합 축적 | ||
| */ | ||
|
|
||
| static final int MOD = 1_234_567; | ||
|
|
||
| public long solution(int n) { | ||
|
|
||
| long[] dp = new long[n + 1]; | ||
|
|
||
| if (n == 1) return 1; | ||
|
|
||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
|
|
||
| for (int i = 3; i <= n; i++) { | ||
|
|
||
| dp[i] = (dp[i - 2] + dp[i - 1]) % MOD; | ||
| } | ||
|
|
||
| return dp[n]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package week2.섬연결하기.Vryez11; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 섬 연결하기 | ||
| * | ||
| * 문제 난이도 : Lv. 3 | ||
| * 문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42861 | ||
| * 풀이 시간 : 20분 | ||
| * 풀이 근거 : costs 배열을 cost로 정렬 / union-find로 연결된 섬 O(1)로 확인 | ||
| */ | ||
|
|
||
| int[] parent; | ||
|
|
||
| public int solution(int n, int[][] costs) { | ||
|
|
||
| Arrays.sort(costs, (o1, o2) -> { | ||
| return Integer.compare(o1[2], o2[2]); | ||
| }); | ||
|
|
||
| int ans = 0; | ||
| int line = 0; | ||
|
|
||
| parent = new int[n]; | ||
| for (int i = 0; i < parent.length; i++) { | ||
| parent[i] = i; | ||
| } | ||
|
|
||
| for (int[] cost : costs) { | ||
|
|
||
| if (line == n - 1) break; | ||
|
|
||
| int start = cost[0]; | ||
| int end = cost[1]; | ||
|
|
||
| if (find(start) == find(end)) continue; | ||
|
|
||
| union(start, end); | ||
|
|
||
| ans += cost[2]; | ||
| line++; | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
|
|
||
| private int find(int node) { | ||
|
|
||
| if (parent[node] == node) return node; | ||
|
|
||
| return parent[node] = find(parent[node]); | ||
| } | ||
|
|
||
| private void union(int start, int end) { | ||
|
|
||
| int startParent = find(start); | ||
| int endParent = find(end); | ||
|
|
||
| if (startParent == endParent) return; | ||
|
|
||
| if (startParent < endParent) { | ||
| parent[startParent] = endParent; | ||
| } | ||
|
|
||
| if (startParent > endParent) { | ||
| parent[endParent] = startParent; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package week2.징검다리건너기.Vryez11; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 징검다리 건너기 | ||
| * | ||
| * 문제 난이도: Lv. 3 | ||
| * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/64062 | ||
| * 풀이 시간: 20분 | ||
| * 풀이 근거: | ||
| */ | ||
|
|
||
| public int solution(int[] stones, int k) { | ||
|
|
||
| int left = 1; | ||
| int right = 200_000_000; | ||
| int answer = 0; | ||
|
|
||
| while (left <= right) { | ||
| int mid = left + (right - left) / 2; | ||
|
|
||
| if (canCross(stones, k, mid)) { | ||
| answer = mid; | ||
| left = mid + 1; | ||
| } else { | ||
| right = mid - 1; | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| private boolean canCross(int[] stones, int k, int people) { | ||
| int count = 0; | ||
|
|
||
| for (int stone : stones) { | ||
| if (stone < people) { | ||
| count++; | ||
|
|
||
| if (count >= k) { | ||
| return false; | ||
| } | ||
|
|
||
| } else { | ||
| count = 0; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package week2.튜플.Vryez11; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Solution { | ||
|
|
||
| /** | ||
| * | ||
| * [프로그래머스] 튜플 | ||
| * <p> | ||
| * 문제 난이도: Lv. 2 | ||
| * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/64065 | ||
| * 풀이 시간: 10분 | ||
| * 풀이 근거: 배열의 크기 정렬 | ||
| */ | ||
|
|
||
| public int[] solution(String s) { | ||
|
|
||
| s = s.substring(2, s.length() - 2); | ||
|
|
||
| String[] arr = s.split("\\},\\{"); | ||
|
|
||
| Arrays.sort(arr, (a, b) -> Integer.compare(a.length(), b.length())); | ||
|
|
||
| List<Integer> answer = new ArrayList<>(); | ||
| Set<Integer> set = new HashSet<>(); | ||
|
|
||
| for (String str : arr) { | ||
| String[] nums = str.split(","); | ||
|
|
||
| for (String num : nums) { | ||
|
|
||
| int value = Integer.parseInt(num); | ||
|
|
||
| if(!set.contains(value)) { | ||
| set.add(value); | ||
| answer.add(value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return answer.stream() | ||
| .mapToInt(i -> i) | ||
| .toArray(); | ||
|
Comment on lines
+42
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 배워갑니다 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다 !! 👍 |
||
| } | ||
| } | ||
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.
👍
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.
👍