-
Notifications
You must be signed in to change notification settings - Fork 122
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
[Tony] WEEK 09 Solutions #518
Changes from all commits
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,25 @@ | ||
// TC: O(log n) | ||
// Using binary search, it takes `log n` time complexity, n indicates the length of the given array nums | ||
// SC: O(1) | ||
// constant space occupation | ||
class Solution { | ||
public int findMin(int[] nums) { | ||
|
||
int start = 0; | ||
int end = nums.length - 1; | ||
int min = Integer.MAX_VALUE; | ||
|
||
while (start <= end) { | ||
int mid = start + (end - start) / 2; | ||
|
||
if (nums[start] <= nums[mid]) { | ||
min = Math.min(min, nums[start]); | ||
start = mid + 1; | ||
} else if (nums[mid] <= nums[end]) { | ||
min = Math.min(min, nums[mid]); | ||
end = mid - 1; | ||
} | ||
} | ||
return min; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
public class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
ListNode slow = head; | ||
ListNode fast = head; | ||
|
||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
if (slow == fast) return true; | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// TC: O(n) | ||
// visit all elements once for each | ||
// SC: O(1) | ||
// constant space occupation | ||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int total = 0; | ||
int output = nums[0]; | ||
|
||
for (int num : nums) { | ||
if (total < 0) total = 0; | ||
total += num; | ||
output = total > output ? total : output; | ||
} | ||
return output; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// TC: O(n) | ||
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. 시간 복잡도를 어떻게 분석하셨는지 설명 좀 부탁드리겠습니다. 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. 외부 while의 경우 s의 길이 만큼 반복하니 |
||
// using two pointer lef and right, it visits all elements only once each. | ||
// SC: O(n + m) | ||
// 2 hashmap used for checking the given Strings s and t, n is the size of s, m is the size of m | ||
class Solution { | ||
public String minWindow(String s, String t) { | ||
Map<Character, Integer> map = new HashMap<>(); | ||
|
||
for (char c : t.toCharArray()) { | ||
map.put(c, map.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
int required = map.size(); | ||
int formed = 0; | ||
|
||
int left = 0; | ||
int right = 0; | ||
int[] ans = {-1, 0, 0}; | ||
|
||
Map<Character, Integer> windowCounts = new HashMap<>(); | ||
|
||
while (right < s.length()) { | ||
char c = s.charAt(right); | ||
windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1); | ||
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. (사소) windowCounts.merge(c, 1, Integer::sum); |
||
|
||
if (map.containsKey(c) && | ||
windowCounts.get(c).intValue() == map.get(c).intValue()) { | ||
formed += 1; | ||
} | ||
|
||
while (left <= right && formed == required) { | ||
c = s.charAt(left); | ||
|
||
if (ans[0] == -1 || right - left + 1 < ans[0]) { | ||
ans[0] = right - left + 1; | ||
ans[1] = left; | ||
ans[2] = right; | ||
} | ||
|
||
windowCounts.put(c, windowCounts.get(c) - 1); | ||
if (map.containsKey(c) && | ||
windowCounts.get(c).intValue() < map.get(c).intValue()) { | ||
formed -= 1; | ||
} | ||
|
||
left += 1; | ||
} | ||
right += 1; | ||
} | ||
return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// TC: O(n * m) | ||
// visit all elements | ||
// SC: O(n * m) | ||
// create result from all elements | ||
class Solution { | ||
public List<List<Integer>> pacificAtlantic(int[][] heights) { | ||
List<List<Integer>> output = new ArrayList<>(); | ||
|
||
if (heights.length == 0 || heights[0].length == 0) return output; | ||
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. nit: 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. 그렇네요! 습관적으로 적은것 같습니다. 주의해야겟네요 감사합니다! |
||
|
||
int rows = heights.length; | ||
int cols = heights[0].length; | ||
|
||
boolean[][] pac = new boolean[rows][cols]; | ||
boolean[][] atl = new boolean[rows][cols]; | ||
|
||
for (int j = 0; j < rows; j++) { | ||
dfs(j, 0, pac, heights[j][0], heights); | ||
dfs(j, cols-1, atl, heights[j][cols-1], heights); | ||
} | ||
|
||
for (int i = 0; i < cols; i++) { | ||
dfs(0, i, pac, heights[0][i], heights); | ||
dfs(rows-1, i, atl, heights[rows-1][i], heights); | ||
} | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (pac[i][j] && atl[i][j]) output.add(List.of(i,j)); | ||
} | ||
} | ||
return output; | ||
} | ||
|
||
private void dfs(int i, int j, boolean[][] visit, int preValue, int[][] heights) { | ||
if (i < 0 || j < 0 || i == heights.length || j == heights[0].length || visit[i][j] || preValue > heights[i][j]) return; | ||
|
||
visit[i][j] = true; | ||
dfs(i + 1, j, visit, heights[i][j], heights); | ||
dfs(i - 1, j, visit, heights[i][j], heights); | ||
dfs(i, j + 1, visit, heights[i][j], heights); | ||
dfs(i, j - 1, visit, heights[i][j], heights); | ||
} | ||
} |
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.
와, 이 알고리즘 굉장히 창의적인 것 같은데 모임 때 소개해주실 수 있으실까요?
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.
헉 ㅋㅋㅋ 다른 문제 이미 발표자료 준비 해 두긴 했는데, 간단하게라도 설명 할 수 있도록 준비해보겠습니다!