Skip to content

Commit 6e0c806

Browse files
authored
Merge pull request #2521 from Cyjin-jani/main
[Cyjin-jani] WEEK 06 Solutions
2 parents 682c786 + 3aac77a commit 6e0c806

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tc: O(n);
2+
// sc: O(1);
3+
const maxArea = function (height) {
4+
let max = 0;
5+
let leftIdx = 0;
6+
let rightIdx = height.length - 1;
7+
8+
while (leftIdx < rightIdx) {
9+
const width = rightIdx - leftIdx;
10+
const minHeight = Math.min(height[leftIdx], height[rightIdx]);
11+
const area = width * minHeight;
12+
max = Math.max(max, area);
13+
14+
if (height[leftIdx] > height[rightIdx]) {
15+
rightIdx -= 1;
16+
} else {
17+
leftIdx += 1;
18+
}
19+
}
20+
21+
return max;
22+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var WordDictionary = function () {
2+
this.dictionary = new Set();
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*/
9+
WordDictionary.prototype.addWord = function (word) {
10+
this.dictionary.add(word);
11+
};
12+
13+
/**
14+
* @param {string} word
15+
* @return {boolean}
16+
*/
17+
18+
// tc: O(n * k), n: dictionary size, k: word length => O(n) in worst case
19+
// sc: O(1)
20+
WordDictionary.prototype.search = function (word) {
21+
if (!word.includes('.')) {
22+
return this.dictionary.has(word);
23+
}
24+
25+
for (const stored of this.dictionary) {
26+
if (stored.length !== word.length) continue;
27+
28+
let isMatch = true;
29+
for (let i = 0; i < word.length; i++) {
30+
if (word[i] === '.') continue;
31+
if (word[i] !== stored[i]) {
32+
isMatch = false;
33+
break;
34+
}
35+
}
36+
if (isMatch) return true;
37+
}
38+
39+
return false;
40+
};
41+
42+
/**
43+
* Your WordDictionary object will be instantiated and called as such:
44+
* var obj = new WordDictionary()
45+
* obj.addWord(word)
46+
* var param_2 = obj.search(word)
47+
*/

valid-parentheses/Cyjin-jani.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// tc: O(n)
2+
// sc: O(n)
3+
const isValid = function (s) {
4+
const bracketMap = {
5+
'(': ')',
6+
'{': '}',
7+
'[': ']',
8+
};
9+
10+
if (s.length % 2 !== 0 || isCloseBracket(s[0])) return false;
11+
12+
const stack = [];
13+
14+
for (let i = 0; i < s.length; i++) {
15+
if (stack.length === 0) {
16+
stack.push(s[i]);
17+
continue;
18+
}
19+
20+
let topBracket = stack.pop();
21+
if (bracketMap[topBracket] !== s[i]) {
22+
stack.push(topBracket);
23+
stack.push(s[i]);
24+
}
25+
}
26+
27+
return stack.length === 0;
28+
};
29+
30+
function isCloseBracket(char) {
31+
const closeBrackets = [')', '}', ']'];
32+
33+
return closeBrackets.includes(char);
34+
}

0 commit comments

Comments
 (0)