Skip to content

Commit

Permalink
Merge pull request #934 from ekgns33/main
Browse files Browse the repository at this point in the history
[byteho0n] - Week 7
  • Loading branch information
ekgns33 authored Jan 25, 2025
2 parents a51e96e + e4a79e7 commit 21f4334
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
66 changes: 66 additions & 0 deletions longest-substring-without-repeating-characters/ekgns33.java
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;
}
}
74 changes: 74 additions & 0 deletions number-of-islands/ekgns33.java
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});
}
}
}
}
37 changes: 37 additions & 0 deletions reverse-linked-list/ekgns33.java
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;
curr.next = prev;
prev = curr;
curr = next;
}
curr.next = prev;
return curr;
}
}
59 changes: 59 additions & 0 deletions set-matrix-zeroes/ekgns33.java
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;
}
}


}
}
38 changes: 38 additions & 0 deletions unique-paths/ekgns33.java
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)
*/

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];
}
}

0 comments on commit 21f4334

Please sign in to comment.