From 0b3343bf387e5172875aa3557544abcf5f8b0e82 Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Mon, 3 Feb 2020 16:42:30 +0900 Subject: [PATCH 01/17] Update Readme: add commit rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 규칙내용 추가 --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 4ab9099..0d1c94c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ # algorithm-practice + 2019 코드스쿼드 백엔드 알고리즘 연습 저장소 + +## 커밋하기 + +- 이 저장소를 포크한다. +- 문제를 풀고 커밋한다. 단 아래의 폴더구조를 따라 문제풀이를 작성한다. + +날짜/GitHubId/문제풀이.확장자 + +예) 20200203/honux77/Runner.java + +- 주석 등을 이용해 문제의 출처를 기입할 것 + +## 푸시 및 풀리퀘 + +- 충돌이 나지 않고 병합이 가능할 것 +- 폴더 구조 규칙을 지킬 것 +- 위 사항을 만족하면 풀리퀘는 리뷰 없이 머지된다. + +## 통계보기 + +- 주기적으로 통계를 통해 자신의 커밋수를 확인하고 열심히 풀어보자 + From f518e8371e4f4aac8771a5a8ee6cea62ab9bfe4b Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Mon, 3 Feb 2020 16:44:16 +0900 Subject: [PATCH 02/17] [sql] programmers sql lv.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 가장 최신 날짜 구하기 문제 --- 200203/honux77/59415.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 200203/honux77/59415.sql 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; + From 3dc5984f6b009d6415ac2908f4da0fb51e17dd6f Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Mon, 3 Feb 2020 16:46:34 +0900 Subject: [PATCH 03/17] Change year to 2020 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d1c94c..28972ad 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # algorithm-practice -2019 코드스쿼드 백엔드 알고리즘 연습 저장소 +2020 코드스쿼드 백엔드 알고리즘 연습 저장소 ## 커밋하기 From 4d5c8b941a0dd4001896f415715220732a514113 Mon Sep 17 00:00:00 2001 From: IdionKim Date: Tue, 4 Feb 2020 13:44:35 +0900 Subject: [PATCH 04/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EC=B2=B4=EC=9C=A1=EB=B3=B5=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20(#85)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 200203/ksundong/Solution.java | 34 +++++++++++++++++ 200203/ksundong/SolutionTest.java | 63 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 200203/ksundong/Solution.java create mode 100644 200203/ksundong/SolutionTest.java 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, "예상 출력과 다릅니다."); + } +} From 5353120ed89f934d7b540c3530aab3e19d3a463d Mon Sep 17 00:00:00 2001 From: IdionKim Date: Tue, 4 Feb 2020 18:33:27 +0900 Subject: [PATCH 05/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=202016=EB=85=84=20=ED=92=80=EC=9D=B4=20(#87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 200204/ksundong/Solution.java | 20 ++++++++++++++++++++ 200204/ksundong/SolutionTest.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 200204/ksundong/Solution.java create mode 100644 200204/ksundong/SolutionTest.java 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, "원하는 요일명과 일치하지 않습니다."); + } +} From 1764764e35cf1c3e73596b1f40d97b391a6eb6bb Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Tue, 4 Feb 2020 18:34:21 +0900 Subject: [PATCH 06/17] Honux (#88) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [honux] programmers h-index 난이도 3점 문제가 좀 이상하다. 분류는 정렬 * Add 190530 problem BOJ 달팽이 * [소니] 프로그래머스 모의고사 알고리즘 풀이 (#45) * 190524 H_index 알고리즘 풀이 * 프로그래머스 알고리즘 모의고사 문제풀이 * Wangmin 190530 문제풀이 (#57) * [Wangmin] 190523 알고리즘 가장 큰 수 풀이 * [wangmin] programmers 42747 H-index -sort * [wangmin] programmers 42840 모의고사 * [wangmin] baek joon 1913 달팽이 * Su (#46) * Add 190521 problem 프로그래머스 k번째수 * k번째 수 * h번째 인덱스 * mockExam algorithm * feat: H index, 모의고사 풀이 (#47) * [ingleby]190527 algorithm (#48) * [190527] H 알고리즘 문제풀이 제출합니다. (#49) * H-Index 문제풀이 * H-Index 문제풀이 * 모의고사 문제풀이 * [mukeunzi] 190528 모의고사 알고리즘 문제풀이 (#50) * [ruby] 모의고사 보냅니다. (#52) * programmers runner * programmers runner * k번째 수 문제풀이 -map 사용해서 slice 로 answer 에 push * refactor : map 중복제거 * h-index solution * ruby 모의고사 문제풀이 * ruby 모의고사 풀이 * Nailer Solve programmers supoja problem (#53) * Solve programmers supoja problem https://programmers.co.kr/learn/courses/30/lessons/42840 * Solve snail problem 백준 달팽이 알고리즘 문제 해결 * [Jake] 190530 달팽이 알고리즘 문제풀이 (#54) * feat: 19년 5월 21일 PS 솔루션 업데이트 프로그래머스 - K번째 수 * fix: 솔루션 파일 이동 * feat: 19년 5월 22일 PS 솔루션 업데이트 프로그래머스 - 가장 큰 수 * feat: 19년 5월 24일 PS 솔루션 업데이트 프로그래머스 - H-index * fix: H-Index 수정 나머지 논문의 포함 범위를 수정(인용 횟수가 h번 미만) * feat: 19년 5월 27일 PS 솔루션 업데이트 프로그래머스 - 모의고사 * feat: 19년 5월 30일 PS 솔루션 업데이트 백준 - 달팽이 * 프로그래머스 - 모의고사 문제 풀이 (#56) * BOJ 1913 snail (#60) * 달팽이 (#62) * Honux (#63) * BOJ 1913 snail * Update 190611 problem * [sony] algorithm, programmers findPrimeNumber (#65) - 프로그래머스 소수 찾기 문제 풀이 * feat: 19년 6월 12일 PS 솔루션 업데이트 (#67) 프로그래머스 - 소수 찾기(Level2, 완전탐색) * [Programmers - 42839] 소수 찾기 문제 풀이 (#66) * Honux (#68) * BOJ 1913 snail * Update 190611 problem * Update problem 109613 BOJ O(n) sort * feat: 19년 6월 13일 PS 솔루션 업데이트 (#69) 백준 - 수 정렬하기 3(counting sort) * 수 정렬하기3 (#70) * [Sony] BOJ algorithm 10989 (#71) - Counting sort * Honux (#72) * Fail to solve find prime 프로그래머스 문제 풀다 실패 순열로 구했는데 조합이었다. 내일 하자. * Solve programmers find prime 복잡하다... 여튼 풀긴 풀었네. * Add problem 문제 추가 * [H] 소수찾기 문제풀이 제출합니다. (#73) * H-Index 문제풀이 * H-Index 문제풀이 * 소수 찾기 문제풀이 * [wangmin] 수 정렬하기 3 (#74) * Honux (#75) * Fail to solve find prime 프로그래머스 문제 풀다 실패 순열로 구했는데 조합이었다. 내일 하자. * Solve programmers find prime 복잡하다... 여튼 풀긴 풀었네. * Add problem 문제 추가 * [H] 소수찾기 문제풀이 제출합니다. (#73) * H-Index 문제풀이 * H-Index 문제풀이 * 소수 찾기 문제풀이 * [wangmin] 수 정렬하기 3 (#74) * Add 190618 problem * Jake 문제 추가 (#77) * Fail to solve find prime 프로그래머스 문제 풀다 실패 순열로 구했는데 조합이었다. 내일 하자. * Solve programmers find prime 복잡하다... 여튼 풀긴 풀었네. * Add problem 문제 추가 * [H] 소수찾기 문제풀이 제출합니다. (#73) * H-Index 문제풀이 * H-Index 문제풀이 * 소수 찾기 문제풀이 * [wangmin] 수 정렬하기 3 (#74) * Add 190618 problem * Fail to solve 190613 algorithm * Add problems Jake 블로그 관련 문제 추가 * Try to solve BOJ 2580 Sudoku 시간이 많이 걸려서 작성 중 커밋 아직 돌아가지 않는다. * 20024 알고리즘 - 프로그래머스 문자열 압축 - 분류: 완전탐색 - 난이도: 2 - 풀만한데 시간이 좀 걸림 - 더 좋은 방법을 찾아 보자 Co-authored-by: Kong Son Park Co-authored-by: Minjae Lee <0201.mj.lee@gmail.com> Co-authored-by: jeongyongsu5366 <38351131+jeongyongsu5366@users.noreply.github.com> Co-authored-by: jack <47104203+jeuk817@users.noreply.github.com> Co-authored-by: gminiy Co-authored-by: SangYun Ha Co-authored-by: EUNJI NA Co-authored-by: Ruby Lee <48516485+ruby413@users.noreply.github.com> Co-authored-by: SeongHeum CHOI Co-authored-by: Jake Co-authored-by: JangHyoSeok --- .gitignore | 3 ++ 190624/2580-sudoku.cpp | 54 ++++++++++++++++++++++++++++++++++++ 190624/input.txt | 9 ++++++ 200204/60057-Solution.java | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 190624/2580-sudoku.cpp create mode 100644 190624/input.txt create mode 100644 200204/60057-Solution.java 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/200204/60057-Solution.java b/200204/60057-Solution.java new file mode 100644 index 0000000..aec7a94 --- /dev/null +++ b/200204/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; + } +} + From b75d8c2a601d77429cdac85ec8459e701e324d63 Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Tue, 4 Feb 2020 18:55:59 +0900 Subject: [PATCH 07/17] =?UTF-8?q?[programmers-sql]=20=EC=97=AD=EC=88=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A0=AC=ED=95=98=EA=B8=B0=20(#90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 진짜 쉬운 SQL 문제 풀이 --- 200204/honux/59035.sql | 2 ++ 200204/{ => honux}/60057-Solution.java | 0 2 files changed, 2 insertions(+) create mode 100644 200204/honux/59035.sql rename 200204/{ => honux}/60057-Solution.java (100%) 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/60057-Solution.java b/200204/honux/60057-Solution.java similarity index 100% rename from 200204/60057-Solution.java rename to 200204/honux/60057-Solution.java From b95c0a615736a6618d6648d39b51b8831e4e74e2 Mon Sep 17 00:00:00 2001 From: Dong Hwan Date: Wed, 5 Feb 2020 10:04:57 +0900 Subject: [PATCH 08/17] =?UTF-8?q?[=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4]=20top=EB=AC=B8=EC=A0=9C=20(#93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Be2020 (#84) * Update Readme: add commit rule - 규칙내용 추가 * [sql] programmers sql lv.1 가장 최신 날짜 구하기 문제 * Change year to 2020 * Test Commit * TestCommit 삭제, 프로그래머스 Top 문제 해결 * Revert "TestCommit 삭제, 프로그래머스 Top 문제 해결" This reverts commit 71ce751c23eeee8f2c8bb3a44a8f85a509ddf923. * 브랜치 수정, Top commit Co-authored-by: Hoyoung Jung --- 200204/102092/Top.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 200204/102092/Top.java 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 From a8b816400484fa8bd3caa7c24ab18c92fa14e51f Mon Sep 17 00:00:00 2001 From: IdionKim Date: Wed, 5 Feb 2020 10:06:07 +0900 Subject: [PATCH 09/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EA=B0=80=EC=9A=B4=EB=8D=B0=20=EA=B8=80?= =?UTF-8?q?=EC=9E=90=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20(#91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ksundong/GetMiddleCharacterSolution.java | 14 +++++++++ .../GetMiddleCharacterSolutionTest.java | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 200204/ksundong/GetMiddleCharacterSolution.java create mode 100644 200204/ksundong/GetMiddleCharacterSolutionTest.java 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, "대상 글자는 가운데 글자가 아닙니다."); + } +} From e2907cb6d57790c64bbfef5ecda0ce1bff4bcb69 Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Wed, 5 Feb 2020 23:27:25 +0900 Subject: [PATCH 10/17] =?UTF-8?q?Programmers=2012093=20=EA=B0=80=EC=9A=B4?= =?UTF-8?q?=EB=8D=B0=20=EA=B8=80=EC=9E=90=20=EA=B0=80=EC=A0=B8=EC=98=A4?= =?UTF-8?q?=EA=B8=B0=20(#94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - java의 substring(s,e)의 특징을 이해하기에 좋다. - substring으로 한글자를 자르려면substring(s, s + 1) - endIndex는 포함되지 않는다. 유익 --- 200205/honux77/programmers-12903-Solution.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 200205/honux77/programmers-12903-Solution.java 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; + } +} From 7d59f5b854704a85d0506adb361ff5b175be0a64 Mon Sep 17 00:00:00 2001 From: Dong Hwan Date: Wed, 5 Feb 2020 23:28:10 +0900 Subject: [PATCH 11/17] =?UTF-8?q?[=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4]=20=EB=AA=A8=EC=9D=98=EA=B3=A0=EC=82=AC=20(#?= =?UTF-8?q?95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Be2020 (#84) * Update Readme: add commit rule - 규칙내용 추가 * [sql] programmers sql lv.1 가장 최신 날짜 구하기 문제 * Change year to 2020 * Test Commit * TestCommit 삭제, 프로그래머스 Top 문제 해결 * Revert "TestCommit 삭제, 프로그래머스 Top 문제 해결" This reverts commit 71ce751c23eeee8f2c8bb3a44a8f85a509ddf923. * 브랜치 수정, Top commit * Programmers 모의고사 세가지 변수중에 최대값을 찾는 방법. Arraylist to List 바꾸는 방법을 찾을수 있었음. 다만 stream()으로 하는 방법은 성능상은 별로인듯함. Co-authored-by: Hoyoung Jung --- 200205/102092/MockTest.java | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 200205/102092/MockTest.java 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 From 5abc9a098c67e7519adbf7212ed720aad0a07ebb Mon Sep 17 00:00:00 2001 From: IdionKim Date: Wed, 5 Feb 2020 23:30:23 +0900 Subject: [PATCH 12/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EA=B0=99=EC=9D=80=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=EB=8A=94=20=EC=8B=AB=EC=96=B4=20=ED=92=80=EC=9D=B4=20(#96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ksundong/NoDuplicateNumber/Solution.java | 30 +++++++++++++++++++ .../NoDuplicateNumber/SolutionTest.java | 29 ++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 200205/ksundong/NoDuplicateNumber/Solution.java create mode 100644 200205/ksundong/NoDuplicateNumber/SolutionTest.java 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, "에상한 배열과 일치하지 않습니다."); + } +} From aa278ecdfd7379008a9f4fdd3cfb6282b38a82b2 Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Fri, 7 Feb 2020 09:12:15 +0900 Subject: [PATCH 13/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EC=99=84=EC=A3=BC=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EB=AA=BB=ED=95=9C=20=EC=84=A0=EC=88=98=20(#99)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 스트림으로 풀었다가 느려서 그냥 이터레이션으로 바꿈 분류: 해시 난이도: 2점 --- .../honux77/programmers-42576-Solution.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 200206/honux77/programmers-42576-Solution.java 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 From 4a98cd661d05f66951da8eeea67a8139d6413130 Mon Sep 17 00:00:00 2001 From: IdionKim Date: Fri, 7 Feb 2020 09:20:10 +0900 Subject: [PATCH 14/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EB=82=98=EB=88=84=EC=96=B4=20=EB=96=A8?= =?UTF-8?q?=EC=96=B4=EC=A7=80=EB=8A=94=20=EC=88=AB=EC=9E=90=20=EB=B0=B0?= =?UTF-8?q?=EC=97=B4=20=ED=92=80=EC=9D=B4=20(#98)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DividingNumberArray/Solution.java | 24 +++++++++++ .../DividingNumberArray/SolutionTest.java | 40 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 200205/ksundong/DividingNumberArray/Solution.java create mode 100644 200205/ksundong/DividingNumberArray/SolutionTest.java 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, "원하는 배열이 출력되지 않았습니다."); + } +} From 871b8024cdb7d06341d9549153abe63ba2015806 Mon Sep 17 00:00:00 2001 From: Hoyoung Jung Date: Fri, 7 Feb 2020 23:00:38 +0900 Subject: [PATCH 15/17] =?UTF-8?q?=EC=99=84=EC=A3=BC=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EB=AA=BB=ED=95=9C=20=EC=84=A0=EC=88=98=203=EC=B0=A8=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 프로그래머스 다시 풀기 HashMap의 getOrDefault(k, v)를 써서 약간 깔끔해졌다. * 완주하지 못한 선수 - 정렬로 풀었음 현욱이가 해시 쓰지 말라고 해서 정렬로 풀었음. --- .../honux77/programmers-42576-Solution-2.java | 18 ++++++++++++++++ .../honux77/programmers-42576-Solution.java | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 200207/honux77/programmers-42576-Solution-2.java create mode 100644 200207/honux77/programmers-42576-Solution.java 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 From d981de810ab22d426a46eaaf3f5d4daef53af359 Mon Sep 17 00:00:00 2001 From: Dong Hwan Date: Wed, 12 Feb 2020 19:28:51 +0900 Subject: [PATCH 16/17] =?UTF-8?q?=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=EB=A8=B8=EC=8A=A4=20=EC=B2=B4=EC=9C=A1=EB=B3=B5,=20groupBy1=20?= =?UTF-8?q?sql=20(#101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Be2020 (#84) * Update Readme: add commit rule - 규칙내용 추가 * [sql] programmers sql lv.1 가장 최신 날짜 구하기 문제 * Change year to 2020 * Test Commit * TestCommit 삭제, 프로그래머스 Top 문제 해결 * Revert "TestCommit 삭제, 프로그래머스 Top 문제 해결" This reverts commit 71ce751c23eeee8f2c8bb3a44a8f85a509ddf923. * 브랜치 수정, Top commit * Programmers 모의고사 세가지 변수중에 최대값을 찾는 방법. Arraylist to List 바꾸는 방법을 찾을수 있었음. 다만 stream()으로 하는 방법은 성능상은 별로인듯함. * Test Commit * 브랜치 수정, Top commit * 프로그래머스 체육복, sql (GroupBy1) Co-authored-by: Hoyoung Jung --- 200211/102092/groupby1.sql | 5 ++ 200211/102092/workoutClothes.java | 85 +++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 200211/102092/groupby1.sql create mode 100644 200211/102092/workoutClothes.java 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); + } +} From c8a8fddcd2125c0b9ea8d1c49823cd651a9ee742 Mon Sep 17 00:00:00 2001 From: Han Date: Thu, 15 Oct 2020 14:21:25 +0900 Subject: [PATCH 17/17] programmer sql , Leetcode (#102) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Be2020 (#84) * Update Readme: add commit rule - 규칙내용 추가 * [sql] programmers sql lv.1 가장 최신 날짜 구하기 문제 * Change year to 2020 * Test Commit * TestCommit 삭제, 프로그래머스 Top 문제 해결 * Revert "TestCommit 삭제, 프로그래머스 Top 문제 해결" This reverts commit 71ce751c23eeee8f2c8bb3a44a8f85a509ddf923. * 브랜치 수정, Top commit * Programmers 모의고사 세가지 변수중에 최대값을 찾는 방법. Arraylist to List 바꾸는 방법을 찾을수 있었음. 다만 stream()으로 하는 방법은 성능상은 별로인듯함. * Test Commit * 브랜치 수정, Top commit * 프로그래머스 체육복, sql (GroupBy1) * 0212, 0213 Leet + Programmers , sql * IsNull, ValidParentheess HashMap + Stack * Leetcode : MergeSortedList, Programmers : IsNull * LeetCode : MergeSortedArray, SQL : IsNull3 Arrays.sort() 사용 IFNUll(.., setValue) * 프로그래머스 : 라면공장, Join1 * 0220 : Leetcode, SQl , Programmers; 기본 BFS, Join Datetime 비교, 프로그래머스 가운데 글자 가져오기. 가장 무식하게 * 0221 : sql 1, easy-2 * 0224 : join4, leetcode easy 1, programmers level2 1 * 0225 : sql1, leetcode1, programmer1 * 0226 : programmers, sql1 * 0227 : leetcode 1, programmer sql1 * 0228 : leetcode1, sql1 * 0301 : 프로그래머스 lv1 2문제, sql 1문제 * 0302 : leetcode 3 * 0304 : Leetcode 2, programmers 1 * 0305 : 2문제 Co-authored-by: Hoyoung Jung --- 200212/102092/MoreSpicy.java | 31 +++++++++++++ 200212/102092/groupby2.sql | 11 +++++ 200213/102092/RomanToInteger.java | 40 ++++++++++++++++ 200213/102092/groupby3.sql | 5 ++ 200214/102092/IsNull.sql | 5 ++ 200214/102092/ValidParentheses.java | 37 +++++++++++++++ 200217/102092/IsNull2.sql | 5 ++ 200217/102092/MergeTwoSorted.java | 66 +++++++++++++++++++++++++++ 200218/102092/IsNull3.sql | 5 ++ 200218/102092/MergeSortedArray.java | 36 +++++++++++++++ 200219/102092/Join1.sql | 7 +++ 200219/102092/RamenFactory.java | 40 ++++++++++++++++ 200220/102092/Join2.sql | 10 ++++ 200220/102092/NumberOfIsland.java | 31 +++++++++++++ 200220/102092/getMiddleString.java | 16 +++++++ 200221/102092/2016.java | 20 ++++++++ 200221/102092/Contains-duplicate.java | 29 ++++++++++++ 200221/102092/Join3.sql | 11 +++++ 200224/102092/Join4.sql | 10 ++++ 200224/102092/PascalsTriangle.java | 32 +++++++++++++ 200224/102092/TruckOnBridge1.java | 62 +++++++++++++++++++++++++ 200225/102092/String1.sql | 8 ++++ 200225/102092/TwoSum2.java | 22 +++++++++ 200225/FindPrimeNumber.java | 30 ++++++++++++ 200226/102092/NotSameNumber.java | 21 +++++++++ 200226/102092/String2.sql | 7 +++ 200227/102092/LinkedListCycle1.java | 30 ++++++++++++ 200227/102092/String3.sql | 10 ++++ 200228/102092/MinStack.java | 49 ++++++++++++++++++++ 200228/102092/MinStack2.java | 40 ++++++++++++++++ 200228/102092/String3.sql | 6 +++ 200301/102092/CompareStrings.java | 25 ++++++++++ 200301/102092/DivisionArray.java | 30 ++++++++++++ 200301/102092/String4.sql | 10 ++++ 200302/102092/Intersection.java | 41 +++++++++++++++++ 200302/102092/Postorder.java | 47 +++++++++++++++++++ 200302/102092/Preorder.java | 52 +++++++++++++++++++++ 200304/102092/CaesarCipher.java | 24 ++++++++++ 200304/102092/MajorityElement.java | 27 +++++++++++ 200304/102092/MergeTrees.java | 30 ++++++++++++ 200305/102092/FactorialZeroes.java | 16 +++++++ 200305/102092/StrangeWord.java | 29 ++++++++++++ 42 files changed, 1063 insertions(+) create mode 100644 200212/102092/MoreSpicy.java create mode 100644 200212/102092/groupby2.sql create mode 100644 200213/102092/RomanToInteger.java create mode 100644 200213/102092/groupby3.sql create mode 100644 200214/102092/IsNull.sql create mode 100644 200214/102092/ValidParentheses.java create mode 100644 200217/102092/IsNull2.sql create mode 100644 200217/102092/MergeTwoSorted.java create mode 100644 200218/102092/IsNull3.sql create mode 100644 200218/102092/MergeSortedArray.java create mode 100644 200219/102092/Join1.sql create mode 100644 200219/102092/RamenFactory.java create mode 100644 200220/102092/Join2.sql create mode 100644 200220/102092/NumberOfIsland.java create mode 100644 200220/102092/getMiddleString.java create mode 100644 200221/102092/2016.java create mode 100644 200221/102092/Contains-duplicate.java create mode 100644 200221/102092/Join3.sql create mode 100644 200224/102092/Join4.sql create mode 100644 200224/102092/PascalsTriangle.java create mode 100644 200224/102092/TruckOnBridge1.java create mode 100644 200225/102092/String1.sql create mode 100644 200225/102092/TwoSum2.java create mode 100644 200225/FindPrimeNumber.java create mode 100644 200226/102092/NotSameNumber.java create mode 100644 200226/102092/String2.sql create mode 100644 200227/102092/LinkedListCycle1.java create mode 100644 200227/102092/String3.sql create mode 100644 200228/102092/MinStack.java create mode 100644 200228/102092/MinStack2.java create mode 100644 200228/102092/String3.sql create mode 100644 200301/102092/CompareStrings.java create mode 100644 200301/102092/DivisionArray.java create mode 100644 200301/102092/String4.sql create mode 100644 200302/102092/Intersection.java create mode 100644 200302/102092/Postorder.java create mode 100644 200302/102092/Preorder.java create mode 100644 200304/102092/CaesarCipher.java create mode 100644 200304/102092/MajorityElement.java create mode 100644 200304/102092/MergeTrees.java create mode 100644 200305/102092/FactorialZeroes.java create mode 100644 200305/102092/StrangeWord.java 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