본문 바로가기
반응형

코딩테스트 준비211

C# - BFS(너비우선탐색)개념 및 코드 구현 너비우선탐색(BFS, Breadth-First Search) 시작 노드로부터 인접한 노드를 먼저 모두 방문한 후 그 인접한 노드들의 근처 노드들을 차례대로 방문하는 방식으로 동작한다. DFS같은 경우 많은 곳에 사용되지만 BFS같은 경우 최단거리에 많이 사용된다. 1. 행렬 01 그래프 행렬로 표현 int[,] adj = new int[10, 10] { {0,1,0,0,0,0,0,1,0,0 }, {1,0,1,0,1,0,0,0,0,0 }, {0,1,0,1,0,0,0,0,0,0 }, {0,0,1,0,0,0,0,0,0,0 }, {0,1,0,0,0,1,1,1,0,0 }, {0,0,0,0,1,0,0,0,0,0 }, {0,0,0,0,1,0,0,0,0,0 }, {1,0,0,0,1,0,0,0,1,0 }, {0,0,0,.. 2023. 9. 19.
백준 C# - 9012 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace baek2 { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); for (int i = 0; i < num; i++) { string answer = "YES"; string s = Console.ReadLine(); int balance = 0; for (int j = 0; j < s.Length; j++) { if (s[j] == '(') { balance++; } else { balance--; } if (balance.. 2023. 9. 19.
백준 C# - 9098 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace baek2 { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); for (int i = 0; i 2023. 9. 17.
프로그래머스 C# - 이진 변환 반복하기 많이 풀어본 이진법 문제가 나와서 나름 금방 풀었던 것 같다. 반복문 2개를 써도 되나 고민하면서 풀었는데 통과되어서 다행이다! using System; using System.Linq; using System.Collections.Generic; public class Solution { public int[] solution(string s) { int[] answer = new int[2]; int count_0 = 0; int count = 0; while(s!= "1") { count++; List s_string = new List(s); count_0 += s_string.Count(n => n=='0'); string a = s.Replace("0",""); int a_len = a.Len.. 2023. 9. 12.
프로그래머스 C# - 최솟값 만들기 풀이 이것저것 끄적이다가 A는 작은 순서대로 B는 큰 순서대로 앞에서 부터 곱하고 더하면 그 결과가 답인 것을 알게 되었다. 1. A와 B Sort 2. B Reverse 3. 앞에서부터 각각 곱하고 다 더하면 정답이 나온다. public class Solution { public int solution(int[] A, int[] B) { int answer = 0; Array.Sort(A); Array.Sort(B); Array.Reverse(B); for(int i=0;i 2023. 9. 6.
C# - DFS(깊이우선탐색)개념 및 코드 구현 깊이 우선 탐색(DFS, Depth-First Search) 특정 노드에서 시작하여 그래프를 탐색하면서 제일 깊은 곳으로 들어갔다가 더 이상 들어갈 곳이 없게 되면 다시 돌아와 다른 경로로 탐색을 진행한다. 이를 통해 모든 노드를 방문하고자 할때 사용할 수 있는 탐색 알고리즘이다. 1. 행렬 01 그래프 행렬로 표현 int[,] adj = new int[10, 10] { {0,1,0,0,0,0,0,1,0,0 }, {1,0,1,0,1,0,0,0,0,0 }, {0,1,0,1,0,0,0,0,0,0 }, {0,0,1,0,0,0,0,0,0,0 }, {0,1,0,0,0,1,1,1,0,0 }, {0,0,0,0,1,0,0,0,0,0 }, {0,0,0,0,1,0,0,0,0,0 }, {1,0,0,0,1,0,0,0,1,0 .. 2023. 9. 6.
반응형