diff --git a/.gitignore b/.gitignore index ad46b30..500f3f6 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ typings/ # next.js build output .next + +# for cpp +a.out diff --git a/190624/2580-sudoku.cpp b/190624/2580-sudoku.cpp new file mode 100644 index 0000000..3e575f0 --- /dev/null +++ b/190624/2580-sudoku.cpp @@ -0,0 +1,54 @@ +#include +#include + +int b[9][9]; + +using namespace std; + +vector findNums(int i,int j) { + //map nums; + //checkv(i, nums); + //checkh(j, nums); + //checkbox(i,j, nums); +} + +bool find = false; + +void fillNums(int i, int j) { + if (find) return; + if (i == 9 && j == 9 && b[i][j] != 0) { + find = true; + return; + } + + int jn = j < 9 ? j + 1: j; + int in = jn == j && i < 9; i + 1: i; + + if (b[i][j] == 0) { + vector nums = findNums(i,j); + if (nums.size() == 0) return; + for (auto n: nums) { + board[i][j] = n; + fillNums(in, jn); + } + } + return; +} + +int main() { + char c; + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + cin >> c; + b[i][j] = c - '0'; + } + } + fillNums(0,0); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + cout << b[i][j] << " "; + } + cout << "\n"; + } + return 0; +} diff --git a/190624/input.txt b/190624/input.txt new file mode 100644 index 0000000..2ea6910 --- /dev/null +++ b/190624/input.txt @@ -0,0 +1,9 @@ +0 3 5 4 6 9 2 7 8 +7 8 2 1 0 5 6 0 9 +0 6 0 2 7 8 1 3 5 +3 2 1 0 4 6 8 9 7 +8 0 4 9 1 3 5 0 6 +5 9 6 8 2 0 4 1 3 +9 1 7 6 5 2 0 8 0 +6 0 3 7 0 1 9 5 2 +2 5 8 3 9 4 7 6 0 diff --git a/200203/honux77/59415.sql b/200203/honux77/59415.sql new file mode 100644 index 0000000..943d93d --- /dev/null +++ b/200203/honux77/59415.sql @@ -0,0 +1,6 @@ +-- 가장 최근에 동물이 들어온 날짜를 좋아하는 SQL 문 +SELECT DATETIME FROM ANIMAL_INS ORDER BY DATETIME DESC LIMIT 1; + +-- 다른 풀이 +SELECT MAX(DATETIME) FROM ANIMAL_INS; + diff --git a/200203/ksundong/Solution.java b/200203/ksundong/Solution.java new file mode 100644 index 0000000..666f901 --- /dev/null +++ b/200203/ksundong/Solution.java @@ -0,0 +1,34 @@ +/** + * 프로그래머스 - 체육복(https://programmers.co.kr/learn/courses/30/lessons/42862) + * 1. 일단 Test Case를 만들었다. + * 2. 일단 배열의 length값을 빼고 더해보았다. (test 1, 3 실패) + * 3. 만들어 둔 Test Case를 모두 만족하는 코드를 작성하였다. + * 4. 예외 상황이 있어서(조건을 제대로 안읽어서) 코드를 수정했다. (먼저 같은 경우를 비교) + */ +public class Solution { + // 전체 학생의 수 n, 도난당한 학생의 배열 lost, 여벌의 체육복을 가진 학생의 배열 reserve + public int solution(int n, int[] lost, int[] reserve) { + n -= lost.length; + for (int i = 0; i < lost.length; i++) { + for (int j = 0; j < reserve.length; j++) { + if (lost[i] == reserve[j]) { + n++; + lost[i] = 32; + 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) { + n++; + lost[i] = 32; + reserve[j] = -1; + break; + } + } + } + return n; + } +} diff --git a/200203/ksundong/SolutionTest.java b/200203/ksundong/SolutionTest.java new file mode 100644 index 0000000..b98fde9 --- /dev/null +++ b/200203/ksundong/SolutionTest.java @@ -0,0 +1,63 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SolutionTest { + Solution solution; + + @BeforeEach + void setUp() { + solution = new Solution(); + } + + @Test + void testSolution1() { + int n = 5; + int[] lost = {2, 4}; + int[] reserve = {1, 3, 5}; + int expected = 5; + int actual = solution.solution(n, lost, reserve); + assertEquals(expected, actual, "예상 출력과 다릅니다."); + } + + @Test + void testSolution2() { + int n = 5; + int[] lost = {2, 4}; + int[] reserve = {3}; + int expected = 4; + int actual = solution.solution(n, lost, reserve); + assertEquals(expected, actual, "예상 출력과 다릅니다."); + } + + @Test + void testSolution3() { + int n = 3; + int[] lost = {3}; + int[] reserve = {1}; + int expected = 2; + int actual = solution.solution(n, lost, reserve); + assertEquals(expected, actual, "예상 출력과 다릅니다."); + } + + @Test + void testSolution4() { + int n = 5; + int[] lost = {4, 5}; + int[] reserve = {4}; + int expected = 4; + int actual = solution.solution(n, lost, reserve); + assertEquals(expected, actual, "예상 출력과 다릅니다."); + } + + @Test + void testSolution5() { + int n = 10; + int[] lost = {3, 9, 10}; + int[] reserve = {3, 8, 9}; + int expected = 9; + int actual = solution.solution(n, lost, reserve); + assertEquals(expected, actual, "예상 출력과 다릅니다."); + } +} diff --git a/200204/102092/Top.java b/200204/102092/Top.java new file mode 100644 index 0000000..a1b7489 --- /dev/null +++ b/200204/102092/Top.java @@ -0,0 +1,30 @@ +import java.util.Stack; + +/* +https://programmers.co.kr/learn/courses/30/lessons/42588 +*/ + +class Solution { + public int[] solution(int[] heights) { + int[] answer = new int[heights.length]; + Stack stk = new Stack(); + int top; + + for (int i = 0; i < heights.length; i++) { + stk.add(heights[i]); + } + + while (!stk.isEmpty()) { + top = stk.pop(); + + for (int j = stk.size(); j >= 0; j--) { + if (heights[j] > top) { + answer[stk.size()] = j + 1; + break; + } + } + } + + return answer; + } +} \ No newline at end of file diff --git a/200204/honux/59035.sql b/200204/honux/59035.sql new file mode 100644 index 0000000..ec5e6a7 --- /dev/null +++ b/200204/honux/59035.sql @@ -0,0 +1,2 @@ +-- 역순 정렬 +SELECT NAME, DATETIME FROM ANIMAL_INS ORDER BY ANIMAL_ID DESC; diff --git a/200204/honux/60057-Solution.java b/200204/honux/60057-Solution.java new file mode 100644 index 0000000..aec7a94 --- /dev/null +++ b/200204/honux/60057-Solution.java @@ -0,0 +1,56 @@ +//programmers 60057 카카오 공채 문자열 압축 +//어렵진 않은데 시간이 오래 걸렸다. + +import java.util.List; +import java.util.ArrayList; + +class Solution { + + private List cutString(String origin, int size) { + List ret = new ArrayList<>(); + for (int i = 0; i < origin.length(); i += size) { + int e = (i + size) > origin.length() ? origin.length(): i + size; + String str = origin.substring(i, e); + ret.add(str); + } + return ret; + } + + private String findCompressString(String str, int size) { + List slist = cutString(str, size); + + int count = 0; + StringBuffer newStr = new StringBuffer(); + + String prev = slist.get(0); + newStr.append(prev); + int repeat = 1; + for (int i = 1; i < slist.size(); i++) { + String curr = slist.get(i); + if(curr.equals(prev)) { + repeat++; + } else { + if (repeat != 1) { + newStr.append(Integer.toString(repeat)); + repeat = 1; + } + newStr.append(curr); + prev = curr; + } + } + + if (repeat != 1) { + newStr.append(Integer.toString(repeat)); + } + return newStr.toString(); + } + + public int solution(String s) { + int ans = s.length(); + for(int i = 1; i < s.length(); i++) { + ans = Math.min(ans, findCompressString(s, i).length()); + } + return ans; + } +} + diff --git a/200204/ksundong/GetMiddleCharacterSolution.java b/200204/ksundong/GetMiddleCharacterSolution.java new file mode 100644 index 0000000..05b8151 --- /dev/null +++ b/200204/ksundong/GetMiddleCharacterSolution.java @@ -0,0 +1,14 @@ +/** + * 프로그래머스 가운데 글자 가져오기 (https://programmers.co.kr/learn/courses/30/lessons/12903) + * - Test Case를 가져와서 만들었다. + * - 역시 너무 쉬웠다. + * - 좀 더 짧은 코드를 만들기 위해서, 짝수, 홀수 모두 통용되는 코드를 짜기위해 노력했다. + */ +public class Solution { + public String solution(String s) { + int length = s.length(); + int halfLength = (length - 1) / 2; + int endIndex = length / 2 + 1; + return s.substring(halfLength, endIndex); + } +} diff --git a/200204/ksundong/GetMiddleCharacterSolutionTest.java b/200204/ksundong/GetMiddleCharacterSolutionTest.java new file mode 100644 index 0000000..cbada28 --- /dev/null +++ b/200204/ksundong/GetMiddleCharacterSolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SolutionTest { + Solution solution; + + @BeforeEach + void setUp() { + solution = new Solution(); + } + + @Test + void testSolution1() { + String input = "abcde"; + String expected = "c"; + String actual = solution.solution(input); + assertEquals(expected, actual, "대상 글자는 가운데 글자가 아닙니다."); + } + + @Test + void testSolution2() { + String input = "qwer"; + String expected = "we"; + String actual = solution.solution(input); + assertEquals(expected, actual, "대상 글자는 가운데 글자가 아닙니다."); + } +} diff --git a/200204/ksundong/Solution.java b/200204/ksundong/Solution.java new file mode 100644 index 0000000..8bf6345 --- /dev/null +++ b/200204/ksundong/Solution.java @@ -0,0 +1,20 @@ +/* + * 프로그래머스 2016년 - https://programmers.co.kr/learn/courses/30/lessons/12901 + * - Test Case를 하나 만들었다. + * - 너무 쉬웠다. 역시 CodeSquad Java Playground! + * - https://www.inflearn.com/course/java-codesquad/dashboard + */ +public class Solution { + public String solution(int a, int b) { + String[] dayOfWeek = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; + int dateCount = 5; + int[] monthDayArray = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + for (int i = 0; i < a - 1; i++) { + dateCount += monthDayArray[i]; + } + dateCount += b - 1; + + return dayOfWeek[dateCount % 7]; + } +} diff --git a/200204/ksundong/SolutionTest.java b/200204/ksundong/SolutionTest.java new file mode 100644 index 0000000..b929549 --- /dev/null +++ b/200204/ksundong/SolutionTest.java @@ -0,0 +1,31 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SolutionTest { + Solution solution; + + @BeforeEach + void setUp() { + solution = new Solution(); + } + + @Test + void testSolution1() { + int month = 5; + int date = 24; + String expected = "TUE"; + String actual = solution.solution(month, date); + assertEquals(expected, actual, "원하는 요일명과 일치하지 않습니다."); + } + + @Test + void testSolution2() { + int month = 1; + int date = 1; + String expected = "FRI"; + String actual = solution.solution(month, date); + assertEquals(expected, actual, "원하는 요일명과 일치하지 않습니다."); + } +} diff --git a/200205/102092/MockTest.java b/200205/102092/MockTest.java new file mode 100644 index 0000000..e0eba66 --- /dev/null +++ b/200205/102092/MockTest.java @@ -0,0 +1,39 @@ +import java.util.*; + +/* +https://programmers.co.kr/learn/courses/30/lessons/42840 +*/ + +class Solution { + public int[] solution(int[] answers) { + int[] A = new int[] { 1, 2, 3, 4, 5 }; + int[] B = new int[] { 2, 1, 2, 3, 2, 4, 2, 5 }; + int[] C = new int[] { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 }; + + List answer = new ArrayList<>(); + int a = 0; + int b = 0; + int c = 0; + + for (int i = 0; i < answers.length; i++) { + if (A[i % A.length] == answers[i]) + a += 1; + if (B[i % B.length] == answers[i]) + b += 1; + if (C[i % C.length] == answers[i]) + c += 1; + } + + int max = Math.max(a, Math.max(b, c)); + + if (a == max) + answer.add(1); + if (b == max) + answer.add(2); + if (c == max) + answer.add(3); + + int[] ret = answer.stream().mapToInt(Integer::intValue).toArray(); + return ret; + } +} \ No newline at end of file diff --git a/200205/honux77/programmers-12903-Solution.java b/200205/honux77/programmers-12903-Solution.java new file mode 100644 index 0000000..48e3ce8 --- /dev/null +++ b/200205/honux77/programmers-12903-Solution.java @@ -0,0 +1,13 @@ +//https://programmers.co.kr/learn/courses/30/lessons/12903 +class Solution { + public String solution(String s) { + String answer = ""; + int m = s.length() / 2; + if (s.length() % 2 == 0) { + answer = s.substring(m - 1, m + 1); + } else { + answer = s.substring(m, m + 1); + } + return answer; + } +} diff --git a/200205/ksundong/DividingNumberArray/Solution.java b/200205/ksundong/DividingNumberArray/Solution.java new file mode 100644 index 0000000..c25572c --- /dev/null +++ b/200205/ksundong/DividingNumberArray/Solution.java @@ -0,0 +1,24 @@ +import java.util.ArrayList; +import java.util.Arrays; + +/** + * [나누어 떨어지는 숫자 배열](https://programmers.co.kr/learn/courses/30/lessons/12910) + * - [풀이](https://github.com/ksundong/algorithm-solution/blob/0007cfb4c168f48bf1f4597814fc715d6a33e685/src/main/java/dev/idion/programmers/dividingnumberarray/Solution.java) + * - 별로 깊게 생각해보지 않아도 되는 문제였다. + */ +public class Solution { + public int[] solution(int[] arr, int divisor) { + ArrayList dividingArray = new ArrayList<>(); + for (int i = 0; i < arr.length; i++) { + if (arr[i] % divisor == 0) dividingArray.add(arr[i]); + } + if (dividingArray.isEmpty()) return new int[]{-1}; + + int[] answer = new int[dividingArray.size()]; + for (int i = 0; i < dividingArray.size(); i++) { + answer[i] = dividingArray.get(i); + } + Arrays.sort(answer); + return answer; + } +} diff --git a/200205/ksundong/DividingNumberArray/SolutionTest.java b/200205/ksundong/DividingNumberArray/SolutionTest.java new file mode 100644 index 0000000..4a3aaca --- /dev/null +++ b/200205/ksundong/DividingNumberArray/SolutionTest.java @@ -0,0 +1,40 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +class SolutionTest { + Solution solution; + + @BeforeEach + void setUp() { + solution = new Solution(); + } + + @Test + void testSolution1() { + int[] inputArray = {5, 7, 9, 10}; + int inputdivisor = 5; + int[] expected = {5, 10}; + int[] actual = solution.solution(inputArray, inputdivisor); + assertArrayEquals(expected, actual, "원하는 배열이 출력되지 않았습니다."); + } + + @Test + void testSolution2() { + int[] inputArray = {2, 36, 1, 3}; + int inputdivisor = 1; + int[] expected = {1, 2, 3, 36}; + int[] actual = solution.solution(inputArray, inputdivisor); + assertArrayEquals(expected, actual, "원하는 배열이 출력되지 않았습니다."); + } + + @Test + void testSolution3() { + int[] inputArray = {3, 2, 6}; + int inputdivisor = 10; + int[] expected = {-1}; + int[] actual = solution.solution(inputArray, inputdivisor); + assertArrayEquals(expected, actual, "원하는 배열이 출력되지 않았습니다."); + } +} diff --git a/200205/ksundong/NoDuplicateNumber/Solution.java b/200205/ksundong/NoDuplicateNumber/Solution.java new file mode 100644 index 0000000..517771c --- /dev/null +++ b/200205/ksundong/NoDuplicateNumber/Solution.java @@ -0,0 +1,30 @@ +/** + * [같은 숫자는 싫어](https://programmers.co.kr/learn/courses/30/lessons/12906) + * - 풀이는 했지만 공간복잡도가 높아지는 단점이 있다. + * - Arrays.copyOf를 사용하지 않았더니 0.5 ms가 단축되었다. + */ +public class Solution { + public int[] solution(int[] arr) { + int tmp = -1; + int count = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] != tmp) { + count++; + } + tmp = arr[i]; + } + + tmp = -1; + int tempCount = 0; + int[] answer = new int[count]; + + for (int i = 0; i < arr.length; i++) { + if (arr[i] != tmp) { + answer[tempCount] = arr[i]; + tempCount++; + } + tmp = arr[i]; + } + return answer; + } +} diff --git a/200205/ksundong/NoDuplicateNumber/SolutionTest.java b/200205/ksundong/NoDuplicateNumber/SolutionTest.java new file mode 100644 index 0000000..fe8e859 --- /dev/null +++ b/200205/ksundong/NoDuplicateNumber/SolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +class SolutionTest { + Solution solution; + + @BeforeEach + void setUp() { + solution = new Solution(); + } + + @Test + void testSolution1() { + int[] input = {1, 1, 3, 3, 0, 1, 1}; + int[] expected = {1, 3, 0, 1}; + int[] actual = solution.solution(input); + assertArrayEquals(expected, actual, "에상한 배열과 일치하지 않습니다."); + } + + @Test + void testSolution2() { + int[] input = {4, 4, 4, 3, 3}; + int[] expected = {4, 3}; + int[] actual = solution.solution(input); + assertArrayEquals(expected, actual, "에상한 배열과 일치하지 않습니다."); + } +} diff --git a/200206/honux77/programmers-42576-Solution.java b/200206/honux77/programmers-42576-Solution.java new file mode 100644 index 0000000..4840eba --- /dev/null +++ b/200206/honux77/programmers-42576-Solution.java @@ -0,0 +1,32 @@ +//프로그래머스 완주하지 못한 선수 + +import java.util.HashMap; + +public class Solution { + + public String solution(String[] participant, String[] completion) { + HashMap cmap = new HashMap<>(); + + for (String s: completion) { + Integer n = cmap.get(s); + if (n == null) { + cmap.put(s, 1); + } + else { + n++; + cmap.put(s, n); + } + } + + for (String s: participant) { + Integer n = cmap.get(s); + if(n == null || n == 0) { + return s; + } else { + n--; + cmap.put(s, n); + } + }; + return "Me"; + } +} \ No newline at end of file diff --git a/200207/honux77/programmers-42576-Solution-2.java b/200207/honux77/programmers-42576-Solution-2.java new file mode 100644 index 0000000..c1d7a59 --- /dev/null +++ b/200207/honux77/programmers-42576-Solution-2.java @@ -0,0 +1,18 @@ +// 정렬 사용해서 다시 풀었음 +import java.util.Arrays; + +public class Solution { + + public String solution(String[] participant, String[] completion) { + Arrays.sort(participant); + Arrays.sort(completion); + + int i; + for (i = 0; i < completion.length; i++) { + if (!participant[i].equals(completion[i])) { + return participant[i]; + } + } + return participant[i]; + } +} \ No newline at end of file diff --git a/200207/honux77/programmers-42576-Solution.java b/200207/honux77/programmers-42576-Solution.java new file mode 100644 index 0000000..e37d871 --- /dev/null +++ b/200207/honux77/programmers-42576-Solution.java @@ -0,0 +1,21 @@ +//프로그래머스 완주하지 못한 선수 + +import java.util.HashMap; + +public class Solution { + + public String solution(String[] participant, String[] completion) { + HashMap cmap = new HashMap<>(); + + for (String s: completion) { + cmap.put(s, cmap.getOrDefault(s, 0) + 1); + } + + for (String s: participant) { + int n = cmap.getOrDefault(s, 0); + if (n == 0) return s; + cmap.put(s, n - 1); + } + return "error"; + } +} \ No newline at end of file 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 diff --git a/README.md b/README.md index 4ab9099..28972ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ # algorithm-practice -2019 코드스쿼드 백엔드 알고리즘 연습 저장소 + +2020 코드스쿼드 백엔드 알고리즘 연습 저장소 + +## 커밋하기 + +- 이 저장소를 포크한다. +- 문제를 풀고 커밋한다. 단 아래의 폴더구조를 따라 문제풀이를 작성한다. + +날짜/GitHubId/문제풀이.확장자 + +예) 20200203/honux77/Runner.java + +- 주석 등을 이용해 문제의 출처를 기입할 것 + +## 푸시 및 풀리퀘 + +- 충돌이 나지 않고 병합이 가능할 것 +- 폴더 구조 규칙을 지킬 것 +- 위 사항을 만족하면 풀리퀘는 리뷰 없이 머지된다. + +## 통계보기 + +- 주기적으로 통계를 통해 자신의 커밋수를 확인하고 열심히 풀어보자 +