-
-
Notifications
You must be signed in to change notification settings - Fork 195
[byteho0n] - Week 7 #934
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
[byteho0n] - Week 7 #934
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,66 @@ | ||
/** | ||
input : String s | ||
output: length of the longest substring without repeating chararcters | ||
|
||
example) | ||
|
||
abccba >> abc, cba >> 3 | ||
abcdeffg >> abcdef >> 6 | ||
constraints | ||
1) length of string s? | ||
[0, 5 * 10^4] | ||
2) alphanumeric characters? | ||
english, digit, symbol, space. >> 256 ascii characters | ||
else >> hashmap | ||
|
||
solution1) | ||
|
||
iterate through string from index i = 0 to n-1 | ||
iterate through string s from index j = i + 1, j to n | ||
if duplicate character comes up, then compare length | ||
tc : O(n^2) | ||
sc : O(1) | ||
|
||
solution 2) sliding window | ||
|
||
ds : array or hashmap << when character range is ascii, we can use array instead | ||
|
||
use two pointer l and r | ||
read until there is no repeating character in range [l, r] | ||
if duplicate exists | ||
move l until every character is unique | ||
ex) | ||
|
||
abaabcdef | ||
l | ||
r | ||
^^ ^ | ||
tc : O(n) | ||
sc : O(1) | ||
|
||
|
||
*/ | ||
|
||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
int[] freq = new int[256]; | ||
int l = 0, r = 0; | ||
int n = s.length(); | ||
int maxLength = 0; | ||
if(n <= 1) return n; | ||
|
||
while(r < n && l <= r) { | ||
char curC = s.charAt(r); | ||
freq[curC]++; | ||
while(l < r && freq[curC] > 1) { | ||
char prevC = s.charAt(l); | ||
freq[prevC]--; | ||
l++; | ||
} | ||
maxLength = Math.max(maxLength, r - l + 1); | ||
r++; | ||
} | ||
maxLength = Math.max(maxLength, r- l); | ||
return maxLength; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
input : 2d matrix filled with '1', '0' | ||
output : number of islands | ||
|
||
1 indicates land, 0 means water. | ||
if lands are surrounded by water we call it island | ||
|
||
example | ||
|
||
1 1 1. >> 3 islands | ||
0 0 0 | ||
1 0 1 | ||
|
||
1 1 1 1 0 | ||
1 1 0 1 0 >> 1 island | ||
0 0 0 0 0 | ||
|
||
constraints: | ||
1) m, n range | ||
[1, 300] | ||
2) can we change the input grid cell? | ||
there is no restriction. | ||
|
||
solution 1) | ||
|
||
ds : array | ||
algo : bfs + in-place | ||
|
||
read every cell in grid. | ||
if cell is '1' | ||
do bfs, change every visiting cell into '2' << if changing input is allowed. | ||
increase count | ||
|
||
tc : O(mn), visiting each cell exactly once. | ||
sc : O(mn) for queue | ||
|
||
optimize?? maintain visited matrix, set or something else.. | ||
if we are allowed to modify given input matrix | ||
we don't have to maintain visited matrix, but simply change values | ||
*/ | ||
class Solution { | ||
public int numIslands(char[][] grid) { | ||
int m = grid.length, n = grid[0].length; | ||
int numberOfIslands = 0; | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < n; j++) { | ||
if(grid[i][j] == '1') { | ||
bfsHelper(grid, i, j, m, n); | ||
numberOfIslands++; | ||
} | ||
} | ||
} | ||
return numberOfIslands; | ||
} | ||
private static int[][] directions = { | ||
{1, 0}, {-1, 0}, {0, 1}, {0, -1} | ||
}; | ||
|
||
private void bfsHelper(char[][] grid, int x, int y, int m, int n) { | ||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.add(new int[]{x,y}); | ||
grid[x][y] = '2'; | ||
while(!queue.isEmpty()) { | ||
int[] cur = queue.poll(); | ||
for(int[] direction : directions) { | ||
int nx = direction[0] + cur[0]; | ||
int ny = direction[1] + cur[1]; | ||
if(nx < 0 || nx >= m || ny < 0 || ny >= n || grid[nx][ny] != '1') continue; | ||
grid[nx][ny] = '2'; | ||
queue.add(new int[]{nx, ny}); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
* | ||
* | ||
* input : head of linked list | ||
* output : reversed linked list | ||
* | ||
* solution1) | ||
* set fast pointer and slow pointer | ||
* iterate through the linked list | ||
* set fast pointer's next to slow pointer | ||
* | ||
* tc : O(n) | ||
* sc : O(1) | ||
*/ | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
if(head == null) return head; | ||
ListNode curr = head; | ||
ListNode prev = null; | ||
while(curr.next != null) { | ||
ListNode next = curr.next; | ||
ekgns33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
curr.next = prev; | ||
return curr; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
|
||
sol1 ) brute force | ||
save zero points, replace row | ||
|
||
read O(mn) | ||
save zero points | ||
replace row,cols | ||
tc : O(mn) | ||
sc : O(mn) | ||
|
||
sol2) use bitwise | ||
length of matrix < integer range | ||
use bit wise | ||
tc : O(mn) | ||
sc : O(m + n) | ||
|
||
sol3) use first row, col as memo | ||
read matrix, check to row or col if zero | ||
|
||
replace if row, col is marked as zero | ||
tc : O(mn) | ||
sc : O(1) in-place | ||
*/ | ||
class Solution { | ||
public void setZeroes(int[][] matrix) { | ||
int m = matrix.length; | ||
int n = matrix[0].length; | ||
boolean firstRowZero = false; | ||
boolean firstColZero = false; | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < n; j++) { | ||
if(matrix[i][j] == 0) { | ||
if(i == 0) firstRowZero = true; | ||
if(j == 0) firstColZero = true; | ||
matrix[i][0] = 0; | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
} | ||
for(int i = 1; i <m; i++) { | ||
for(int j = 1; j < n; j++) { | ||
if(matrix[i][0] == 0 || matrix[0][j] == 0) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
if(firstRowZero) { | ||
Arrays.fill(matrix[0], 0); | ||
} | ||
if(firstColZero) { | ||
for(int j = 0; j <m; j++) { | ||
matrix[j][0] = 0; | ||
} | ||
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
input : 2D matrix | ||
output : the number of possible unique paths | ||
constraints : | ||
1) range of m and n | ||
[1, 100] | ||
|
||
solution 1) dfs? | ||
move right or move down. | ||
tc : O(mn) | ||
sc : O(mn) | ||
|
||
solution 2) dp + tabulation | ||
let dp[i][j] the number of unique paths from starting point. | ||
there are only 2 way to get coordinate i, j | ||
1) from i-1, j | ||
2) from i, j-1 | ||
dp[i][j] = dp[i-1][j] + dp[i][j-1] | ||
|
||
tc : O(mn) | ||
sc : O(mn) | ||
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. 공간복잡도를 O(m)까지 낮춰볼 수 있을까요? 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. 1D 배열로 압축하면 될 것 같네요! 어차피 dp[i-1][j]는 1차원 배열에서 바뀌기 이전값을 의미하니 DP 최적화로 가능할 것 같습니당 :) 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. 굿입니다~ |
||
*/ | ||
|
||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
int[][] dp = new int[m][n]; | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < n; j++) { | ||
if(i == 0 || j == 0) { | ||
dp[i][j] = 1; | ||
continue; | ||
} | ||
dp[i][j] = dp[i][j-1] + dp[i-1][j]; | ||
} | ||
} | ||
return dp[m-1][n-1]; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.