Skip to content

Commit 6089332

Browse files
authored
Merge pull request #902 from ekgns33/main
[byteho0n] Week 6
2 parents 015ee61 + c636ff2 commit 6089332

File tree

5 files changed

+348
-0
lines changed

5 files changed

+348
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
input : array of integer height
3+
output : maximum amount of water a container can store.
4+
5+
we can build container with two different line (height[i], height[j] when i < j)
6+
7+
example
8+
1 8 6 2 5 4 8 3 7
9+
choose i = 1.
10+
choose j = 4
11+
8 ______5
12+
amount of water == (j - i) * min(height[i], height[j])
13+
= 3 * 5 = 15
14+
15+
we have to maximize amount of water.
16+
17+
constraints:
18+
1) is the input array valid?
19+
length of array is in range [2, 10^5]
20+
2) positive integers?
21+
no. range of height is [0, 10 ^4]
22+
>> check amount can be overflow. 10^4 * 10 ^ 5 = 10^9 < Integer range
23+
edge:
24+
1) if length is 2
25+
return min(height[0], height[1]).
26+
...
27+
28+
solution 1) brute force;
29+
iterate through the array from index i = 0 to n-1
30+
when n is the length of input array
31+
iterate through the array from index j = i + 1 to n;
32+
calculate the amount of water and update max amount
33+
ds : array
34+
algo : x
35+
tc : O(n^2) ~= 10^10 TLE
36+
space : O(1)
37+
38+
solution 2) better
39+
ds : array
40+
algo: two pointer?
41+
42+
two variant for calculating amount of water
43+
1. height 2. width
44+
45+
set width maximum at first, check heights
46+
decrease width one by one
47+
48+
- at each step width is maximum.
49+
so we have to maximize
50+
the minimum between left and right pointer
51+
52+
use left and right pointer
53+
while left < right
54+
55+
calculate amount
56+
compare
57+
if height[left] < height[right]
58+
move left by one
59+
else
60+
vice versa
61+
62+
return max
63+
tc : O(n)
64+
sc : O(1)
65+
66+
*/
67+
class Solution {
68+
public int maxArea(int[] height) {
69+
int left = 0;
70+
int right = height.length - 1;
71+
int maxAmount = 0;
72+
while(left < right) {
73+
int curAmount = (right - left) * Math.min(height[left], height[right]);
74+
maxAmount = Math.max(maxAmount, curAmount);
75+
if(height[left] < height[right]) {
76+
left++;
77+
} else {
78+
right--;
79+
}
80+
}
81+
return maxAmount;
82+
}
83+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
design ds that supports add, find
3+
find if string matches any previously added string.
4+
>> some kind of dictionary
5+
6+
example
7+
add bad
8+
add dad
9+
add mad
10+
search pad >> false
11+
search bad >> true
12+
search .ad >> true . can be 'b' or 'd' or 'm'
13+
search b.. >> true .. can be "ad"
14+
15+
constraints:
16+
1) empty string can be valid input?
17+
nope. lenght of string is in range of [1, 25]
18+
2) string only contiains alphanumeric? or alphabet
19+
only lowercase English letters
20+
3) how many queries can be given as input?
21+
at most 10^4
22+
23+
solution 1) brute force
24+
save to data structure. for add
25+
check every character for all the saved words
26+
O(kn) when k is the number of saved words,
27+
n is the length of input word token
28+
25 * 25 * 10^4
29+
tc : O(kn) + O(kn)
30+
add : O(1)
31+
search : O(kn)
32+
sc : O(kn)
33+
34+
solution 2) trie?
35+
36+
save words to trie.
37+
when searching
38+
do bfs or backtracking dfs
39+
if current charcter is . add all childs
40+
else add matching childs
41+
overall tc : O(sum(m)) + O(n),
42+
add : O(m), when m is the length of saved word
43+
search : O(n), when n is the length of searh word
44+
sc : O(sum(m))
45+
46+
if volume of search query is much bigger than add query
47+
trie solution would be better
48+
O(n) search time vs O(mn) search time
49+
*/
50+
class WordDictionary {
51+
class TrieNode {
52+
TrieNode[] childs;
53+
boolean isEnd;
54+
TrieNode() {
55+
childs = new TrieNode[26];
56+
isEnd = false;
57+
}
58+
}
59+
TrieNode root;
60+
public WordDictionary() {
61+
root = new TrieNode();
62+
}
63+
64+
public void addWord(String word) {
65+
TrieNode curNode = root;
66+
for(char c : word.toCharArray()) {
67+
if(curNode.childs[c-'a'] == null) {
68+
curNode.childs[c-'a'] = new TrieNode();
69+
}
70+
curNode = curNode.childs[c-'a'];
71+
}
72+
curNode.isEnd = true;
73+
}
74+
75+
public boolean search(String word) {
76+
return dfsHelper(root, word, 0);
77+
}
78+
79+
public boolean dfsHelper(TrieNode node, String word, int p) {
80+
//end clause
81+
if(p == word.length()) {
82+
return node.isEnd;
83+
}
84+
85+
char curC = word.charAt(p);
86+
if(curC == '.') {
87+
for(TrieNode next : node.childs) {
88+
if(next != null) {
89+
if(dfsHelper(next, word, p + 1)) return true;
90+
}
91+
}
92+
return false;
93+
} else {
94+
if(node.childs[curC-'a'] != null) {
95+
if(dfsHelper(node.childs[curC - 'a'], word, p + 1)) return true;
96+
}
97+
return false;
98+
}
99+
100+
101+
}
102+
}
103+
104+
/**
105+
* Your WordDictionary object will be instantiated and called as such:
106+
* WordDictionary obj = new WordDictionary();
107+
* obj.addWord(word);
108+
* boolean param_2 = obj.search(word);
109+
*/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
input : array of integers
3+
output : length of the longest strictly increasing subsequence;
4+
example
5+
0 1 0 3 2 3 > 0 1 2 3 >> length : 4
6+
4 3 2 1 > 1 >> length : 1
7+
1 1 1 1 > 1 >> length : 1
8+
9+
constraints :
10+
1) empty array allowed?
11+
no, at least one
12+
13+
solution 1) brute force
14+
nested for loop
15+
iterate through the array from index i = 0 to n-1 when n is the lenght of input
16+
iterate throught the array from index j = i + 1 to n
17+
update current Maximum value
18+
if bigger than prev max value count ++
19+
tc : O(n^2);
20+
sc : O(1)
21+
22+
solution 2) binary search + dp
23+
iterate through the array
24+
search from saved values.
25+
if current value is bigger than everything add
26+
else change value
27+
28+
let val[i] save the smallest value of length i subsequence
29+
tc : O(nlogn)
30+
sc : O(n)
31+
32+
*/
33+
34+
class Solution {
35+
public int lengthOfLIS(int[] nums) {
36+
List<Integer> vals = new ArrayList<>();
37+
for(int num : nums) {
38+
if(vals.isEmpty() || vals.getLast() < num) {
39+
vals.add(num);
40+
} else {
41+
vals.set(binarySearch(vals, num), num);
42+
}
43+
}
44+
return vals.size();
45+
}
46+
int binarySearch(List<Integer> vals, int num) {
47+
int l = 0, r = vals.size()-1;
48+
while(l <= r) {
49+
int mid = (r - l) / 2+ l;
50+
if(vals.get(mid) == num) {
51+
return mid;
52+
} else if (vals.get(mid) > num) {
53+
r = mid - 1;
54+
} else {
55+
l = mid + 1;
56+
}
57+
}
58+
return l;
59+
}
60+
}

spiral-matrix/ekgns33.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*. lr ->
3+
* ^ 1 2 3 4. rd
4+
* lu 5 6 7 8. v
5+
* <- rl
6+
* tc : O(mn)
7+
* sc : O(1)
8+
* */
9+
10+
class Solution {
11+
public List<Integer> spiralOrder(int[][] matrix) {
12+
int m = matrix.length;
13+
int n = matrix[0].length;
14+
int lr = 0, rd = 0, rl = n -1, lu = m - 1;
15+
int cnt = 0, target = m*n;
16+
List<Integer> res = new ArrayList<>();
17+
18+
while(lr <= rl && rd <= lu) {
19+
for(int startLeft = lr; startLeft <= rl; startLeft++) {
20+
res.add(matrix[rd][startLeft]);
21+
}
22+
rd++;
23+
for(int startUp = rd; startUp <=lu; startUp++) {
24+
res.add(matrix[startUp][rl]);
25+
}
26+
rl--;
27+
if(rd <= lu) {
28+
for(int startRight = rl; startRight >= lr; startRight--) {
29+
res.add(matrix[lu][startRight]);
30+
}
31+
}
32+
lu--;
33+
if(lr <= rl) {
34+
for(int startDown = lu; startDown >= rd; startDown--) {
35+
res.add(matrix[startDown][lr]);
36+
}
37+
}
38+
lr++;
39+
}
40+
return res;
41+
}
42+
}

valid-parentheses/ekgns33.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
input : string s contains just (, ), {, }, [, ]
3+
output : return if s is valid
4+
valid means that parentheses are matched(balanced)
5+
example
6+
((())) : valid
7+
(((())) : invalid
8+
{() : invalid
9+
10+
constraint :
11+
1) input string can be empty string?
12+
nope. at least one character >> if length is odd number return false
13+
edge:
14+
1) if the length of string is odd number return false
15+
16+
solution 1)
17+
ds : stack
18+
algo : x
19+
iterate through the string s
20+
if opening bracket, add to stack
21+
else check the top element of stack
22+
if matched pop
23+
else return false;
24+
return false
25+
26+
tc : O(n)
27+
sc : O(n) worst case : every character is opening bracket
28+
29+
*/
30+
class Solution {
31+
public boolean isValid(String s) {
32+
//edge
33+
if(s.length() % 2 == 1) return false;
34+
// we can use deque instead
35+
Stack<Character> stack = new Stack<>();
36+
37+
for(char c : s.toCharArray()) {
38+
if(c == '(' || c == '{' || c == '[') {
39+
stack.push(c);
40+
} else {
41+
if(stack.isEmpty()) return false;
42+
if(c == ')') {
43+
if(stack.peek() != '(') return false;
44+
} else if (c == '}') {
45+
if(stack.peek() != '{') return false;
46+
} else if (c == ']'){
47+
if(stack.peek() != '[') return false;
48+
}
49+
stack.pop();
50+
}
51+
}
52+
return stack.isEmpty();
53+
}
54+
}

0 commit comments

Comments
 (0)