반응형

개발/코딩테스트 28

[Beakjoon] 1978. 소수 찾기 #B2 #Java

문제주어진 수 N개 중에서 소수가 몇 개인지 찾아서 출력하는 프로그램을 작성하시오.예제 입력 141 3 5 7예제 출력 13제약조건✅ 첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.✅ 주어진 수들 중 소수의 개수를 출력한다. 문제풀이public class Main { public static void main(String[] args) throws IOException { // 1. 변수 선언 및 초기화 int maxValue = 1_000; int[] primeArr = new int[maxValue + 1]; BufferedReader br = new BufferedRe..

[Baekjoon] N과 M (1 ~ 12) #BackTracking

그동안 너무 쉬운 문제만 풀어와서 백트래킹을 까먹었다..백트래킹을 이해하기 좋은 N과 M 문제들을 모두 풀어보았다. N과 M (1)https://www.acmicpc.net/problem/15649 문제1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열 문제풀이public class Main { // 전역 변수 static int n, m; static boolean[] visited; // 백 트래킹 처리 메서드 static void backtracking(int depth, String str) { if (depth == m) { System.out.println(str); return; } ..

[Baekjoon] 1052. 물병 #G5 #Java

문제지민이는 N개의 물병을 가지고 있다. 각 물병에는 물을 무한대로 부을 수 있다. 처음에 모든 물병에는 물이 1리터씩 들어있다. 지민이는 이 물병을 또 다른 장소로 옮기려고 한다. 지민이는 한 번에 K개의 물병을 옮길 수 있다. 하지만, 지민이는 물을 낭비하기는 싫고, 이동을 한 번보다 많이 하기는 싫다. 따라서, 지민이는 물병의 물을 적절히 재분배해서, K개를 넘지 않는 비어있지 않은 물병을 만들려고 한다.물은 다음과 같이 재분배 한다. 먼저 같은 양의 물이 들어있는 물병 두 개를 고른다. 그 다음에 한 개의 물병에 다른 한 쪽에 있는 물을 모두 붓는다. 이 방법을 필요한 만큼 계속 한다.이런 제약 때문에, N개로 K개를 넘지않는 비어있지 않은 물병을 만드는 것이 불가능할 수도 있다. 다행히도, 새로..

[LeetCode] 100. Same Tree #Easy #Java

문제Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.예제 1Input: p = [1,2,3], q = [1,2,3]Output: true예제 2Input: p = [1,2], q = [1,null,2]Output: false예제 3Input: p = [1,2,1], q = [1,1,2]Output: false제약조건✅ The number of nodes in both trees i..

[LeetCode] 94. Binary Tree Inorder Traversal #Easy #Java

문제Given the root of a binary tree, return the inorder traversal of its nodes' values. 예제 1Input: root = [1,null,2,3]Output: [1,3,2]예제 2Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]Output: [4,2,6,5,7,1,3,9,8]예제 3Input: root = []Output: []예제 4Input: root = [1]Output: [1]제약조건✅ The number of nodes in the tree is in the range [0, 100]. ✅ -100  문제풀이/** * Definition for a binary tree node. * public ..

[LeetCode] 83. Remove Duplicates from Sorted List #Easy #Java

문제Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.예제 1Input: head = [1,1,2]Output: [1,2]예제 2Input: head = [1,1,2,3,3]Output: [1,2,3]제약조건✅ The number of nodes in the list is in the range [0, 300].✅ -100 ✅ The list is guaranteed to be sorted in ascending order. 문제풀이/** * Definition for singly-linked list...

[LeetCode] 69. Sqrt(x) #Easy #Java

문제Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.예제 1 Input: x = 4Output: 2// Explanation: The square root of 4 is 2, so we return 2. 예제 2Input: x = 8Output: 2// Explana..