-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from TonyKim9401/main
[Tony] WEEK 09 Solutions
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// TC: O(n) | ||
// 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); | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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); | ||
} | ||
} |