본문 바로가기

Algorithm118

[Algorithm / Programmers] 월간 코드 챌린지 시즌2-괄호 회전하기 [Version]⦁ 2024.04.30 / [Algorithm / Programmers] 월간 코드 챌린지 시즌2-괄호 회전하기 import java.util.*;class Solution { public int solution(String s) { int answer = 0; int total = s.length(); for(int i = 0; i stack = new Stack(); boolean isPair = false; char[] input = s.toCharArray(); for(int i = 0; i   문자열의 길이만큼 반복문을 수행하며 회전하는 문자열을 생성한다. 그 이후, S.. 2024. 4. 30.
[Algorithm / Programmers] 2017 팁스타운-예상 대진표 [Version]⦁ 2024.04.30 / [Algorithm / Programmers] 2017 팁스타운-예상 대진표 class Solution { public int solution(int n, int a, int b) { int answer = 0; while(a != b) { a = (a + 1) / 2; b = (b + 1) / 2; answer++; } answer++; return answer; }} a와 b가 몇 번의 경기 이후 대진을 진행하는지 묻는 문제였다. a와 b는 무조건 우승하는 조건이 있었기 때문에 홀수 또는 짝수와 상관.. 2024. 4. 30.
[Algorithm / Programmers] 프로세스 [Version]⦁ 2024.04.26 / [Algorithm / Programmers] 프로세스 import java.util.*;class Solution { public int solution(int[] priorities, int location) { PriorityQueue queue = new PriorityQueue(Collections.reverseOrder()); for(int i = 0; i  문제를 처음 접했을 때, 우선순위 큐를 활용하여 해결할 수 있겠다고 판단했다. 핵심 로직에 대해 간략하게 말하자면, 큐의 첫 번째 원소가 프로세스의 i번째 위치와 동일할 경우 큐에서 원소를 제거하고 위치 값을 1증가하였다. 그 과정에서 원소의 위치와 i가 동일하면 현.. 2024. 4. 26.
[Algorithm / Programmers] H-Index [Version]⦁ 2024.04.26 / [Algorithm / Programmers] H-Index import java.util.*;class Solution { public int solution(int[] citations) { // 배열을 오름차순으로 정렬합니다. Arrays.sort(citations); // h-index 값을 저장할 변수를 초기화합니다. int hIndex = Integer.MIN_VALUE; // 각 논문에 대해 반복하면서 h-index를 계산합니다. for(int i = 0; i 문제를 처음 접했을 때, H-Index의 정의를 이해하는데 시간이 걸렸다. H-In.. 2024. 4. 26.
[Algorithm / Programmers] 카펫 [Version] ⦁ 2024.04.23 / [Algorithm / Programmers] 카펫 class Solution { public int[] solution(int brown, int yellow) { int totalTile = brown + yellow; // yellow가 존재하기 위해서는 가로와 세로의 길이가 3이상이여야 한다. for(int i = 3; i = col) { if((row - 2) * (col - 2) .. 2024. 4. 23.
[그리디 알고리즘] 크루스칼 알고리즘(Kruskal Algorithm) [Update] - 2024.04.11: [그리디 알고리즘] 크루스칼 알고리즘(Kruskal Algorithm) 크루스칼 알고리즘이란? 크루스칼 알고르짐은 주어진 그래프에서 모든 정점을 최소 비용으로 연결하는 최소 신장 트리를 찾는 데 사용된다. 그래프의 간선은 가중치가 할당되어 있으며 이 가중치의 합이 최소가 되도록 하면서 모든 정점을 연결하는 것을 목표로 하는 알고리즘이다. 크루스칼 알고리즘을 학습하기 전에, 신장 트리와 최소 신장트리에 대해 알아보자. 신장 트리와 최소 신장 트리 신장 트리란 그래프에서 모든 정점을 포함하여, 정점들 사이를 연결하되 사이클이 존재하지 않는 구조를 말한다. 따라서 정점의 개수가 N이라면, 간선의 개수는 N-1이 되어야 한다. 최소 신장 트리는 간선들의 가중치 합이 최소.. 2024. 4. 11.
[Algorithm / Programmers] 2022 KAKAO BLIND RECRUITMENT-k진수에서 소수 개수 구하기 [Version] ⦁ 2024.04.08 / [Algorithm / Programmers] 2022 KAKAO BLIND RECRUITMENT-k진수에서 소수 개수 구하기 import java.util.*; class Solution { public boolean isPrime(long n) { if (n 2024. 4. 8.
[Algorithm / Programmers] 완전탐색-피로도 [Version] ⦁ 2024.04.08 / [Algorithm / Programmers] 완전탐색-피로도 class Solution { public static int maxEntree = Integer.MIN_VALUE; public int solution(int k, int[][] dungeons) { boolean[] visited = new boolean[dungeons.length]; DFS(k, dungeons, visited, 0); return maxEntree; } public static void DFS(int k, int[][] dungeons, boolean[] visited, int depth) { if(depth > maxEntree && depth != 0) { maxEntr.. 2024. 4. 8.
[Algorithm / Programmers] 2019 카카오 개발자 겨울 인턴십-튜플 [Version] ⦁ 2024.04.08 / [Algorithm / Programmers] 2019 카카오 개발자 겨울 인턴십-튜플 import java.util.*; class Solution { public int[] solution(String s) { // 1. 괄호 제거 s = s.replace("},{", "/"); s = s.replace("{{", ""); s = s.replace("}}", ""); // 2. 각 튜플 등장 횟수 저장 맵 Map frequencyMap = new HashMap(); // 3. "/"를 구분자로 나눔 String[] tuples = s.split("/"); for(int i = 0; i < tuples.length; i++) { String[] element.. 2024. 4. 8.