[LeetCode] Grind 75 questions (14/75) First Bad Version

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */ public class Solution extends VersionControl { public int firstBadVersion(int n) { int left = 1; int right = n; while(left < right) { int mid = left + (right - left) / 2; // 정수 오버플로를 방지하면서 중간값 계산 if(isBadVersion(mid)) right = mid; // mid가 불량이면 그 이전에도 불량이 있을 수 있으므로 right를 mid로 설정 else left = mid + 1; // mid가 불량이 아니라면 그 이후에 불량이 있으므로 left를 mid + 1로 설정 } return left; // left가 right와 같아지는 시점에 첫 번째 불량품을 찾는다. } }

2024년 7월 10일 · 김태영

[LeetCode] Grind 75 questions (15/75) Ransom Note

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public boolean canConstruct(String ransomNote, String magazine) { Map<Character, Integer> magazineMap = new HashMap(); for(char c : magazine.toCharArray()) { magazineMap.put(c, magazineMap.getOrDefault(c, 0) + 1); } for(char c : ransomNote.toCharArray()) { magazineMap.put(c, magazineMap.getOrDefault(c, 0) - 1); } for(int i : magazineMap.values()) { if(i < 0) return false; } return true; } } ...

2024년 7월 10일 · 김태영

[LeetCode] Grind 75 questions (11/75) Balanced Binary Tree

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isBalanced(TreeNode root) { if(root == null) return true; if(Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) return false; return isBalanced(root.left) && isBalanced(root.right); } private int getHeight(TreeNode node) { if(node == null) return 1; return Math.max(getHeight(node.left), getHeight(node.right)) + 1; } }

2024년 7월 8일 · 김태영

[LeetCode] Grind 75 questions (12/75) Linked List Cycle

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head == null || head.next == null) return false; ListNode slow = head; ListNode fast = head.next; while(slow != fast) { if(fast == null || fast.next == null) return false; slow = slow.next; fast = fast.next.next; } return true; } } ...

2024년 7월 8일 · 김태영

[LeetCode] Grind 75 questions (13/75) Implement Queue using Stacks

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 class MyQueue { private Stack<Integer> stack1; private Stack<Integer> stack2; public MyQueue() { stack1 = new Stack<>(); stack2 = new Stack<>(); } public void push(int x) { stack1.push(x); } public int pop() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } public int peek() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.peek(); } public boolean empty() { return stack1.isEmpty() && stack2.isEmpty(); } } /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */ ...

2024년 7월 8일 · 김태영

[LeetCode] Grind 75 questions (10/75) Lowest Common Ancestor of a Binary Search Tree

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val > p.val && root.val > q.val) { // root보다 값이 작을 경우 return lowestCommonAncestor(root.left, p, q); } else if(root.val < p.val && root.val < q.val) { // root보다 값이 클 경우 return lowestCommonAncestor(root.right, p, q); } else { // TreeNode p와 q가 같은 노드에 없다면 root가 부모다 return root; } } }

2024년 7월 6일 · 김태영

월급쟁이끼리 사정 아시잖아요 — 말도 안 되는 일정

같은 월급쟁이들끼리… 사정 아시잖아요 최근 우리팀에게 프로젝트 제안을 보내는 현업팀에서 자주 하는 멘트다. 6월 중순까지도 협의되지 않은 요건이었지만 ‘가능하다면’ 7월 초 테스트 오픈이었다. ...

2024년 7월 6일 · 김태영

[LeetCode] Grind 75 questions (7/75) Valid Anagram

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 class Solution { public boolean isAnagram(String s, String t) { char[] c1 = s.toCharArray(); char[] c2 = t.toCharArray(); Arrays.sort(c1); Arrays.sort(c2); return new String(c1).equals(new String(c2)); } } 풀이2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } Map<Character, Integer> count = new HashMap<>(); for(char c : s.toCharArray()) { count.put(c, count.getOrDefault(c, 0) + 1); } for(char c : t.toCharArray()) { count.put(c, count.getOrDefault(c, 0) -1); } for(int n : count.values()) { if(n != 0) return false; } return true; } } ...

2024년 7월 2일 · 김태영

[LeetCode] Grind 75 questions (8/75) Binary Search

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length-1; while(left <= right) { int mid = left + (right-left) / 2; if(nums[mid] == target) return mid; else if(nums[mid] > target) { right = mid - 1; } else { left = mid + 1; } } return -1; } } ...

2024년 7월 2일 · 김태영

[LeetCode] Grind 75 questions (9/75) Flood Fill

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public int[][] floodFill(int[][] image, int sr, int sc, int color) { int startColor = image[sr][sc]; // 1. 시작 색상 저장 if (startColor == color) return image; // 기저 조건: 색상이 같으면 변경 불필요 fill(image, sr, sc, startColor, color); // 4. 재귀 호출로 플러드 필 시작 return image; } private void fill(int[][] image, int sr, int sc, int startColor, int newColor) { // 2. 기저 조건 설정: 범위를 벗어나거나 현재 픽셀의 색상이 시작 색상과 다르면 재귀 호출을 종료합니다. if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != startColor) { return; } // 3. 색상 변경 image[sr][sc] = newColor; // 4. 재귀 호출: 4방향(상하좌우)으로 인접한 픽셀에 대해 재귀적으로 fill 메서드를 호출하여 동일한 작업을 반복합니다. fill(image, sr + 1, sc, startColor, newColor); // 아래로 이동 fill(image, sr - 1, sc, startColor, newColor); // 위로 이동 fill(image, sr, sc + 1, startColor, newColor); // 오른쪽으로 이동 fill(image, sr, sc - 1, startColor, newColor); // 왼쪽으로 이동 } }

2024년 7월 2일 · 김태영