-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hyeri0903] WEEK 09 Solutions #2572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a42ef1d
49d0f66
af83cb2
bae3fa7
a191261
26f755c
fb649e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * class ListNode { | ||
| * int val; | ||
| * ListNode next; | ||
| * ListNode(int x) { | ||
| * val = x; | ||
| * next = null; | ||
| * } | ||
| * } | ||
| */ | ||
| public class Solution { | ||
| public boolean hasCycle(ListNode head) { | ||
| /** | ||
| 1.prob: linked list 이면 true, 아니면 false return | ||
| 2.constraints | ||
| - # of nodes min = 0, max = 10000 | ||
| - pos: -1 or valid index | ||
| - pos는 파라미터로 받지 않음 | ||
| 3.solution | ||
| - pointer 2개를 두고 1개는 2칸씩, 1개는 1칸씩 이동하며 결국 만나는지 체크 | ||
| 만나면 true, 안 만나면 false return | ||
| */ | ||
|
|
||
| ListNode slow = head; | ||
| ListNode fast = head; | ||
|
|
||
| while(fast != null && fast.next != null) { | ||
| slow = slow.next; | ||
| fast = fast.next.next; | ||
|
|
||
| //노드가 동일하면 linked list | ||
| if(slow == fast) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
|
|
||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 순회로 최대값과 최소값을 갱신하며, 각 요소마다 상수 시간 연산을 수행하므로 선형 시간입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class Solution { | ||
| public int maxProduct(int[] nums) { | ||
| /** | ||
| 1.problem: subarray 중에 가장 큰 product return | ||
| 2.constraints | ||
| - num.length min = 1 | ||
| - value min = -10, max=10 | ||
| 3.solutions | ||
| - bruteforce, time: O(n^2), space: O(1) | ||
| - min, max value 추적, time: O(n), space: O(1) | ||
| */ | ||
| int n = nums.length; | ||
| if (n == 1) return nums[0]; | ||
|
|
||
| // int maxValue = Integer.MIN_VALUE; | ||
| // for(int i = 0; i < n; i++) { | ||
| // int curValue = 1; | ||
| // for(int j = i; j < n; j++) { | ||
| // curValue *= nums[j]; | ||
| // maxValue = Math.max(maxValue, curValue); | ||
| // } | ||
| // } | ||
|
|
||
| int max = nums[0]; | ||
| int min = nums[0]; | ||
| int res = nums[0]; | ||
|
|
||
| for(int i = 1; i < n; i++) { | ||
| int cur = nums[i]; | ||
| //현재값이 음수이면 min, max swap | ||
| if(cur < 0) { | ||
| int tmp = min; | ||
| min = max; | ||
| max = tmp; | ||
| } | ||
| max = Math.max(cur, max * cur); | ||
| min = Math.min(cur, min * cur); | ||
|
|
||
| res = Math.max(max, res); | ||
| } | ||
| return res; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 문자 빈도수 배열과 두 포인터를 활용하여 문자열을 한 번씩 이동하며 조건을 만족하는 최소 구간을 찾는 방식으로, 시간 복잡도는 문자열 길이의 합입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| class Solution { | ||
| public String minWindow(String s, String t) { | ||
| /** | ||
| 1.prob: t문자를 포함하는 최소 길이의 substring return. | ||
| 2.constraints | ||
| - 정답은 단 1개 | ||
| - t의 모든 문자를 포함 | ||
| - m,n length min = 1, max = 100000 | ||
| - s,t 는 uppercase or lowercase | ||
| - 정답 없으면 emptystring "" return | ||
| 3.solution | ||
| - brutforce, 모든 substring 구해서 t문자 포함 여부 체크, time complexity: O(n^2) | ||
| - two pointer: right pointer 움직이다가 t문자 모두 포함하면 left pointer 옮기면서 가장 작은 사이즈의 substring 구하기, time compliexty: O(m+n), space: O(1) | ||
| 힌트 봐도 모르겠어서 풀이보고 풀었습니다 ㅜㅜ | ||
| */ | ||
|
|
||
| int m = s.length(); | ||
| int n = t.length(); | ||
| String answer = ""; | ||
|
|
||
| if(m < n) return ""; | ||
|
|
||
| int left = 0, right = 0; | ||
| int count = n; //필요한 문자 수 = t.length() | ||
| int minLen = Integer.MAX_VALUE; | ||
| int start = 0; //조건 만족하는 left pointer 시작 포인트 | ||
|
|
||
| int[] freq = new int[128]; | ||
| for(char c : t.toCharArray()) { | ||
| freq[c]++; | ||
| } | ||
|
|
||
| while(right < m) { | ||
| char r = s.charAt(right); | ||
| //t 문자면 count-- | ||
| if(freq[r] > 0) { | ||
| count --; | ||
| } | ||
| freq[r]--; | ||
| right++; | ||
|
|
||
| //조건 만족하면 left pointer 이동하며 minimum length 찾음 | ||
| while(count == 0) { | ||
| //minLen update | ||
| if(right - left < minLen) { | ||
| minLen = right - left; | ||
| start = left; //minLen 업데이트 시 Left pointer index 저장 | ||
| } | ||
| char l = s.charAt(left); | ||
| freq[l]++; | ||
|
|
||
| //t문자(필요한 문자)이면 count 증가 (원래 값으로 복구) | ||
| if(freq[l] > 0) count++; | ||
| left++; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if(minLen == Integer.MAX_VALUE) { | ||
| answer = ""; | ||
| } else { | ||
| answer = s.substring(start, start + minLen); | ||
| } | ||
| return answer; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 칸에 대해 DFS를 수행하며, 전체 맵 크기만큼 방문 체크를 하므로 시간과 공간 모두 맵 크기에 비례합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| class Solution { | ||
| int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}}; | ||
|
|
||
| public List<List<Integer>> pacificAtlantic(int[][] heights) { | ||
| /** | ||
| 1.2개의 ocean 모두로 도달할 수 있는 칸들을 찾는 문제 | ||
| 2.height 높은 곳 -> 낮은 곳으로 이동 | ||
| time, space: O(mn) | ||
| */ | ||
| int m = heights.length; | ||
| int n = heights[0].length; | ||
| boolean[][] pacific = new boolean[m][n]; | ||
| boolean[][] atlantic = new boolean[m][n]; | ||
|
|
||
| List<List<Integer>> answer = new ArrayList<>(); | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| dfs(i, 0, m, n, heights, pacific); | ||
| dfs(i, n-1, m, n, heights, atlantic); | ||
| } | ||
| for(int j = 0; j < n; j++) { | ||
| dfs(0, j, m, n, heights, pacific); | ||
| dfs(m-1, j, m, n, heights, atlantic); | ||
| } | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| for(int j = 0; j < n; j++) { | ||
| if(pacific[i][j] && atlantic[i][j]) { | ||
| answer.add(Arrays.asList(i,j)); | ||
| } | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
|
|
||
| void dfs(int i, int j, int m, int n, int[][] heights, boolean[][] visited) { | ||
| if(i < 0 || i >= m || j < 0 || j >= n || visited[i][j]) { | ||
| return; | ||
| } | ||
|
|
||
| visited[i][j] = true; | ||
| for(int[] d: dirs) { | ||
| int nexti = i + d[0]; | ||
| int nextj = j + d[1]; | ||
|
|
||
| //범위 안 && 방문 안했고 && 높이 조건 만족하면 다음 루트 탐색 | ||
| if(nexti >= 0 && nexti < m && nextj >= 0 && nextj < n && !visited[nexti][nextj] && heights[nexti][nextj] >= heights[i][j]) { | ||
| dfs(nexti, nextj, m, n, heights, visited); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 반복문이 비트 연산을 통해 자리 올림과 합을 처리하며, 최대 자리수는 정수 크기 제한으로 상수입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public int getSum(int a, int b) { | ||
| /** | ||
| 1.operator 을 쓰지않고 two sum 구하는 문제 | ||
| 2.constraints | ||
| - without using operators + and - | ||
| - value min = -1000, max = 1000 | ||
| 3.solution | ||
| -bit 연산 | ||
| */ | ||
| while (b != 0) { | ||
| int carry = (a & b) << 1; // 자리 올림 | ||
| a = a ^ b; // 자리 올림 없는 합 | ||
| b = carry; // 다음에 더할 carry | ||
| } | ||
| return a; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 포인터 두 개를 이용하여 리스트를 한 번 순회하며 만나는지 체크하는 방식으로, 리스트 길이에 비례하는 시간 복잡도를 갖고, 추가 공간은 상수입니다.
개선 제안: 현재 구현이 적절해 보입니다.