diff --git a/200211/102092/groupby1.sql b/200211/102092/groupby1.sql new file mode 100644 index 0000000..101dad8 --- /dev/null +++ b/200211/102092/groupby1.sql @@ -0,0 +1,5 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59040 +*/ + +SELECT ANIMAL_TYPE,COUNT(ANIMAL_TYPE) FROM ANIMAL_INS GROUP BY ANIMAL_TYPE ORDER BY ANIMAL_TYPE; \ No newline at end of file diff --git a/200211/102092/workoutClothes.java b/200211/102092/workoutClothes.java new file mode 100644 index 0000000..8bd0abf --- /dev/null +++ b/200211/102092/workoutClothes.java @@ -0,0 +1,85 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/* +https://programmers.co.kr/learn/courses/30/lessons/42862 + +1. Greedy 문제 +2. 다중 for문을 돌려야하나? +3. 정확히 어떤 컨셉의 문제인지 빠르게 캐치가 안됨. +4. + */ + +class Solution { + + public int solution(int n, int[] lost, int[] reserve) { + int result = n; + result -= lost.length; + + for (int i = 0; i < lost.length; i++) { + for (int j = 0; j < reserve.length; j++) { + // 여분을 가져왔는데, 잃어버렸을 경우, 그러면 빌려줄 수 없으니까. + if (lost[i] == reserve[j]) { + result++; + lost[i] = -1; + reserve[j] = -1; + break; + } + } + + } + + for (int i = 0; i < lost.length; i++) { + for (int j = 0; j < reserve.length; j++) { + if (lost[i] == reserve[j] - 1 || lost[i] == reserve[j] + 1) { + result++; + reserve[j] = -1; + break; + } + } + + } + return result; + } +} + +public class workoutClothes { + + Solution solution; + + @BeforeEach + public void 객체생성() { + solution = new Solution(); + } + + @Test + public void 테스트1() { + int n = 5; + int[] lost = new int[] { 2, 4 }; + int[] reserve = new int[] { 1, 3, 5 }; + int result = 5; + assertEquals(solution.solution(n, lost, reserve), result); + } + + @Test + public void 테스트2() { + int n = 5; + int[] lost = new int[] { 2, 4 }; + int[] reserve = new int[] { 3 }; + int result = 4; + + assertEquals(solution.solution(n, lost, reserve), result); + } + + @Test + public void 테스트3() { + int n = 3; + int[] lost = new int[] { 3 }; + int[] reserve = new int[] { 1 }; + int result = 2; + + assertEquals(solution.solution(n, lost, reserve), result); + } +} diff --git a/200212/102092/MoreSpicy.java b/200212/102092/MoreSpicy.java new file mode 100644 index 0000000..95cf47e --- /dev/null +++ b/200212/102092/MoreSpicy.java @@ -0,0 +1,31 @@ +import java.util.PriorityQueue; + +//https://programmers.co.kr/learn/courses/30/lessons/42626 + +/* +1. 스코빌 지수가, 숫자대로 정렬되어야 한다는 점에서 우선순위 큐를 이용하여 풀었음. + +*/ + +class Solution { + public int solution(int[] scoville, int K) { + PriorityQueue pq = new PriorityQueue<>(); + int answer = 0; + for(int input : scoville){ + pq.add(input); + } + + while(pq.peek() < K){ + if(pq.size() == 1){ + return -1; + } + + int lowest = pq.poll(); + int slowest = pq.poll(); + int mix = lowest + (slowest * 2); + pq.add(mix); + answer++; + } + return answer; + } +} \ No newline at end of file diff --git a/200212/102092/groupby2.sql b/200212/102092/groupby2.sql new file mode 100644 index 0000000..3ac5b3c --- /dev/null +++ b/200212/102092/groupby2.sql @@ -0,0 +1,11 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59041 + +SELECT NAME, COUNT(*) FROM ANIMAL_INS GROUP BY NAME HAVING COUNT(*) > 1; +null인 경우를 신경 쓰지 않아서 실패함.. + +SELECT NAME, COUNT(*) FROM ANIMAL_INS WHERE NAME IS NOT NULL GROUP BY NAME HAVING COUNT(NAME) > 1; + +*/ + +SELECT NAME, COUNT(NAME) FROM ANIMAL_INS GROUP BY NAME HAVING COUNT(NAME) > 1; \ No newline at end of file diff --git a/200213/102092/RomanToInteger.java b/200213/102092/RomanToInteger.java new file mode 100644 index 0000000..644b902 --- /dev/null +++ b/200213/102092/RomanToInteger.java @@ -0,0 +1,40 @@ +/* +https://leetcode.com/problems/roman-to-integer/ +*/ + +class Solution { + + public int romanToInt(String s) { + int sum = 0; + if (s.contains("IV")) + sum -= 2; + if (s.contains("IX")) + sum -= 2; + if (s.contains("XL")) + sum -= 20; + if (s.contains("XC")) + sum -= 20; + if (s.contains("CD")) + sum -= 200; + if (s.contains("CM")) + sum -= 200; + + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'I') + sum += 1; + if (s.charAt(i) == 'V') + sum += 5; + if (s.charAt(i) == 'X') + sum += 10; + if (s.charAt(i) == 'L') + sum += 50; + if (s.charAt(i) == 'C') + sum += 100; + if (s.charAt(i) == 'D') + sum += 500; + if (s.charAt(i) == 'M') + sum += 1000; + } + return sum; + } +} diff --git a/200213/102092/groupby3.sql b/200213/102092/groupby3.sql new file mode 100644 index 0000000..0900be1 --- /dev/null +++ b/200213/102092/groupby3.sql @@ -0,0 +1,5 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59412 +*/ + +SELECT HOUR(DATETIME), COUNT(DATETIME) FROM ANIMAL_OUTS WHERE HOUR(DATETIME)BETWEEN '09:00:00' AND '19:00:00' GROUP BY HOUR(DATETIME); \ No newline at end of file diff --git a/200214/102092/IsNull.sql b/200214/102092/IsNull.sql new file mode 100644 index 0000000..e7d04d3 --- /dev/null +++ b/200214/102092/IsNull.sql @@ -0,0 +1,5 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59039 +*/ + +SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NULL; \ No newline at end of file diff --git a/200214/102092/ValidParentheses.java b/200214/102092/ValidParentheses.java new file mode 100644 index 0000000..247251d --- /dev/null +++ b/200214/102092/ValidParentheses.java @@ -0,0 +1,37 @@ +/* +https://leetcode.com/problems/valid-parentheses/ +*/ + +import java.util.HashMap; +import java.util.Stack; + +public class ValidParentheses { + + Solution solution; + + class Solution { + + public boolean isValid(String s) { + HashMap hashMap = new HashMap<>(); + hashMap.put('(', ')'); + hashMap.put('[', ']'); + hashMap.put('{', '}'); + + Stack stack = new Stack(); + + for (int i = 0; i < s.length(); i++) { + char target = s.charAt(i); + if (target == '(' || target == '[' || target == '{') { + stack.add(target); + } else { + if (stack.isEmpty() || hashMap.get(stack.peek()) != target) { + return false; + } + stack.pop(); + } + + } + return stack.isEmpty(); + } + } +} diff --git a/200217/102092/IsNull2.sql b/200217/102092/IsNull2.sql new file mode 100644 index 0000000..1aa39a5 --- /dev/null +++ b/200217/102092/IsNull2.sql @@ -0,0 +1,5 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59407 +*/ + +SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NOT NULL; \ No newline at end of file diff --git a/200217/102092/MergeTwoSorted.java b/200217/102092/MergeTwoSorted.java new file mode 100644 index 0000000..00cbcc8 --- /dev/null +++ b/200217/102092/MergeTwoSorted.java @@ -0,0 +1,66 @@ +/* + * https://leetcode.com/problems/merge-two-sorted-lists/submissions/ + * + * + * public ListNode mergeTwoLists(ListNode l1, ListNode l2){ + if(l1 == null) return l2; + if(l2 == null) return l1; + if(l1.val < l2.val){ + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } else{ + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } +} +- 회귀를 이용해서 푸는 방법도 있구나. + */ + +class Solution { + + public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } + } + + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode answer, target; + + if (l1 == null && l2 == null) { + return null; + } + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + + answer = new ListNode(0); + target = answer; + + while (l1 != null && l2 != null) { + if (l1.val >= l2.val) { + target.next = l2; + l2 = l2.next; + } else { + target.next = l1; + l1 = l1.next; + } + target = target.next; + } + + if (l1 != null) { + target.next = l1; + } + if (l2 != null) { + target.next = l2; + } + return answer.next; + + } +} \ No newline at end of file diff --git a/200218/102092/IsNull3.sql b/200218/102092/IsNull3.sql new file mode 100644 index 0000000..ca74c8f --- /dev/null +++ b/200218/102092/IsNull3.sql @@ -0,0 +1,5 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59410 +*/ + +SELECT ANIMAL_TYPE, IFNULL(NAME,'No name'), SEX_UPON_INTAKE FROM ANIMAL_INS; \ No newline at end of file diff --git a/200218/102092/MergeSortedArray.java b/200218/102092/MergeSortedArray.java new file mode 100644 index 0000000..0a5daef --- /dev/null +++ b/200218/102092/MergeSortedArray.java @@ -0,0 +1,36 @@ +/* +https://leetcode.com/problems/merge-sorted-array/submissions/ +*/ + +import java.util.Arrays; + +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int index = 0; + for (int i = 0; i < nums1.length; i++) { + if (nums1[i] == 0 && index < n) { + nums1[i] = nums2[index]; + index++; + } + } + Arrays.sort(nums1); + } +} + +/* + * - 출제자가 의도한 해답은 아니었는 듯. + * - 처음 생각은 빈곳에 num2 배열 값을 넣고, Arrays.sort를 돌리자 + * + */ + +// public class Solution2 { +// public void merge(int A[], int m, int B[], int n) { +// int i = m - 1, j = n - 1, k = m + n - 1; +// while (i >= 0 && j >= 0) { +// A[k--] = A[i] > B[j] ? A[i--] : B[j--]; +// } +// while (j >= 0) { +// A[k--] = B[j--]; +// } +// } +// } diff --git a/200219/102092/Join1.sql b/200219/102092/Join1.sql new file mode 100644 index 0000000..5f3c10a --- /dev/null +++ b/200219/102092/Join1.sql @@ -0,0 +1,7 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59042 +*/ + +SELECT OUTS.ANIMAL_ID , OUTS.NAME FROM ANIMAL_INS AS INS +RIGHT JOIN ANIMAL_OUTS AS OUTS ON INS.ANIMAL_ID = OUTS.ANIMAL_ID +WHERE INS.ANIMAL_ID IS NULL; \ No newline at end of file diff --git a/200219/102092/RamenFactory.java b/200219/102092/RamenFactory.java new file mode 100644 index 0000000..b3579dc --- /dev/null +++ b/200219/102092/RamenFactory.java @@ -0,0 +1,40 @@ +import java.util.Comparator; +import java.util.PriorityQueue; +import java.util.Queue; + +/* +https://programmers.co.kr/learn/courses/30/lessons/42629 + +1. heap 이니까 우선순위 큐를 이용해봐야할듯. +2. 왜? 공급횟수를 최소로 해야하니까. +3. 주어진 매개변수를 모두 사용할 수 있도록 하자. + + */ + +public class RamenFactory { + + class Solution { + + public int solution(int stock, int[] dates, int[] supplies, int k) { + int answer = 0; + int index = 0; + Queue pq = new PriorityQueue<>(Comparator.reverseOrder()); + + for (int day = 0; day < k; day++) { + + if (index < dates.length && day == dates[index]) { + pq.add(supplies[index]); + index++; + } + + if (stock == 0) { + stock += pq.poll(); + answer++; + } + stock--; + } + + return answer; + } + } +} \ No newline at end of file diff --git a/200220/102092/Join2.sql b/200220/102092/Join2.sql new file mode 100644 index 0000000..befb2bc --- /dev/null +++ b/200220/102092/Join2.sql @@ -0,0 +1,10 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59043 +*/ + +SELECT OUTS.ANIMAL_ID, OUTS.NAME +FROM ANIMAL_OUTS AS OUTS +INNER JOIN ANIMAL_INS AS INS +ON INS.ANIMAL_ID = OUTS.ANIMAL_ID +WHERE INS.DATETIME > OUTS.DATETIME +ORDER BY INS.DATETIME ASC; \ No newline at end of file diff --git a/200220/102092/NumberOfIsland.java b/200220/102092/NumberOfIsland.java new file mode 100644 index 0000000..2c3ad0f --- /dev/null +++ b/200220/102092/NumberOfIsland.java @@ -0,0 +1,31 @@ +/* +https://leetcode.com/problems/number-of-islands/submissions/ +*/ + +class Solution { + public int numIslands(char[][] grid) { + int count = 0; + + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + if (grid[i][j] == '1') { + count++; + BFS(grid, i, j); + } + } + } + return count; + } + + private void BFS(char[][] grid, int i, int j) { + if (i < 0 || i >= grid.length || j < 0 || j >= grid[i].length || grid[i][j] == '0') { + return; + } + grid[i][j] = '0'; + + BFS(grid, i - 1, j); + BFS(grid, i + 1, j); + BFS(grid, i, j - 1); + BFS(grid, i, j + 1); + } +} \ No newline at end of file diff --git a/200220/102092/getMiddleString.java b/200220/102092/getMiddleString.java new file mode 100644 index 0000000..b2c2103 --- /dev/null +++ b/200220/102092/getMiddleString.java @@ -0,0 +1,16 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12903 +*/ + +class Solution { + public String solution(String s) { + int length = s.length(); + String result = ""; + if (length % 2 != 0) { + result = s.substring(length / 2, length / 2 + 1); + } else { + result = s.substring(length / 2 - 1, length / 2 + 1); + } + return result; + } +} \ No newline at end of file diff --git a/200221/102092/2016.java b/200221/102092/2016.java new file mode 100644 index 0000000..4d2da29 --- /dev/null +++ b/200221/102092/2016.java @@ -0,0 +1,20 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12901 +*/ + +class Solution { + public String solution(int a, int b) { + String answer = ""; + String[] weekends = { "FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU" }; + int[] days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + int temp = 0; + + for (int i = 0; i < a - 1; i++) { + temp += days[i]; + } + temp += b - 1; + answer = weekends[temp % 7]; + + return answer; + } +} \ No newline at end of file diff --git a/200221/102092/Contains-duplicate.java b/200221/102092/Contains-duplicate.java new file mode 100644 index 0000000..5a34f34 --- /dev/null +++ b/200221/102092/Contains-duplicate.java @@ -0,0 +1,29 @@ +import java.util.*; + +/* + * https://leetcode.com/problems/contains-duplicate/submissions/ + */ + +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet hash = new HashSet<>(); + + for (int i = 0; i < nums.length; i++) { + if (hash.contains(nums[i])) + return true; + hash.add(nums[i]); + } + return false; + } +} + +// class Solution { +// public boolean containsDuplicate(int[] nums) { +// Arrays.sort(nums); + +// for(int i = 0; i < nums.length -1 ; i++) { +// if(nums[i] == nums[i+1]) return true; +// } +// return false; +// } +// } diff --git a/200221/102092/Join3.sql b/200221/102092/Join3.sql new file mode 100644 index 0000000..0144c65 --- /dev/null +++ b/200221/102092/Join3.sql @@ -0,0 +1,11 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59044 +*/ + +SELECT INS.NAME, INS.DATETIME +FROM ANIMAL_INS AS INS +LEFT JOIN ANIMAL_OUTS AS OUTS +ON INS.ANIMAL_ID = OUTS.ANIMAL_ID +WHERE OUTS.ANIMAL_ID IS NULL +ORDER BY INS.DATETIME ASC +LIMIT 3; \ No newline at end of file diff --git a/200224/102092/Join4.sql b/200224/102092/Join4.sql new file mode 100644 index 0000000..966df5d --- /dev/null +++ b/200224/102092/Join4.sql @@ -0,0 +1,10 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59045 +*/ + +SELECT OUTS.ANIMAL_ID, OUTS.ANIMAL_TYPE, OUTS.NAME +FROM ANIMAL_INS AS INS +RIGHT JOIN ANIMAL_OUTS AS OUTS +ON INS.ANIMAL_ID = OUTS.ANIMAL_ID +WHERE INS.SEX_UPON_INTAKE IN ('Intact Female' , 'Intact Male') AND +OUTS.SEX_UPON_OUTCOME IN ('Neutered Male', 'Spayed Female'); \ No newline at end of file diff --git a/200224/102092/PascalsTriangle.java b/200224/102092/PascalsTriangle.java new file mode 100644 index 0000000..3a4074c --- /dev/null +++ b/200224/102092/PascalsTriangle.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; +import java.util.List; + +/* +https://leetcode.com/problems/pascals-triangle/submissions/ +*/ + +class Solution { + public List> generate(int numRows) { + List> triangle = new ArrayList<>(); + + if (numRows == 0) { + return triangle; + } + List first = new ArrayList<>(); + first.add(1); + triangle.add(first); + + for (int i = 1; i < numRows; i++) { + List prev = triangle.get(i - 1); + List row = new ArrayList<>(); + row.add(1); + + for (int j = 1; j < i; j++) { + row.add(prev.get(j - 1) + prev.get(j)); + } + row.add(1); + triangle.add(row); + } + return triangle; + } +} \ No newline at end of file diff --git a/200224/102092/TruckOnBridge1.java b/200224/102092/TruckOnBridge1.java new file mode 100644 index 0000000..84dc45c --- /dev/null +++ b/200224/102092/TruckOnBridge1.java @@ -0,0 +1,62 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/42583 +*/ + +import java.util.LinkedList; +import java.util.Queue; + +class Solution { + public int solution(int bridge_length, int weight, int[] truck_weights) { + Queue waits = new LinkedList<>(); + Queue moves = new LinkedList<>(); + + for (int w : truck_weights) { + waits.add(new Truck(w)); + } + + int answer = 0; + int currentWeight = 0; + + while (!waits.isEmpty() || !moves.isEmpty()) { + answer++; + + if (moves.isEmpty()) { + Truck t = waits.poll(); + currentWeight += t.weight; + moves.add(t); + continue; + } + + for (Truck t : moves) { + t.forward(); + } + + if (moves.peek().move > bridge_length) { + Truck t = moves.poll(); + currentWeight -= t.weight; + } + + if (!waits.isEmpty() && currentWeight + waits.peek().weight <= weight) { + Truck t = waits.poll(); + currentWeight += t.weight; + moves.add(t); + } + } + + return answer; + } +} + +class Truck { + int weight; + int move; + + public Truck(int weight) { + this.weight = weight; + this.move = 1; + } + + public void forward() { + move++; + } +} \ No newline at end of file diff --git a/200225/102092/String1.sql b/200225/102092/String1.sql new file mode 100644 index 0000000..78969a1 --- /dev/null +++ b/200225/102092/String1.sql @@ -0,0 +1,8 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59046 +*/ + +SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE +FROM ANIMAL_INS +WHERE NAME IN ('Lucy', 'Ella','Pickle', 'Rogan', 'Sabrina' ,'Mitty') +ORDER BY ANIMAL_ID ASC; \ No newline at end of file diff --git a/200225/102092/TwoSum2.java b/200225/102092/TwoSum2.java new file mode 100644 index 0000000..6c2c929 --- /dev/null +++ b/200225/102092/TwoSum2.java @@ -0,0 +1,22 @@ +/* +https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ +*/ + +class Solution { + public int[] twoSum(int[] numbers, int target) { + int aPointer = 0; + int bPointer = numbers.length - 1; + + while (aPointer <= bPointer) { + int sum = numbers[aPointer] + numbers[bPointer]; + if (sum > target) { + bPointer -= 1; + } else if (sum < target) { + aPointer += 1; + } else { + return new int[] { aPointer + 1, bPointer + 1 }; + } + } + return new int[] { aPointer + 1, bPointer + 1 }; + } +} \ No newline at end of file diff --git a/200225/FindPrimeNumber.java b/200225/FindPrimeNumber.java new file mode 100644 index 0000000..dba7470 --- /dev/null +++ b/200225/FindPrimeNumber.java @@ -0,0 +1,30 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12921 +*/ + +import java.util.Arrays; + +class Solution { + public int solution(int n) { + int answer = 0; + int[] nums = new int[n + 1]; + + for (int i = 2; i <= n; i++) { + nums[i] = i; + } + + for (int i = 2; i <= n; i++) { + if (nums[i] == 0) { + continue; + } + + for (int j = 2 * i; j <= n; j += i) { + nums[j] = 0; + } + } + + answer = (int) Arrays.stream(nums).filter(x -> x != 0).count(); + + return answer; + } +} \ No newline at end of file diff --git a/200226/102092/NotSameNumber.java b/200226/102092/NotSameNumber.java new file mode 100644 index 0000000..599903f --- /dev/null +++ b/200226/102092/NotSameNumber.java @@ -0,0 +1,21 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12906 +*/ + +import java.util.ArrayList; +import java.util.List; + +public class NotSameNumber { + public int[] solution(int[] arr) { + List list = new ArrayList<>(); + int num = 10; + for (int i = 0; i < arr.length; i++) { + if (num != arr[i]) { + list.add(arr[i]); + } + num = arr[i]; + } + int[] answer = list.stream().mapToInt(Number::intValue).toArray(); + return answer; + } +} \ No newline at end of file diff --git a/200226/102092/String2.sql b/200226/102092/String2.sql new file mode 100644 index 0000000..a1254a3 --- /dev/null +++ b/200226/102092/String2.sql @@ -0,0 +1,7 @@ +/* +https://programmers.co.kr/learn/challenges +*/ + +SELECT ANIMAL_ID, NAME FROM ANIMAL_INS +WHERE ANIMAL_TYPE = "Dog" AND NAME LIKE "%el%" +ORDER BY NAME ASC; diff --git a/200227/102092/LinkedListCycle1.java b/200227/102092/LinkedListCycle1.java new file mode 100644 index 0000000..1d38a5c --- /dev/null +++ b/200227/102092/LinkedListCycle1.java @@ -0,0 +1,30 @@ +/* +https://leetcode.com/problems/linked-list-cycle/solution/ +*/ + +import java.util.*; + +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } +} + +public class LinkedListCycle1 { + public boolean hasCycle(ListNode head) { + Set set = new HashSet<>(); + while (head != null) { + if (set.contains(head)) { + return true; + } else { + set.add(head); + } + head = head.next; + } + return false; + } +} \ No newline at end of file diff --git a/200227/102092/String3.sql b/200227/102092/String3.sql new file mode 100644 index 0000000..92e5f9f --- /dev/null +++ b/200227/102092/String3.sql @@ -0,0 +1,10 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59409 +*/ + +SELECT ANIMAL_ID, NAME, + CASE + WHEN SEX_UPON_INTAKE LIKE '%Neutered%' OR SEX_UPON_INTAKE LIKE '%Spayed%' + THEN 'O' ELSE 'X' + END AS '중성화' +FROM ANIMAL_INS; \ No newline at end of file diff --git a/200228/102092/MinStack.java b/200228/102092/MinStack.java new file mode 100644 index 0000000..6843afe --- /dev/null +++ b/200228/102092/MinStack.java @@ -0,0 +1,49 @@ +/* + * https://leetcode.com/problems/min-stack/submissions/ + */ + +class MinStack { + private Node head; + + /** initialize your data structure here. */ + public MinStack() { + } + + public void push(int x) { + if (head == null) { + head = new Node(x, x, null); + } else { + head = new Node(x, Math.min(x, head.min), head); + } + } + + public void pop() { + head = head.next; + } + + public int top() { + return head.value; + } + + public int getMin() { + return head.min; + } + + private class Node { + int value; + int min; + Node next; + + private Node(int value, int min, Node next) { + this.value = value; + this.min = min; + this.next = next; + } + } +} + +/** + * Your MinStack object will be instantiated and called as such: MinStack obj = + * new MinStack(); obj.push(x); obj.pop(); int param_3 = obj.top(); int param_4 + * = obj.getMin(); + */ \ No newline at end of file diff --git a/200228/102092/MinStack2.java b/200228/102092/MinStack2.java new file mode 100644 index 0000000..f521696 --- /dev/null +++ b/200228/102092/MinStack2.java @@ -0,0 +1,40 @@ +import java.util.Stack; + +class MinStack { + Stack value; + Stack minValue; + + /** initialize your data structure here. */ + public MinStack() { + value = new Stack<>(); + minValue = new Stack<>(); + } + + public void push(int x) { + if (minValue.isEmpty() || x <= minValue.peek()) { + minValue.push(x); + } + value.push(x); + } + + public void pop() { + if (value.peek().equals(minValue.peek())) { + minValue.pop(); + } + value.pop(); + } + + public int top() { + return value.peek(); + } + + public int getMin() { + return minValue.peek(); + } +} + +/** + * Your MinStack object will be instantiated and called as such: MinStack obj = + * new MinStack(); obj.push(x); obj.pop(); int param_3 = obj.top(); int param_4 + * = obj.getMin(); + */ \ No newline at end of file diff --git a/200228/102092/String3.sql b/200228/102092/String3.sql new file mode 100644 index 0000000..2634bfb --- /dev/null +++ b/200228/102092/String3.sql @@ -0,0 +1,6 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59414 +*/ + +SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, "%Y-%m-%d") AS '날짜' +FROM ANIMAL_INS ORDER BY ANIMAL_ID; \ No newline at end of file diff --git a/200301/102092/CompareStrings.java b/200301/102092/CompareStrings.java new file mode 100644 index 0000000..2d7f3fa --- /dev/null +++ b/200301/102092/CompareStrings.java @@ -0,0 +1,25 @@ +import java.util.*; + +/* +https://programmers.co.kr/learn/courses/30/lessons/12915 +*/ + +class Solution { + public String[] solution(String[] strings, int n) { + Arrays.sort(strings, new Comparator() { + @Override + public int compare(String s1, String s2) { + if (s1.charAt(n) > s2.charAt(n)) { + // 오름차순 + return 1; + } else if (s1.charAt(n) < s2.charAt(n)) { + // 오름차순으로 되어있으면 굳이 자리 바꾸지 말고 + return -1; + } else { + return s1.compareTo(s2); + } + } + }); + return strings; + } +} \ No newline at end of file diff --git a/200301/102092/DivisionArray.java b/200301/102092/DivisionArray.java new file mode 100644 index 0000000..8ca0d3b --- /dev/null +++ b/200301/102092/DivisionArray.java @@ -0,0 +1,30 @@ +import java.util.*; + +/* +https://programmers.co.kr/learn/courses/30/lessons/12910 +*/ + +class Solution { + public int[] solution(int[] arr, int divisor) { + List answer = new ArrayList<>(); + + for (int i = 0; i < arr.length; i++) { + if (arr[i] % divisor == 0) { + answer.add(arr[i]); + } + } + Collections.sort(answer); + return convertIntegers(answer); + } + + public static int[] convertIntegers(List integers) { + if (integers.size() == 0) { + return new int[] { -1 }; + } + int[] ret = new int[integers.size()]; + for (int i = 0; i < ret.length; i++) { + ret[i] = integers.get(i).intValue(); + } + return ret; + } +} \ No newline at end of file diff --git a/200301/102092/String4.sql b/200301/102092/String4.sql new file mode 100644 index 0000000..ef64680 --- /dev/null +++ b/200301/102092/String4.sql @@ -0,0 +1,10 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/59411 +*/ + +SELECT INS.ANIMAL_ID, INS.NAME FROM ANIMAL_INS AS INS +LEFT JOIN ANIMAL_OUTS AS OUTS +ON INS.ANIMAL_ID = OUTS.ANIMAL_ID +WHERE OUTS.ANIMAL_ID IS NOT NULL +ORDER BY INS.DATETIME - OUTS.DATETIME +LIMIT 2; diff --git a/200302/102092/Intersection.java b/200302/102092/Intersection.java new file mode 100644 index 0000000..b3c0f2e --- /dev/null +++ b/200302/102092/Intersection.java @@ -0,0 +1,41 @@ +import java.util.HashSet; +import java.util.Set; + +/* + * https://leetcode.com/problems/intersection-of-two-linked-lists + */ + +public class Intersection { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + if (headA == null || headB == null) + return null; + + Set nodes = new HashSet<>(); + + ListNode a = headA; + ListNode b = headB; + + while (a != null) { + nodes.add(a); + a = a.next; + } + + while (b != null) { + if (nodes.contains(b)) { + return b; + } + b = b.next; + } + return null; + } + + public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } + } +} \ No newline at end of file diff --git a/200302/102092/Postorder.java b/200302/102092/Postorder.java new file mode 100644 index 0000000..2efb495 --- /dev/null +++ b/200302/102092/Postorder.java @@ -0,0 +1,47 @@ +import java.util.LinkedList; +import java.util.List; + +/* +https://leetcode.com/problems/n-ary-tree-postorder-traversal/ +*/ + +class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } +}; + +class Solution { + public List postorder(Node root) { + + LinkedList stk = new LinkedList<>(); + LinkedList out = new LinkedList<>(); + + if (root == null) { + return out; + } + + stk.add(root); + + while (!stk.isEmpty()) { + Node node = stk.pollLast(); + out.addFirst(node.val); + + for (Node child : node.children) { + stk.add(child); + } + } + return out; + } +} \ No newline at end of file diff --git a/200302/102092/Preorder.java b/200302/102092/Preorder.java new file mode 100644 index 0000000..c9cd5b9 --- /dev/null +++ b/200302/102092/Preorder.java @@ -0,0 +1,52 @@ +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/* + * https://leetcode.com/problems/n-ary-tree-preorder-traversal/ + */ + +/* +// Definition for a Node. +class Node { + public int val; + public List children; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } +}; +*/ +class Solution { + public List preorder(Node root) { + LinkedList stk = new LinkedList<>(); + LinkedList out = new LinkedList<>(); + + if (root == null) { + return out; + } + + stk.add(root); + + while (!stk.isEmpty()) { + Node node = stk.pollLast(); + out.add(node.val); + + if (node.children != null) { + Collections.reverse(node.children); + } + + for (Node child : node.children) { + stk.add(child); + } + } + return out; + } +} \ No newline at end of file diff --git a/200304/102092/CaesarCipher.java b/200304/102092/CaesarCipher.java new file mode 100644 index 0000000..8b1541b --- /dev/null +++ b/200304/102092/CaesarCipher.java @@ -0,0 +1,24 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12926 +*/ + +class Solution { + public String solution(String s, int n) { + char[] chars = s.toCharArray(); + + for (int i = 0; i < chars.length; i++) { + if (chars[i] == ' ') + continue; + // 범위 넘어가는 대문자 + else if (chars[i] <= 'Z' && chars[i] >= 'A' && chars[i] + n > 'Z') + chars[i] = (char) (chars[i] + n - 26); + // 범위 넘어가는 소문자 + else if (chars[i] <= 'z' && chars[i] >= 'a' && chars[i] + n > 'z') + chars[i] = (char) (chars[i] + n - 26); + else + chars[i] = (char) (chars[i] + n); + + } + return String.valueOf(chars); + } +} \ No newline at end of file diff --git a/200304/102092/MajorityElement.java b/200304/102092/MajorityElement.java new file mode 100644 index 0000000..3525fef --- /dev/null +++ b/200304/102092/MajorityElement.java @@ -0,0 +1,27 @@ +import java.util.HashMap; +import java.util.Map; + +/* +https://leetcode.com/problems/majority-element/submissions/ +*/ + +class Solution { + public int majorityElement(int[] nums) { + int result = 0; + Map map = new HashMap<>(); + + for (int num : nums) { + if (!map.containsKey(num)) { + map.put(num, 1); + } else { + map.put(num, map.get(num) + 1); + } + if (map.get(num) > nums.length / 2) { + result = num; + break; + } + } + return result; + + } +} \ No newline at end of file diff --git a/200304/102092/MergeTrees.java b/200304/102092/MergeTrees.java new file mode 100644 index 0000000..e0bf4a5 --- /dev/null +++ b/200304/102092/MergeTrees.java @@ -0,0 +1,30 @@ +/* +https://leetcode.com/problems/merge-two-binary-trees/ +*/ + +class Solution { + public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { + if (t1 == null) { + return t2; + } + + if (t2 == null) { + return t1; + } + + t1.val += t2.val; + t1.left = mergeTrees(t1.left, t2.left); + t1.right = mergeTrees(t1.right, t2.right); + return t1; + } + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + } +} \ No newline at end of file diff --git a/200305/102092/FactorialZeroes.java b/200305/102092/FactorialZeroes.java new file mode 100644 index 0000000..2a476e4 --- /dev/null +++ b/200305/102092/FactorialZeroes.java @@ -0,0 +1,16 @@ +/* +https://leetcode.com/list?selectedList=xtcq66mm +*/ + +class Solution { + public int trailingZeroes(int n) { + if (n < 1) { + return 0; + } + int answer = 0; + for (long i = 5; i <= n; i *= 5) { + answer += n / i; + } + return answer; + } +} \ No newline at end of file diff --git a/200305/102092/StrangeWord.java b/200305/102092/StrangeWord.java new file mode 100644 index 0000000..78c3d2b --- /dev/null +++ b/200305/102092/StrangeWord.java @@ -0,0 +1,29 @@ +/* +https://programmers.co.kr/learn/courses/30/lessons/12930 +*/ + +class Solution { + public String solution(String s) { + String answer = ""; + if (s.length() == 0) { + return answer; + } + String[] arr = s.split(" ", -1); + + for (int i = 0; i < arr.length; i++) { + String str = arr[i]; + for (int j = 0; j < str.length(); j++) { + char ch = str.charAt(j); + if (j == 0 || j % 2 == 0) { + answer += String.valueOf(ch).toUpperCase(); + } else { + answer += String.valueOf(ch).toLowerCase(); + } + } + if (i < arr.length - 1) { + answer += " "; + } + } + return answer; + } +} \ No newline at end of file