탐색
가이드
@junghyun
전체 보기
프로젝트
포스트
팔로잉
스크랩
전체 보기
프로젝트
포스트
팔로잉
스크랩
프로젝트 히스토리
프로젝트 상세 페이지
타임라인
리스트
2022.03.28
@junghyun님이 새 포스트를 작성했습니다.
오늘의 문제: 미로 찾기 or 목표 문제 풀기
class Queue { private int MAX_QUEUE_SIZE = 10; private int queue[]; private int head; private int tail; public Queue() { head = -1; tail = -1; queue = new int[MAX_QUEUE_SIZE]; } boolean is_empty() { if (head == tail) { return true; } return false; } boolean is_full() { if ((tail + 1) % MAX_QUEUE_SIZE == head) { return true; } return false; } void enqueue(int item) { if (this.is_full()) { System.out.println("큐에 더이상 데이터를 넣을 수 없습니다.\n"); return ; } tail = (tail + 1) % MAX_QUEUE_SIZE; queue[tail] = item; } int dequeue() { if (is_empty()) { System.out.println("큐가 비어있습니다. \n"); return -1; } head = (head + 1) % MAX_QUEUE_SIZE; return queue[head]; } } class Graph { int node_count = 7; int[] visited = {0, 0, 0, 0, 0, 0, 0};; int[][] graph = { {0, 1, 0, 1, 0, 0, 0}, {1, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, }; } public class Main { public static void BFS(Graph graph, int node) { Queue queue = new Queue(); graph.visited[node] = 1; System.out.printf("%d ", node); queue.enqueue(node); while (!queue.is_empty()) { int v = queue.dequeue(); for (int w = 0; w < graph.node_count; w++) { if (graph.graph[v][w] == 1 && graph.visited[w] == 0) { graph.visited[w] = 1; System.out.printf("%d ", w); queue.enqueue(w); } } } } public static void main(String[] args) { Graph graph = new Graph(); BFS(graph, 0); } }
2022.03.25
@junghyun님이 새 포스트를 작성했습니다.
✅ 오늘의 문제: 데이터 삽입 구현하기
void insert(int nn) { int i, j, k = 0; if (n==MAX) { printf("꽉 찼음\n"); return; } for (i = 0; i < n; i++) { if (list[i] == nn) { printf("같은 숫자임\n"); return; } } switch(n) { case 0: list[0] = nn; n++; return; case 1: if (nn < list[0]) { list[1] = list[0]; list[0] = nn; n++; return; } else { list[1] = nn; n++; return; } default : for (i = 0; i < n; i++) { if (list[i] < nn && nn < list[i + 1]) { for (j = n; j > i + 1; j--) { list[j] = list[j - 1]; } list[i + 1] = nn; n++; return; } else if (num2 < list[i]) { for (j = n; j >= i + 1; j--) { list[j] = list[j - 1]; } list[i] = num2; n++; return; } } if (nn > list[n - 1]) { list[n] = nn; n++; return; } break; } } sbu07043 님의 코드를 참고하였습니다!
2022.03.24
@junghyun님이 새 포스트를 작성했습니다.
우선순위 큐
class Heap { static int MAX_HEAP_SIZE = 101; int[] arr; int heap_count = 0; public Heap(){ this.arr = new int[MAX_HEAP_SIZE]; } private boolean compare_with_parent(int index) { if (index <= 1) return false; int parent_index = index / 2; if (this.arr[index] < this.arr[parent_index]) return true; else return false; } public boolean insert(int data) { int insert_index, parent, tmp; this.heap_count++; if (this.heap_count == 1) { this.arr[this.heap_count] = data; return true; } this.arr[this.heap_count] = data; insert_index = this.heap_count; while (this.compare_with_parent(insert_index)) { parent = insert_index / 2; tmp = this.arr[insert_index]; this.arr[insert_index] = this.arr[parent]; this.arr[parent] = tmp; insert_index = parent; } return true; } private int compare_with_child(int index) { int left, right; left = 2 * index; right = 2 * index + 1; if (left >= this.heap_count && right >= this.heap_count) { return 0; } if (right >= this.heap_count) if (this.arr[left] < this.arr[index]) return left; if (this.arr[left] < this.arr[right]) return left; else return right; } public int pop() { int index = 1, child_index, root, terminal_data, tmp; root = this.arr[1]; terminal_data = this.arr[this.heap_count]; this.arr[this.heap_count] = -1; this.arr[1] = terminal_data; this.heap_count--; while (true) { child_index = compare_with_child(index); if (child_index == 0) break; tmp = this.arr[child_index]; this.arr[child_index] = this.arr[index]; this.arr[index] = tmp; index = child_index; } return root; } } public class Main { public static void main(String[] args) { Heap heap = new Heap(); heap.insert(1); heap.insert(3); heap.insert(4); heap.insert(5); heap.insert(6); heap.insert(7); heap.insert(8); heap.insert(9); heap.insert(10); heap.insert(11); System.out.println(heap.pop()); for (int i = 1; i < heap.heap_count+1; i++) { System.out.print(heap.arr[i] + " "); } System.out.println(); System.out.println(heap.pop()); for (int i = 1; i < heap.heap_count+1; i++) { System.out.print(heap.arr[i] + " "); } } }
2022.03.23
@junghyun님이 새 포스트를 작성했습니다.
12-4. QUIZ : 전위 순회를 트리로
class Node { int data; Node left; Node right; public Node(int data) { this.data = data; left = null; right = null; } } class BinarySearchTree { Node root; public void setRoot(int data) { this.root = new Node(data); } public int find_node_by_recursion(Node current_node, int data) { if(current_node == null) return 0; if(data < current_node.data) return find_node_by_recursion(current_node.left, data); else if(data > current_node.data) return find_node_by_recursion(current_node.right, data); else return 1; } int find(int data) { if(find_node_by_recursion(root, data) == 0) return 0; else return 1; } void insert_node(Node current_node, int data) { if(data == current_node.data) { System.out.printf("이미 %d값이 존재합니다. 중복된 값은 삽입할 수 없습니다.\n", data); return; } if (data < current_node.data) { if (current_node.left == null) { current_node.left = new Node(data); } else { insert_node(current_node.left, data); } } else if (data > current_node.data) { if (current_node.right == null) { current_node.right = new Node(data); } else { insert_node(current_node.right, data); } } } void insert(int data) { if(root == null) { setRoot(data); } else { insert_node(root, data); } } Node get_next_node(Node node) { while (node.left != null) { node = node.left; } return node; } void delete_node(Node parent, Node current_node, int data) { if (current_node == null) { System.out.printf("트리에 %d가 존재하지 않습니다. \n", data); return; } if (data < current_node.data) delete_node(current_node, current_node.left, data); else if (data > current_node.data) delete_node(current_node, current_node.right, data); else { if (current_node.left == null && current_node.right == null) { if (data < parent.data) parent.left = null; else parent.right = null; } else if (current_node.left != null && current_node.right == null) { if (data < parent.data) parent.left = current_node.left; else parent.right = current_node.left; } else if (current_node.left == null && current_node.right != null) { if (data < parent.data) parent.left = current_node.right; else parent.right = current_node.right; } else if (current_node.left != null && current_node.right != null) { Node next_node = get_next_node(current_node.right); current_node.data = next_node.data; delete_node(current_node, current_node.right, next_node.data); } } } void delete(int data) { if (root == null) System.out.printf("트리에 노드가 존재하지 않습니다."); else delete_node(null, root, data); } } // 데이터 탐색, 삽입, 제거 public class BinarySearchTree1 { static void in_order(Node node) { if (node == null) { return; } in_order(node.left); System.out.printf("%d ", node.data); in_order(node.right); return; } public static void main(String[] args) { BinarySearchTree BST = new BinarySearchTree(); BST.setRoot(7); BST.insert(3); BST.insert(1); BST.insert(5); BST.insert(10); BST.insert(8); BST.insert(4); BST.insert(12); BST.insert(15); BST.delete(7); in_order(BST.root); } }
2022.03.22
@junghyun님이 새 포스트를 작성했습니다.
#11-3. QUIZ ✅ 오늘의 문제: 트리의 높이 구하기
class Node { int data; Node left; Node right; public Node(int data) { this.data = data; left = null; right = null; } } public class Main { public static Node initTree() { Node root = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); root.left = node2; root.right = node3; Node node4 = new Node(4); Node node5 = new Node(5); node2.left = node4; node2.right = node5; return root; } public static void pre_order(Node node) { if (node == null) { return; } System.out.printf("%d ", node.data); pre_order(node.left); pre_order(node.right); } public static void in_order(Node node) { if (node == null) { return; } in_order(node.left); System.out.printf("%d ", node.data); in_order(node.right); } public static void post_order(Node node) { if (node == null) { return; } post_order(node.left); post_order(node.right); System.out.printf("%d ", node.data); } public static void main(String[] args) { Node root = initTree(); // pre_order(root); // in_order(root); post_order(root); } }
2022.03.21
@junghyun님이 새 포스트를 작성했습니다.
#10-2. QUIZ ✅ 오늘의 문제: 이중 링크드 리스트
class Node { public Object data; public Node pointer; public Node(Object input) { this.data = input; this.pointer = null; } } class LinkedList { private Node head = new Node(null); public Node get(int index) { Node node = this.head.pointer; while (true) { if (index == 0) return node; if (node == null) { System.out.println("Index Error"); return null; } node = node.pointer; index--; } } public void append(int data) { Node node = head; Node newNode = new Node(data); while (true) { if (node.pointer == null) break; node = node.pointer; } node.pointer = newNode; } public void insert(int data, int index) { Node node = head; Node newNode = new Node(data); while (true) { if (node == null) { System.out.println("Index error"); return; } if (index == 0) { Node nextNode = node.pointer; node.pointer = newNode; newNode.pointer = nextNode; break; } node = node.pointer; index--; } } void delete(int index) { if (index == 0) { head.pointer = head.pointer.pointer; } else { // get함수를 사용해, 인덱스 위치의 데이터를 얻어온다 Node node = get(index - 1); node.pointer = node.pointer.pointer; } } void print_linked_list() { Node node = head.pointer; while (node != null) { if (node.pointer != null) System.out.print(node.data + " -> "); else System.out.println(node.data); node = node.pointer; } } } public class Main { public static void main(String[] args) { LinkedList LL = new LinkedList(); LL.append(1); LL.append(2); LL.append(3); LL.print_linked_list(); LL.insert(10, 0); LL.print_linked_list(); LL.insert(20, 3); LL.print_linked_list(); LL.delete(1); LL.print_linked_list(); LL.delete(0); LL.print_linked_list(); } }
2022.03.18
@junghyun님이 새 포스트를 작성했습니다.
#9-3. QUIZ ✅ 오늘의 문제: 고객 응대 시뮬레이터
class Queue { private int MAX_QUEUE_SIZE = 5; private int queue[]; private int head; private int tail; public Queue() { head = -1; tail = -1; queue = new int[MAX_QUEUE_SIZE]; } boolean is_empty() { if (head == tail) { return true; } return false; } boolean is_full() { if ((tail + 1) % MAX_QUEUE_SIZE == head) { return true; } return false; } void enqueue(int item) { if (this.is_full()) { System.out.println("큐에 더이상 데이터를 넣을 수 없습니다.\n"); return ; } tail = (tail + 1) % MAX_QUEUE_SIZE; queue[tail] = item; } int dequeue() { if (is_empty()) { System.out.println("큐가 비어있습니다. \n"); return -1; } head = (head + 1) % MAX_QUEUE_SIZE; return queue[head]; } } public class Main { public static void main(String[] args) { Queue queue = new Queue(); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); queue.enqueue(4); queue.enqueue(5); queue.enqueue(6); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); System.out.println("pop :" + queue.dequeue()); } }
2022.03.17
@junghyun님이 새 포스트를 작성했습니다.
8-2. QUIZ: 도핑제 만들기
투포인터만 구현 완료했으며 슬라이딩 윈도우 구현 후 코드 바꿔두겠습니다. import java.util.Scanner; public class TwoPointer { public static void main(String[] args) { int N, arr[], M; // 수의 갯수, 배열, 구해야하는 수 int start=0, end=0, partial_sum=0, answer=0; // 수 입력받기 Scanner sc = new Scanner(System.in); N = sc.nextInt(); arr = new int [N]; for(int i=0; i<N; i++) { arr[i] = sc.nextInt(); } M = sc.nextInt(); while(start < N){ // >M이 먼저인 이유 : if조건을 건너뛰고 elif 가 먼저 실행되어야해서? if(partial_sum > M || end >= N) partial_sum -= arr[start++]; else if(partial_sum <= M) partial_sum += arr[end++]; if (partial_sum == M) answer++; } System.out.println(answer); sc.close(); } }
2022.03.16
@junghyun님이 새 포스트를 작성했습니다.
#7-3. QUIZ
package Study.CS_Study.DataStructure; public class SieveOfEratosthenes { static int getPrimeList(int n) { int range = n+1; int sqrt = (int)(Math.sqrt(n)) + 1; int cnt = 0; boolean[] primeList = new boolean[range]; for (int i = 0; i < range; i++) primeList[i] = true; for (int i = 2; i < sqrt; i++) { if (primeList[i]) { for (int j = i * i; j < range; j += i) primeList[j] = false; } } for (int i = 2; i < range; i++){ if (primeList[i]) cnt ++; } return cnt; } public static void main(String[] args) { int prime_count = getPrimeList(50000); System.out.println(prime_count); } }
2022.03.15
@junghyun님이 포스트를 업데이트했습니다.
포스트
괄호
2022.03.15
@junghyun님이 새 포스트를 작성했습니다.
괄호
.
2022.03.14
@junghyun님이 새 포스트를 작성했습니다.
5-4) 오늘의 문제: 선택 정렬
public class SelectionSort { public static void main(String[] args) { int [] arr = {9, 6, 7, 3, 5}; int tmp; // 교환용 변수 int min; // 최솟값 저장용 변수 for(int i=0; i<arr.length - 1; i++) { // 가장 앞의 원소를 최솟값으로 설정 min = i; // 탐색을 통해 가장 최솟값 찾기 for(int j=i+1; j<arr.length; j++) { // j는 i보다 1 큰 수 이어야 하기 때문 if(arr[min] > arr[j]) { min = j; } } // 탐색이 완료되면 최솟값과 가장 앞의 원소의 자리를 바꿈. tmp = arr[min]; arr[min] = arr[i]; arr[i] = tmp; } System.out.println("정렬 완료✌️"); for(int i=0; i<arr.length; i++) { System.out.println(arr[i]); } } }
2022.03.11
@junghyun님이 새 포스트를 작성했습니다.
4-2) 오늘의 문제: 이분 탐색 재귀 변환
java public class Binary { public static void main(String[] args) { int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int target = 5; int result = BinarySearch(arr, 0, arr.length-1, target); System.out.println(result); } private static int BinarySearch(int[] arr, int start, int end, int target) { if (start>end) { return end+1; // 중앙값 설정 } int mid = (start+end)/2; if(target > arr[mid]) return BinarySearch(arr, mid+1, end, target); else return BinarySearch(arr, start, mid-1, target); } }
2022.03.09
@junghyun님이 새 포스트를 작성했습니다.
3-2. QUIZ: 이분 탐색 응용
public class BinarySearch01 { public static void main(String[] args) { int[] lan = {215, 513, 712, 803}; int target = 10; int min = 0; int max = lan[lan.length-1]; //최댓값 int result = 0; while(max >= min) { // 최댓값일 때까지 int sum = 0; int mid = (min + max) / 2; // 중간 값 for (int i : lan) { sum += i / mid; } if (sum > target) { min = mid + 1; } else if (sum < target) { max = mid - 1; } else { result = mid; break; } } // while문 종료 System.out.println(result); } }
2022.03.08
@junghyun님이 새 포스트를 작성했습니다.
2-2) ✅ 오늘의 문제: 시간 복잡도 구하기
1번 : O(n) 두 반복문 모두 입력되는 값 n, m의 값의 크기와 비례한 결과값을 보여주기 때문. 2번 : O(n^2) 첫번째 반복문에서 O(n^2)의 시간복잡도를 가지고, 두번째에서는 O(n)의 시간복잡도를 가지지만, 둘의 더했을 때 상수항은 생각되기때문에 O(n^2)만 남음.
2022.03.04
@junghyun님이 새 포스트를 작성했습니다.
1-2) 오늘의 문제: 목표 설정
사용언어 : JAVA 환경 : vscode 목표 문제 : https://www.acmicpc.net/problem/1260