Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6292f47

Browse files
authoredFeb 2, 2025
Merge pull request #978 from ekgns33/main
[byteho0n] Week 8
2 parents e7c558d + cf1c998 commit 6292f47

File tree

5 files changed

+284
-0
lines changed

5 files changed

+284
-0
lines changed
 

‎clone-graph/ekgns33.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public List<Node> neighbors;
6+
public Node() {
7+
val = 0;
8+
neighbors = new ArrayList<Node>();
9+
}
10+
public Node(int _val) {
11+
val = _val;
12+
neighbors = new ArrayList<Node>();
13+
}
14+
public Node(int _val, ArrayList<Node> _neighbors) {
15+
val = _val;
16+
neighbors = _neighbors;
17+
}
18+
}
19+
*/
20+
21+
class Solution {
22+
public Node cloneGraph(Node node) {
23+
Map<Integer, Node> nodeMap = new HashMap<>();
24+
if(node == null) return null;
25+
boolean[] visited = new boolean[101];
26+
dfsHelper(nodeMap, node, visited);
27+
return nodeMap.get(1);
28+
}
29+
30+
private void dfsHelper(Map<Integer, Node> map, Node node, boolean[] visited) {
31+
if(!map.containsKey(node.val)) {
32+
map.put(node.val, new Node(node.val));
33+
}
34+
visited[node.val] = true;
35+
Node cur = map.get(node.val);
36+
for(Node adjacent : node.neighbors) {
37+
map.putIfAbsent(adjacent.val, new Node(adjacent.val));
38+
cur.neighbors.add(map.get(adjacent.val));
39+
if(!visited[adjacent.val]) dfsHelper(map, adjacent, visited);
40+
}
41+
}
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
input : string text1, text2
3+
output : length of lcs else 0
4+
constraints:
5+
1) both input strings are not empty
6+
2) input strings are consist of lowercase eng. letters
7+
8+
solution 1) brute force
9+
10+
nested for loop
11+
tc : O(n^2 * m) where n is min (len(text1), len(text2)), m is max
12+
sc : O(1)
13+
14+
solution 2) dp
15+
16+
at each step we have 3 conditions
17+
18+
1. current characters are same. compare next characters
19+
abcde. ith index
20+
^
21+
abc. jth index
22+
^
23+
then go to next and maximum length also increases
24+
move (i + 1, j+1)
25+
2, 3. current characters are different. move i or j to compare
26+
27+
abcde
28+
^
29+
abc
30+
^
31+
move (i +1, j) or (i, j+1)
32+
33+
34+
let dp[i][j] max length of lcs
35+
36+
a b c d e
37+
a 1 1 1 1 1
38+
c 1 1 2 2 2
39+
e 1 1 2 2 3
40+
41+
a b d e f g h
42+
a 1 1 1 1 1 1 1
43+
f 1 1 1 1 2 2 2
44+
h 1 1 1 1 2 2 3
45+
d 1 1 2 2 2 2 3
46+
47+
if equals
48+
dp[i][j] = dp[i-1][j-1] + 1
49+
else
50+
dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
51+
tc : O(mn)
52+
sc : O(mn) > O(min(m, n)) if optimize space to 2-Row array
53+
*/
54+
class Solution {
55+
public int longestCommonSubsequence(String text1, String text2) {
56+
int m = text1.length(), n = text2.length();
57+
if(m < n) {
58+
return longestCommonSubsequence(text2, text1);
59+
}
60+
int[][] dp = new int[2][n+1];
61+
int prevRow = 0;
62+
int curRow = 1;
63+
for(int i = 1; i <= m; i++) {
64+
for(int j = 1; j <= n; j++) {
65+
if(text1.charAt(i-1) == text2.charAt(j-1)) {
66+
dp[curRow][j] = dp[prevRow][j-1] + 1;
67+
} else {
68+
dp[curRow][j] = Math.max(dp[prevRow][j], dp[curRow][j-1]);
69+
}
70+
}
71+
curRow = prevRow;
72+
prevRow = (prevRow + 1) % 2;
73+
}
74+
return dp[prevRow][n];
75+
}
76+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
3+
input : String s
4+
output : after k times operation, return length of longest substring
5+
that consist of same letters
6+
7+
example
8+
AAAA << 4
9+
AAAB << AAA << 3
10+
11+
constraints:
12+
1) input string can be empty?
13+
nope. at least one letter
14+
2) string s contains whitespace? non-alphabetical chars?
15+
nope. only uppercase English letters
16+
3) range of k?
17+
[0, n], when n is the length of string s
18+
19+
ABAB
20+
AAAB
21+
AAAA
22+
23+
AABABBA
24+
AAAABBA
25+
AABBBBA
26+
27+
AABABBA
28+
l
29+
r
30+
count : 1
31+
4
32+
33+
solution 1: brute force
34+
ds : array
35+
algo : for loop
36+
37+
nested for loop
38+
iterate through the string s from index i = 0 to n
39+
set current character as 'target'
40+
set count = 0
41+
iterate through the string s from index j = i + 1 to n
42+
if next character is identical to 'target'
43+
continue;
44+
else
45+
increase count;
46+
47+
if count > k
48+
break and save length
49+
50+
if count <=k compare length with max
51+
ABAB
52+
i
53+
j
54+
target = A
55+
count = 1
56+
ABAB << count = 2, length = 4
57+
tc : O(n^2)
58+
sc : O(1)
59+
60+
solution 2: two pointer
61+
62+
AABCABBA
63+
l
64+
r
65+
A : 1
66+
B : 1
67+
C : 1
68+
count = 1
69+
res = 4
70+
set two pointer l and r
71+
set count to 0;
72+
set maxFrequency character s[0];
73+
74+
move r pointer to right
75+
if current char is not maxFreqChar
76+
count + 1;
77+
else
78+
continue;
79+
80+
if count > k
81+
while count <= k
82+
move leftpointer right;
83+
update frequency
84+
set max Frequency character at each iteration O(26)
85+
update count
86+
87+
tc: O(n)
88+
sc : O(26) ~= O(1)
89+
*/
90+
91+
class Solution {
92+
public int characterReplacement(String s, int k) {
93+
int l = 0, r = 1, maxFreq = 1, res = 1;
94+
int n = s.length();
95+
int[] freq = new int[26];
96+
// base case;
97+
char maxFreqChar = s.charAt(0);
98+
freq[maxFreqChar - 'A']++;
99+
// loop
100+
while (r < n) {
101+
char next = s.charAt(r);
102+
maxFreq = Math.max(maxFreq, ++freq[next - 'A']);
103+
104+
while(r - l + 1 - maxFreq > k) {
105+
freq[s.charAt(l) - 'A']--;
106+
l++;
107+
for(int i = 0; i <26; i++) {
108+
if(freq[i] > maxFreq) {
109+
maxFreq = freq[i];
110+
}
111+
}
112+
}
113+
res = Math.max(res, r - l+1);
114+
r++;
115+
}
116+
117+
return res;
118+
}
119+
}

‎number-of-1-bits/ekgns33.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
int curN = n;
4+
int cnt = 0;
5+
while(curN > 0) {
6+
if(curN %2 == 1) {
7+
cnt++;
8+
}
9+
curN /= 2;
10+
}
11+
return cnt;
12+
}
13+
}
14+
/**
15+
16+
1 2 3 4 5 6 7 8 9 10 11
17+
0 1 2 1 2 2 3 1 2 2 3
18+
19+
*/

‎sum-of-two-integers/ekgns33.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
3+
bit manipulation
4+
5+
find carry of each binary digits sum
6+
example
7+
8+
1 0 1 1
9+
+0 0 1 0
10+
-----------
11+
0 1
12+
^carry 1 (1<<2)
13+
1
14+
1
15+
16+
17+
*/
18+
class Solution {
19+
public int getSum(int a, int b) {
20+
int carryBitSet;
21+
while(b != 0) {
22+
carryBitSet = a & b;
23+
a = a ^ b;
24+
b = carryBitSet << 1;
25+
}
26+
return a;
27+
}
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.