Skip to content

[byteho0n] Week 8 #978

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

Merged
merged 5 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions clone-graph/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/

class Solution {
public Node cloneGraph(Node node) {
Map<Integer, Node> nodeMap = new HashMap<>();
if(node == null) return null;
boolean[] visited = new boolean[101];
dfsHelper(nodeMap, node, visited);
return nodeMap.get(1);
}

private void dfsHelper(Map<Integer, Node> map, Node node, boolean[] visited) {
if(!map.containsKey(node.val)) {
map.put(node.val, new Node(node.val));
}
visited[node.val] = true;
Node cur = map.get(node.val);
for(Node adjacent : node.neighbors) {
map.putIfAbsent(adjacent.val, new Node(adjacent.val));
cur.neighbors.add(map.get(adjacent.val));
if(!visited[adjacent.val]) dfsHelper(map, adjacent, visited);
}
}
}
76 changes: 76 additions & 0 deletions longest-common-subsequence/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
input : string text1, text2
output : length of lcs else 0
constraints:
1) both input strings are not empty
2) input strings are consist of lowercase eng. letters

solution 1) brute force

nested for loop
tc : O(n^2 * m) where n is min (len(text1), len(text2)), m is max
sc : O(1)

solution 2) dp

at each step we have 3 conditions

1. current characters are same. compare next characters
abcde. ith index
^
abc. jth index
^
then go to next and maximum length also increases
move (i + 1, j+1)
2, 3. current characters are different. move i or j to compare

abcde
^
abc
^
move (i +1, j) or (i, j+1)


let dp[i][j] max length of lcs

a b c d e
a 1 1 1 1 1
c 1 1 2 2 2
e 1 1 2 2 3

a b d e f g h
a 1 1 1 1 1 1 1
f 1 1 1 1 2 2 2
h 1 1 1 1 2 2 3
d 1 1 2 2 2 2 3

if equals
dp[i][j] = dp[i-1][j-1] + 1
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
tc : O(mn)
sc : O(mn) > O(min(m, n)) if optimize space to 2-Row array
*/
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length(), n = text2.length();
if(m < n) {
return longestCommonSubsequence(text2, text1);
}
int[][] dp = new int[2][n+1];
int prevRow = 0;
int curRow = 1;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
if(text1.charAt(i-1) == text2.charAt(j-1)) {
dp[curRow][j] = dp[prevRow][j-1] + 1;
} else {
dp[curRow][j] = Math.max(dp[prevRow][j], dp[curRow][j-1]);
}
}
curRow = prevRow;
prevRow = (prevRow + 1) % 2;
}
return dp[prevRow][n];
}
}
119 changes: 119 additions & 0 deletions longest-repeating-character-replacement/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**

input : String s
output : after k times operation, return length of longest substring
that consist of same letters

example
AAAA << 4
AAAB << AAA << 3

constraints:
1) input string can be empty?
nope. at least one letter
2) string s contains whitespace? non-alphabetical chars?
nope. only uppercase English letters
3) range of k?
[0, n], when n is the length of string s

ABAB
AAAB
AAAA

AABABBA
AAAABBA
AABBBBA

AABABBA
l
r
count : 1
4

solution 1: brute force
ds : array
algo : for loop

nested for loop
iterate through the string s from index i = 0 to n
set current character as 'target'
set count = 0
iterate through the string s from index j = i + 1 to n
if next character is identical to 'target'
continue;
else
increase count;

if count > k
break and save length

if count <=k compare length with max
ABAB
i
j
target = A
count = 1
ABAB << count = 2, length = 4
tc : O(n^2)
sc : O(1)

solution 2: two pointer

AABCABBA
l
r
A : 1
B : 1
C : 1
count = 1
res = 4
set two pointer l and r
set count to 0;
set maxFrequency character s[0];

move r pointer to right
if current char is not maxFreqChar
count + 1;
else
continue;

if count > k
while count <= k
move leftpointer right;
update frequency
set max Frequency character at each iteration O(26)
update count

tc: O(n)
sc : O(26) ~= O(1)
*/

class Solution {
public int characterReplacement(String s, int k) {
int l = 0, r = 1, maxFreq = 1, res = 1;
int n = s.length();
int[] freq = new int[26];
// base case;
char maxFreqChar = s.charAt(0);
freq[maxFreqChar - 'A']++;
// loop
while (r < n) {
char next = s.charAt(r);
maxFreq = Math.max(maxFreq, ++freq[next - 'A']);

while(r - l + 1 - maxFreq > k) {
freq[s.charAt(l) - 'A']--;
l++;
for(int i = 0; i <26; i++) {
if(freq[i] > maxFreq) {
maxFreq = freq[i];
}
}
}
res = Math.max(res, r - l+1);
r++;
}

return res;
}
}
19 changes: 19 additions & 0 deletions number-of-1-bits/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int hammingWeight(int n) {
int curN = n;
int cnt = 0;
while(curN > 0) {
if(curN %2 == 1) {
cnt++;
}
curN /= 2;
}
return cnt;
}
}
/**

1 2 3 4 5 6 7 8 9 10 11
0 1 2 1 2 2 3 1 2 2 3

*/
28 changes: 28 additions & 0 deletions sum-of-two-integers/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*

bit manipulation

find carry of each binary digits sum
example

1 0 1 1
+0 0 1 0
-----------
0 1
^carry 1 (1<<2)
1
1


*/
class Solution {
public int getSum(int a, int b) {
int carryBitSet;
while(b != 0) {
carryBitSet = a & b;
a = a ^ b;
b = carryBitSet << 1;
}
return a;
}
}