From 2c5b3dc7fcb87447a8b96d4a7ddd553da2601385 Mon Sep 17 00:00:00 2001 From: Jisoo Kim Date: Thu, 4 Jan 2024 21:14:38 +0900 Subject: [PATCH 1/5] =?UTF-8?q?42586=5F=EA=B9=80=EC=A7=80=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\271\200\354\247\200\354\210\230.java" | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 "4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" diff --git "a/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" new file mode 100644 index 0000000..c4a5b37 --- /dev/null +++ "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" @@ -0,0 +1,159 @@ +// Solution 1 +import java.util.*; +class Solution { + public int[] solution(int[] progresses, int[] speeds) { + + int[] daysLeft = new int[speeds.length]; + + for (int i = 0; i < speeds.length; i++) { + // 자바에서 정수끼리 나눌 때 소수점 아래 버려지는 것 주의! + daysLeft[i] = (int) Math.ceil( (double) (100 - progresses[i]) / speeds[i] ); + System.out.println(daysLeft[i]); + } + + Stack answerList = new Stack<>(); + int max = 0; + + for (int i = 0; i < daysLeft.length; i++) { + if (max < daysLeft[i]) { + max = daysLeft[i]; + answerList.push(1); + continue; + } else if (max >= daysLeft[i]) { + if (!answerList.isEmpty()) { + int lastCnt = answerList.pop(); + lastCnt++; + answerList.push(lastCnt); + } + } + } + + int[] answer = new int[answerList.size()]; + for (int i = 0; i < answerList.size(); i++) { + answer[i] = answerList.get(i); + } + + return answer; + } +} + + +// Solution 2 +import java.util.*; +class Solution { + public int[] solution(int[] progresses, int[] speeds) { + + Queue q = new LinkedList<>(); + List answerList = new ArrayList<>(); + + for (int i = 0; i < speeds.length; i++) { + double remain = (100 - progresses[i]) / (double) speeds[i]; + int date = (int) Math.ceil(remain); + + if (!q.isEmpty() && q.peek() < date) { + answerList.add(q.size()); + q.clear(); + } + + q.offer(date); + } + + answerList.add(q.size()); + + int[] answer = new int[answerList.size()]; + + for (int i = 0; i < answer.length; i++) { + answer[i] = answerList.get(i); + } + + return answer; + } +} + + +// Solution 3 모듈화 코드인데..어렵다.. +import java.lang.System; + import java.lang.Math; + import java.util.ArrayList; +class Solution { + + int progressesCount; + int[] needDays; + + ArrayList workCountStorage; + + public int[] solution(int[] progresses, int[] speeds) { + + //Init + progressesCount = progresses.length; + needDays = new int[progressesCount]; + workCountStorage = new ArrayList<>(); + + + //필요한 작업일 계산 + this.calcNeedDays(progresses, speeds); + + //this.viewAll(needDays, 0); + + + //동시에 진행된 프로세스 계산 + for (int step = 0; step < progressesCount; ) { + int stepNeedDay = needDays[step]; + + //날짜 동시에 경과 + for (int remainStep = step; remainStep < progressesCount; remainStep++) { + needDays[remainStep] -= stepNeedDay; + } + + //this.viewAll(needDays, step); + + //완료한 작업까지의 갯수 + int workCount = 1; + for ( ; step + workCount < progressesCount; workCount++) { + if (needDays[step + workCount] > 0) break; + } + + System.out.println("workCount:" + workCount); + + //완료한 작업 갯수 저장 + workCountStorage.add(workCount); + + //작업 갯수만큼 step 증가 + step += workCount; + + } + + //int[] answer = {}; + int[] answer = Solution.convertIntegers(workCountStorage); + return answer; + } + + private void calcNeedDays(int[] progresses, int[] speeds) { + for (int i = 0; i < progressesCount; i++) { + double remainProgress = 100 - progresses[i]; + double fNeedDay = remainProgress / speeds[i]; + + needDays[i] = (int) Math.ceil(fNeedDay); + } + } + + public static int[] convertIntegers(ArrayList integers) { + int size = integers.size(); + int[] ret = new int[size]; + for (int i = 0; i < size; i++) { + ret[i] = integers.get(i).intValue(); + } + return ret; + } + + private void viewAll(int[] array, int startIdx) { + System.out.print("viewAll:"); + + int arrayCount = array.length; + for(int i = startIdx; i < arrayCount; i++) { + System.out.print(array[i]+","); + } + + System.out.println(); + } +} \ No newline at end of file From 78833f9cfb9d85ddb56d34deed16e653d9fff057 Mon Sep 17 00:00:00 2001 From: Jisoo Kim Date: Fri, 5 Jan 2024 12:43:21 +0900 Subject: [PATCH 2/5] =?UTF-8?q?43165=5F=EA=B9=80=EC=A7=80=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\271\200\354\247\200\354\210\230.java" | 36 +++++++++++++++++- ...\352\271\200\354\247\200\354\210\230.java" | 38 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 "4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" diff --git "a/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" index c4a5b37..e174a44 100644 --- "a/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" +++ "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" @@ -71,7 +71,41 @@ public int[] solution(int[] progresses, int[] speeds) { } -// Solution 3 모듈화 코드인데..어렵다.. +// Solution 3 +import java.util.*; + import java.util.stream.*; + +class Solution { + public int[] solution(int[] progresses, int[] speeds) { + int[] arr = new int[progresses.length]; + int length = progresses.length; + + for(int i=0; i < length; i++) { + arr[i] = (int)Math.ceil((100 - progresses[i]) / (double)speeds[i]); + } + + List list = new ArrayList<>(); + int pre = arr[0]; + int idx = 0; + + for(int i=1; i < length; i++) { + if(arr[i] <= pre) { + continue; + } + + list.add(i - idx); + pre = arr[i]; + idx = i; + } + + list.add(length - idx); + + return list.stream().mapToInt(i->i).toArray(); + } +} + + +// Solution 4 모듈화 코드인데..어렵다.. import java.lang.System; import java.lang.Math; import java.util.ArrayList; diff --git "a/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" new file mode 100644 index 0000000..74bd09c --- /dev/null +++ "b/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" @@ -0,0 +1,38 @@ +// Solution 1 +class Solution { + int answer = 0; + + public int solution(int[] numbers, int target) { + dfs(numbers, 0, target, 0); + return answer; + } + + public void dfs(int[] numbers, int depth, int target, int sum) { + if(depth == numbers.length) { + if (target == sum) answer++; + } else { + dfs(numbers, depth + 1, target, sum + numbers[depth]); + dfs(numbers, depth + 1, target, sum - numbers[depth]); + } + } +} + + +// Solution 2 이해 어려우니까 다시 보기 +class Solution { + public int solution(int[] numbers, int target) { + int answer = 0; + answer = dfs(numbers, 0, 0, target); + return answer; + } + + int dfs(int[] numbers, int n, int sum, int target) { + if(n == numbers.length) { + if(sum == target) { + return 1; + } + return 0; + } + return dfs(numbers, n + 1, sum + numbers[n], target) + dfs(numbers, n + 1, sum - numbers[n], target); + } +} \ No newline at end of file From 0e623677fa66d6b0d29decf27ae44494391d76bf Mon Sep 17 00:00:00 2001 From: Jisoo Kim Date: Fri, 5 Jan 2024 15:25:55 +0900 Subject: [PATCH 3/5] =?UTF-8?q?42885=5F=EA=B9=80=EC=A7=80=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\271\200\354\247\200\354\210\230.java" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "4\354\243\274\354\260\250/42885/42885_\352\271\200\354\247\200\354\210\230.java" diff --git "a/4\354\243\274\354\260\250/42885/42885_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/42885/42885_\352\271\200\354\247\200\354\210\230.java" new file mode 100644 index 0000000..bbd3d13 --- /dev/null +++ "b/4\354\243\274\354\260\250/42885/42885_\352\271\200\354\247\200\354\210\230.java" @@ -0,0 +1,42 @@ +// Solution 1 +import java.util.*; +class Solution { + public int solution(int[] people, int limit) { + Arrays.sort(people); + + int start = 0; + int end = people.length - 1; + int answer = 0; + + while (start <= end) { + if (people[start] + people[end] <= limit) { + answer++; + start++; + end--; + } else { + answer++; + end--; + } + } + + return answer; + } +} + + +// Solution 2 +import java.util.Arrays; +class Solution { + public int solution(int[] people, int limit) { + Arrays.sort(people); + int i = 0, j = people.length - 1; + + for ( ; i < j; --j) { + if (people[i] + people[j] <= limit) + ++i; + } + + return people.length - i; + // 사람 수 - 두 명 태우는 경우의 수 => 한 명 태우는 경우의 수 + 두 명 태우는 경우의 수 + } +} \ No newline at end of file From c1ea8bc2cb608ce6a86ca9489636b8d08335a1ba Mon Sep 17 00:00:00 2001 From: Jisoo Kim Date: Fri, 5 Jan 2024 15:26:23 +0900 Subject: [PATCH 4/5] =?UTF-8?q?64065=5F=EA=B9=80=EC=A7=80=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\271\200\354\247\200\354\210\230.java" | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 "4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" diff --git "a/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" new file mode 100644 index 0000000..62438c8 --- /dev/null +++ "b/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" @@ -0,0 +1,128 @@ +// Solution 1 + +import java.util.*; + +class Solution { + public int[] solution(String s) { + // 문자열 s를 배열 arr로 만들어주기 + s = s.substring(2, s.length() - 2).replace("},{", " "); + String[] arr = s.split(" "); + + // 배열 arr 안의 문자열 원소를 length 기준으로 오름차순 정렬 + Arrays.sort(arr, Comparator.comparingInt(str -> str.length())); + + // 1차 결과를 담을 빈 answer 배열 생성 + ArrayList answerList = new ArrayList<>(); + + // 정렬된 배열 arr 안의 원소를 차례로 돌며 각 문자열 안의 , 제거 + for (String str : arr) { + String[] strArr = str.split(","); + // 위에서 만든 answer에 원소 추가해주기 + for (String num : strArr) { + int n = Integer.parseInt(num); + if (!answerList.contains(n)) { + answerList.add(n); + } + } + } + // ArrayList를 int[]로 변환 + int[] answer = new int[answerList.size()]; + for (int i = 0; i < answerList.size(); i++) { + answer[i] = answerList.get(i); + } + + return answer; + + } +} +/* +입력 문자열 파싱: O(N) +배열 정렬: O(N log N) +배열 순회: O(N) +원소를 답 리스트에 추가하는 중첩 루프: O(N) +총 시간 복잡도: O(N log N) + */ + + +// Solution 2 +import java.util.*; +class Solution { + public int[] solution(String s) { + Set set = new HashSet<>(); + String[] arr = s.replaceAll("[{]", " ").replaceAll("[}]", " ").trim().split(" , "); + + Arrays.sort(arr, (a, b) -> { return a.length() - b.length();} ); + + int[] answer = new int[arr.length]; + int idx = 0; + for(String s1 : arr) { + for(String s2 : s1.split(",")) { + if(set.add(s2)) answer[idx++] = Integer.parseInt(s2); + } + } + return answer; + } +} +/* +입력 문자열 내의 문자 대체: O(N) +입력 문자열을 배열로 파싱: O(N) +배열 정렬: O(N log N) +배열 순회 및 중첩 루프로 원소를 답 배열에 추가: O(N) +총 시간 복잡도: O(N log N) + */ + + +// Solution 3 => 정렬이 없어서 가장 빠름! +import java.util.*; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +class Solution { + public int[] solution(String s) { + + Map map = new HashMap<>(); + Pattern pattern = Pattern.compile("[0-9]+"); + Matcher matcher = pattern.matcher(s); + + while (matcher.find()) { + String n = matcher.group(); + map.put(n, map.getOrDefault(n, 0) + 1); + } + + int size = map.size(); + int[] answer = new int[size]; + + for (String key : map.keySet()) { + answer[size - map.get(key)] = Integer.parseInt(key); + } + + return answer; + } +} +/* +정규식을 사용하여 입력 문자열에서 숫자 찾기: O(N) +고유한 숫자 및 그 빈도로 구성된 맵 생성: O(N) +맵에서 답 배열 작성: O(N) +총 시간 복잡도: O(N) + */ + + +// Solution 4 +import java.util.*; +import java.util.stream.Stream; +class Solution { + final Map counts = new LinkedHashMap<>(); + + public int[] solution(String s) { + Stream.of(s.replaceAll("[}{]", "").split(",")).mapToInt(Integer::parseInt) + .forEach(i -> counts.merge(i, 1, Integer::sum)); + return counts.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) + .map(Map.Entry::getKey).mapToInt(x -> x).toArray(); + } +} +/* +입력 문자열에서 불필요한 문자 제거: O(N) +스트림을 사용하여 배열 처리 및 맵 작성: O(N) +맵 정렬: O(N log N) +정렬된 맵에서 답 배열 작성: O(N) +총 시간 복잡도: O(N log N) + */ \ No newline at end of file From 2195571ec0defeed279504539d97e11afd220d59 Mon Sep 17 00:00:00 2001 From: Jisoo Kim Date: Tue, 9 Jan 2024 12:20:15 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=ED=92=80=EC=9D=B4=20=ED=9B=94=EC=B9=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\271\200\354\247\200\354\210\230.java" | 25 +++++++++++++++- ...\352\271\200\354\247\200\354\210\230.java" | 21 ++++++++++++++ ...\352\271\200\354\247\200\354\210\230.java" | 24 +++++++++++++++ ...\352\271\200\354\247\200\354\210\230.java" | 29 +++++++++++++++++-- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git "a/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" index e174a44..78e1bd8 100644 --- "a/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" +++ "b/4\354\243\274\354\260\250/42586/42586_\352\271\200\354\247\200\354\210\230.java" @@ -73,7 +73,7 @@ public int[] solution(int[] progresses, int[] speeds) { // Solution 3 import java.util.*; - import java.util.stream.*; +import java.util.stream.*; class Solution { public int[] solution(int[] progresses, int[] speeds) { @@ -190,4 +190,27 @@ private void viewAll(int[] array, int startIdx) { System.out.println(); } +} + + +// Solution 5 +import java.util.*; +class Solution { + public int[] solution(int[] progresses, int[] speeds) { + Queue works = new LinkedList<>(); + List result = new ArrayList<>(); + for (int i=0; i 한 명 태우는 경우의 수 + 두 명 태우는 경우의 수 } +} + + +// Solution 3 +import java.util.*; +class Solution { + public int solution(int[] people, int limit) { + int answer = 0; + Arrays.sort(people); + int j = 0; + for (int i = people.length - 1; i >= 0; i--) { + if (j > i) { + break; + } + if (people[i] + people[j] <= limit) { + j++; + } + answer++; + } + return answer; + } } \ No newline at end of file diff --git "a/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" index 74bd09c..19e05c8 100644 --- "a/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" +++ "b/4\354\243\274\354\260\250/43165/43165_\352\271\200\354\247\200\354\210\230.java" @@ -35,4 +35,28 @@ int dfs(int[] numbers, int n, int sum, int target) { } return dfs(numbers, n + 1, sum + numbers[n], target) + dfs(numbers, n + 1, sum - numbers[n], target); } +} + + +// Solution 3 +class Solution { + static int goal; + static int answer=0; + static int[] arr; + public int solution(int[] numbers, int target) { + arr = numbers; + goal = target; + dfs(0, 0, numbers); + return answer; + } + private static void dfs(int depth, int sum, int[] numbers) { + if (depth == numbers.length) { + if (sum == goal) { + answer++; + } + return; + } + dfs(depth + 1, sum + arr[depth], numbers); + dfs(depth + 1, sum - arr[depth], numbers); + } } \ No newline at end of file diff --git "a/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" "b/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" index 62438c8..4761924 100644 --- "a/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" +++ "b/4\354\243\274\354\260\250/64065/64065_\352\271\200\354\247\200\354\210\230.java" @@ -46,6 +46,31 @@ public int[] solution(String s) { // Solution 2 import java.util.*; +class Solution { + public ArrayList solution(String s) { + ArrayList answer = new ArrayList<>(); + String str[] = s.substring(2, s.length() - 2).replace("},{", "-").split("-"); + Arrays.sort(str, new Comparator() { + public int compare(String o1, String o2) { + + return Integer.compare(o1.length(), o2.length()); + } + }); + for (String x : str) { + String[] temp = x.split(","); + for (int i = 0; i < temp.length; i++) { + int n = Integer.parseInt(temp[i]); + if (!answer.contains(n)) + answer.add(n); + } + } + return answer; + } +} + + +// Solution 3 +import java.util.*; class Solution { public int[] solution(String s) { Set set = new HashSet<>(); @@ -72,7 +97,7 @@ public int[] solution(String s) { */ -// Solution 3 => 정렬이 없어서 가장 빠름! +// Solution 4 => 정렬이 없어서 가장 빠름! import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; @@ -106,7 +131,7 @@ public int[] solution(String s) { */ -// Solution 4 +// Solution 5 import java.util.*; import java.util.stream.Stream; class Solution {