코딩테스트
BFS(Breadth-First Search) 너비 우선 탐색
2025.05.30
BFS(Breadth-First Search, 너비 우선 탐색)는 그래프나 트리에서 시작 노드로부터 가까운 노드부터 탐색하는 알고리즘 ✅ 핵심 개념가까운 노드부터 탐색Queue(큐) 자료구조 사용최단 거리 탐색에 적합그래프의 레벨 순서 탐색 가능📌 예시로 이해하기 1 / \ 2 3 / \ \4 5 6✅ BFS 동작 과정 (요약)시작 노드를 큐에 삽입하고 방문 표시큐에서 노드를 꺼내서 인접한 노드들을 모두 큐에 추가추가할 때 방문하지 않은 노드만 추가큐가 빌 때까지 2~3 반복✅ Java 코드import java.util.*;public class BFSExample { public static void main(String[] args) { /..
코딩테스트
DFS(Depth-First Search) 깊이 우선 탐색
2025.05.30
DFS(Depth-First Search, 깊이 우선 탐색)는 그래프나 트리에서 한 방향으로 끝까지 탐색한 후, 더 이상 갈 곳이 없으면 다시 돌아와서 다른 방향을 탐색하는 알고리즘✅ 핵심 개념시작 노드에서 한 방향으로 쭉 내려감더 이상 갈 곳이 없으면 백트래킹(되돌아가기)스택(stack) 또는 재귀(recursion) 를 사용해 구현✅ 동작 방식 A├── B│ └── D└── C ✅ DFS 구현 방법import java.util.*;public class DFSExample { public static void dfs(Map> graph, String start) { Set visited = new HashSet(); Stack stack = new Stack(); ..
코딩테스트
백준 - 9081 번 단어 맞추기
2025.04.12
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main_9081_단어맞추기 { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testCase = Integer.parseInt(br.readLine()); for(int i=1; i 기준점(인덱스) 찾기 // arr[i]=0 && array[i] >= array[i+1] ) { //i>=0 : 반복문에서 배열의 인덱스를 벗어나는 참조..
코딩테스트
SORT 정리 / ArrayTesr
2025.04.12
예제 - 1 import java.util.Arrays;class Person implements Comparable { String name; int age; String job; int score; public Person(String name, int age, String job, int score) { this.name = name; this.age = age; this.job = job; this.score = score; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", job=" + job + ", score=" + score + "]\n"; } @Override..
코딩테스트
NextPermutation
2025.04.12
import java.util.Arrays;public class NextPermutationTest { public void nextPermutation(int[] nums) { int i = nums.length - 2; // Step 1: a= 0 && nums[i] >= nums[i + 1]) { i--; } // Step 2: i번지의 값보다 큰값을 뒤에서 부터 찾기!! ==>찾았으면 swap if (i >= 0) { int j = nums.length - 1; while (nums[i] >= nums[j]) { j--; ..