개발하는 쿠키
article thumbnail
Java, LeetCode | 212. Word Search 2
Coding/algorithm 2024. 9. 7. 19:40

🚀 풀이 후기Trie 문제를 처음 풀어봤습니다.Trie의 원리만 알고 있다면 쉽게 풀 수 있을 것 같습니다.class Trie { public Trie[] trie; public boolean flag; public Trie() { trie = new Trie[26]; flag = false; } public void insert(String word) { Trie node = this; for (char ch : word.toCharArray()) { int index = ch - 'a'; if (node.trie[index] == null) { node.tr..

article thumbnail
Java, LeetCode | 1136. Parallel Courses
Coding/algorithm 2024. 9. 7. 00:38

🚀 풀이 후기이 문제를 풀다 보니 그래프에 순환이 있는지 파악하는 방법을 배우게 됐습니다.선행 조건이 없는 노드들을 모두 제거한 후, 제거하지 못한 노드들이 있다면 순환 그래프입니다.🌒 문제 설명선행수업을 들어야 다음 수업을 들을 수 있습니다.한 학기에는 여러 수업을 수강할 수 있습니다.수업을 다 들을 수 있는 최소 학기 수를 반환하는 문제입니다.만약 수업을 모두 들을 수 없다면 -1을 반환합니다.You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCours..

article thumbnail
Java, LeetCode | 418. Sentence Screen Fitting
Coding/algorithm 2024. 9. 5. 04:44

🚀 풀이 후기DP.. 너무 어렵습니다.string에서 index와 size가 1이 차이나다 보니 더 헷갈립니다. 🌒 문제 설명주어진 sentence 문자열 배열이 rows * cols 의 스크린에 몇 번 채워질 수 있는지 반환하는 문제입니다.문자열 배열의 순서는 바꿀 수 없고, 각 문자는 2줄로 쪼개져서도 안됩니다.같은 줄에서 각각의 문자는 구분되어야 합니다.Given a rows x cols screen and a sentence represented as a list of strings, return the number of times the given sentence can be fitted on the screen.The order of words in the sentence must rema..

article thumbnail
Java, BaekJoon | 30802. 웰컴 키트
Coding/algorithm 2024. 8. 3. 18:11

🚀 풀이 후기StringTokenizer 까먹어서 다시 공부했습니다..BufferedReader에서 띄어쓰기로 구분된 값을 각 변수 혹은 배열에 저장하기 위해서는 StringTokenizer를 사용해야 합니다.하지만, 저장할 배열의 자료형이 String이거나 Character라면 더 간단한 방법을 사용할 수 있습니다.char charInput[] = br.readLine().toCharArray();String stringInput[] = br.readLine().split(" ");🌒 문제 설명🌓 문제 풀이티셔츠는 남아도 되지만 부족해서는 안되므로 (사이즈를 선택한 사람 수 / 티셔츠 묶음 수)를 했을 때 나머지가 있다면 한 묶음 더 주문해야 합니다.펜은 정확히 참가자 수만큼 준비되어야 하므로 (..

article thumbnail
Java, LeetCode | 366. Find Leaves of Binary Tree
Coding/algorithm 2024. 8. 3. 16:55

🚀 풀이 후기Leaf 노드만 찾아서 삭제하는 부분이 까다로웠습니다.Leaf 노드를 삭제하니 부모 노드도 Leaf 노드라고 착각하게 되는 것을 주의하면서 코드를 짜야합니다.🌒 문제 설명Given the root of a binary tree, collect a tree's nodes as if you were doing this:Collect all the leaf nodes.Remove all the leaf nodes.Repeat until the tree is empty. Example 1:Input: root = [1,2,3,4,5]Output: [[4,5,3],[2],[1]]Explanation:[[3,5,4],[2],[1]] and [[3,4,5],[2],[1]] are also consid..

article thumbnail
Java, BaekJoon | 25083. 새싹
Coding/algorithm 2024. 7. 23. 23:57

🚀 풀이 후기자바 String 자료형에 특수문자를 쓸 때는 특정 특수문자는 "역슬래쉬" 를 앞에 넣어주면 됩니다.String 특수문자 표현의미\t탭\n줄 바꿈\'작은 따옴표(')\"큰 따옴표(")\\역슬래쉬(\)~!@#$%^&*{}[]()기타 특수문자🌒 문제 설명🌓 문제 풀이public class Main { public static void main(String[] args){ print(" ,r\'\"7"); print("r`-_ ,\' ,/"); print(" \\. \". L_r'"); print(" `~\\/"); print(" |"); print(" |"); ..

article thumbnail
Java, LeetCode | 35. Search Insert Position
Coding/algorithm 2024. 7. 23. 23:48

🚀 풀이 후기이진 탐색 문제를 오랜만에 푸니까 요령이 생겼습니다.항상 헷갈렸던 부분이 else if 구문에서 어느 조건에 answer 답변을 세팅해줘야 하는지였습니다.아래 문제에서 Example 2를 보면 nums[i]  일 때 answer = i+1 을 넣어주므로, 코드를 짤 때, target  nums[i] 인 조건에서 answer = i 를 세팅해주면 됩니다. 🌒 문제 설명Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must..

article thumbnail
Java, BaekJoon | 27866. 문자와 문자열
Coding/algorithm 2024. 7. 23. 01:07

🚀 풀이 후기자바에서 String의 특정 index 에 있는 문자를 가져올 때 쓰는 함수는 `string.charAt(index)` 입니다.🌒 문제 설명🌓 문제 풀이import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); ..

반응형