Skip to content

Commit 75fe00c

Browse files
authored
Merge pull request #1866 from njngwn/main
2 parents 8500301 + 75361b7 commit 75fe00c

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

container-with-most-water/njngwn.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time Complexity: O(n), n: height.length
2+
// Space Complexity: O(1)
3+
class Solution {
4+
public int maxArea(int[] height) {
5+
int maxWaterAmount = 0;
6+
int leftLineIdx = 0;
7+
int rightLineIdx = height.length-1;
8+
9+
while (leftLineIdx < rightLineIdx) {
10+
int leftHeight = height[leftLineIdx];
11+
int rightHeight = height[rightLineIdx];
12+
int tempAmount = 0;
13+
14+
if (leftHeight < rightHeight) {
15+
tempAmount = leftHeight * (rightLineIdx - leftLineIdx);
16+
leftLineIdx++;
17+
} else {
18+
tempAmount = rightHeight * (rightLineIdx - leftLineIdx);
19+
rightLineIdx--;
20+
}
21+
22+
// update maximum amount
23+
maxWaterAmount = tempAmount > maxWaterAmount ? tempAmount : maxWaterAmount;
24+
}
25+
26+
return maxWaterAmount;
27+
}
28+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class WordDictionary {
2+
private static class CharDictionary {
3+
HashMap<Character, CharDictionary> charMap;
4+
boolean isEnd;
5+
6+
private CharDictionary() {
7+
this.charMap = new HashMap<>();
8+
this.isEnd = false;
9+
}
10+
}
11+
12+
private CharDictionary rootNode;
13+
14+
public WordDictionary() {
15+
this.rootNode = new CharDictionary();
16+
}
17+
18+
public void addWord(String word) {
19+
CharDictionary currentNode = this.rootNode;
20+
21+
for (char ch : word.toCharArray()) {
22+
if (!currentNode.charMap.containsKey(ch)) {
23+
currentNode.charMap.put(ch, new CharDictionary());
24+
}
25+
currentNode = currentNode.charMap.get(ch);
26+
}
27+
currentNode.isEnd = true;
28+
}
29+
30+
public boolean search(String word) {
31+
return searchRecursive(word, this.rootNode, 0);
32+
}
33+
34+
private boolean searchRecursive(String word, CharDictionary node, int index) {
35+
// Base case
36+
if (index == word.length()) {
37+
return node.isEnd;
38+
}
39+
40+
char ch = word.charAt(index);
41+
42+
if (ch == '.') {
43+
for (CharDictionary childNode : node.charMap.values()) {
44+
if (searchRecursive(word, childNode, index + 1)) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
} else {
50+
if (!node.charMap.containsKey(ch)) {
51+
return false;
52+
}
53+
return searchRecursive(word, node.charMap.get(ch), index + 1);
54+
}
55+
}
56+
}
57+
58+
/**
59+
* Your WordDictionary object will be instantiated and called as such:
60+
* WordDictionary obj = new WordDictionary();
61+
* obj.addWord(word);
62+
* boolean param_2 = obj.search(word);
63+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// time complexity: O(nlogn), n: nums.length (logn because of binary search)
2+
// space complexity: O(n), n: nums.length
3+
class Solution {
4+
public int lengthOfLIS(int[] nums) {
5+
ArrayList<Integer> incSeqList = new ArrayList<Integer>(); // dp
6+
incSeqList.add(nums[0]);
7+
8+
for (int num : nums) {
9+
if (num > incSeqList.get(incSeqList.size()-1)) {
10+
// add element to incSeqLit
11+
incSeqList.add(num);
12+
} else {
13+
int idx = Collections.binarySearch(incSeqList, num);
14+
if (idx < 0) { // idx returns -(insertedPos + 1)
15+
int insertedIdx = -(idx + 1);
16+
incSeqList.set(insertedIdx, num);
17+
}
18+
}
19+
}
20+
21+
return incSeqList.size();
22+
}
23+
}

reverse-linked-list/njngwn.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
// Time Complexity: O(n)
12+
// Sprace Compexity: O(1)
13+
class Solution {
14+
public ListNode reverseList(ListNode head) {
15+
ListNode prevNode = null;
16+
ListNode currNode = head;
17+
18+
while (currNode != null) {
19+
ListNode nextNode = currNode.next;
20+
currNode.next = prevNode;
21+
prevNode = currNode;
22+
currNode = nextNode;
23+
}
24+
25+
return prevNode;
26+
}
27+
}

spiral-matrix/njngwn.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Time Complexity: O(m+n), m: matrix.length, n: matrix[0].length
2+
// Space Complexity: O(m*n), m: matrix.length, n: matrix[0].length, because of arraylist for output
3+
class Solution {
4+
public List<Integer> spiralOrder(int[][] matrix) {
5+
int rowMin = 0;
6+
int rowMax = matrix.length-1;
7+
int colMin = 0;
8+
int colMax = matrix[0].length-1;
9+
10+
ArrayList<Integer> orderedElements = new ArrayList<>();
11+
12+
while (rowMin <= rowMax && colMin <= colMax) {
13+
// left to right
14+
for (int col = colMin; col <= colMax; ++col) {
15+
orderedElements.add(matrix[rowMin][col]);
16+
}
17+
rowMin++;
18+
19+
// top to bottom
20+
for (int row = rowMin; row <= rowMax; ++row) {
21+
orderedElements.add(matrix[row][colMax]);
22+
}
23+
colMax--;
24+
25+
// right to left
26+
if (rowMin <= rowMax) {
27+
for (int col = colMax; col >= colMin; --col) {
28+
orderedElements.add(matrix[rowMax][col]);
29+
}
30+
}
31+
rowMax--;
32+
33+
// bottom to top
34+
if (colMin <= colMax) {
35+
for (int row = rowMax; row >= rowMin; --row) {
36+
orderedElements.add(matrix[row][colMin]);
37+
}
38+
}
39+
colMin++;
40+
}
41+
42+
return orderedElements;
43+
}
44+
}

valid-parentheses/njngwn.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n), n: s.length
2+
// Space Complexity: O(n), n: s.length (worst case: s="(((((((")
3+
class Solution {
4+
public boolean isValid(String s) {
5+
Stack<Character> bracketStack = new Stack<>();
6+
7+
for (char ch : s.toCharArray()) {
8+
if (ch == '(' || ch == '{' || ch == '[') { // open bracket
9+
bracketStack.push(ch);
10+
} else { // close bracket
11+
if (bracketStack.empty()) {
12+
return false;
13+
}
14+
15+
char sp = bracketStack.pop();
16+
if (!((sp == '(' && ch == ')') || (sp == '{' && ch == '}') || (sp == '[' && ch == ']'))) {
17+
return false;
18+
}
19+
}
20+
}
21+
22+
return bracketStack.empty();
23+
}
24+
}

0 commit comments

Comments
 (0)