diff --git a/container-with-most-water/njngwn.java b/container-with-most-water/njngwn.java new file mode 100644 index 000000000..579e8073d --- /dev/null +++ b/container-with-most-water/njngwn.java @@ -0,0 +1,28 @@ +// Time Complexity: O(n), n: height.length +// Space Complexity: O(1) +class Solution { + public int maxArea(int[] height) { + int maxWaterAmount = 0; + int leftLineIdx = 0; + int rightLineIdx = height.length-1; + + while (leftLineIdx < rightLineIdx) { + int leftHeight = height[leftLineIdx]; + int rightHeight = height[rightLineIdx]; + int tempAmount = 0; + + if (leftHeight < rightHeight) { + tempAmount = leftHeight * (rightLineIdx - leftLineIdx); + leftLineIdx++; + } else { + tempAmount = rightHeight * (rightLineIdx - leftLineIdx); + rightLineIdx--; + } + + // update maximum amount + maxWaterAmount = tempAmount > maxWaterAmount ? tempAmount : maxWaterAmount; + } + + return maxWaterAmount; + } +} diff --git a/design-add-and-search-words-data-structure/njngwn.java b/design-add-and-search-words-data-structure/njngwn.java new file mode 100644 index 000000000..f636fd40b --- /dev/null +++ b/design-add-and-search-words-data-structure/njngwn.java @@ -0,0 +1,63 @@ +class WordDictionary { + private static class CharDictionary { + HashMap charMap; + boolean isEnd; + + private CharDictionary() { + this.charMap = new HashMap<>(); + this.isEnd = false; + } + } + + private CharDictionary rootNode; + + public WordDictionary() { + this.rootNode = new CharDictionary(); + } + + public void addWord(String word) { + CharDictionary currentNode = this.rootNode; + + for (char ch : word.toCharArray()) { + if (!currentNode.charMap.containsKey(ch)) { + currentNode.charMap.put(ch, new CharDictionary()); + } + currentNode = currentNode.charMap.get(ch); + } + currentNode.isEnd = true; + } + + public boolean search(String word) { + return searchRecursive(word, this.rootNode, 0); + } + + private boolean searchRecursive(String word, CharDictionary node, int index) { + // Base case + if (index == word.length()) { + return node.isEnd; + } + + char ch = word.charAt(index); + + if (ch == '.') { + for (CharDictionary childNode : node.charMap.values()) { + if (searchRecursive(word, childNode, index + 1)) { + return true; + } + } + return false; + } else { + if (!node.charMap.containsKey(ch)) { + return false; + } + return searchRecursive(word, node.charMap.get(ch), index + 1); + } + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ diff --git a/longest-increasing-subsequence/njngwn.java b/longest-increasing-subsequence/njngwn.java new file mode 100644 index 000000000..654a9d340 --- /dev/null +++ b/longest-increasing-subsequence/njngwn.java @@ -0,0 +1,23 @@ +// time complexity: O(nlogn), n: nums.length (logn because of binary search) +// space complexity: O(n), n: nums.length +class Solution { + public int lengthOfLIS(int[] nums) { + ArrayList incSeqList = new ArrayList(); // dp + incSeqList.add(nums[0]); + + for (int num : nums) { + if (num > incSeqList.get(incSeqList.size()-1)) { + // add element to incSeqLit + incSeqList.add(num); + } else { + int idx = Collections.binarySearch(incSeqList, num); + if (idx < 0) { // idx returns -(insertedPos + 1) + int insertedIdx = -(idx + 1); + incSeqList.set(insertedIdx, num); + } + } + } + + return incSeqList.size(); + } +} diff --git a/reverse-linked-list/njngwn.java b/reverse-linked-list/njngwn.java new file mode 100644 index 000000000..94ee5ac9d --- /dev/null +++ b/reverse-linked-list/njngwn.java @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +// Time Complexity: O(n) +// Sprace Compexity: O(1) +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prevNode = null; + ListNode currNode = head; + + while (currNode != null) { + ListNode nextNode = currNode.next; + currNode.next = prevNode; + prevNode = currNode; + currNode = nextNode; + } + + return prevNode; + } +} diff --git a/spiral-matrix/njngwn.java b/spiral-matrix/njngwn.java new file mode 100644 index 000000000..60190a138 --- /dev/null +++ b/spiral-matrix/njngwn.java @@ -0,0 +1,44 @@ +// Time Complexity: O(m+n), m: matrix.length, n: matrix[0].length +// Space Complexity: O(m*n), m: matrix.length, n: matrix[0].length, because of arraylist for output +class Solution { + public List spiralOrder(int[][] matrix) { + int rowMin = 0; + int rowMax = matrix.length-1; + int colMin = 0; + int colMax = matrix[0].length-1; + + ArrayList orderedElements = new ArrayList<>(); + + while (rowMin <= rowMax && colMin <= colMax) { + // left to right + for (int col = colMin; col <= colMax; ++col) { + orderedElements.add(matrix[rowMin][col]); + } + rowMin++; + + // top to bottom + for (int row = rowMin; row <= rowMax; ++row) { + orderedElements.add(matrix[row][colMax]); + } + colMax--; + + // right to left + if (rowMin <= rowMax) { + for (int col = colMax; col >= colMin; --col) { + orderedElements.add(matrix[rowMax][col]); + } + } + rowMax--; + + // bottom to top + if (colMin <= colMax) { + for (int row = rowMax; row >= rowMin; --row) { + orderedElements.add(matrix[row][colMin]); + } + } + colMin++; + } + + return orderedElements; + } +} diff --git a/valid-parentheses/njngwn.java b/valid-parentheses/njngwn.java new file mode 100644 index 000000000..3c18e535d --- /dev/null +++ b/valid-parentheses/njngwn.java @@ -0,0 +1,24 @@ +// Time Complexity: O(n), n: s.length +// Space Complexity: O(n), n: s.length (worst case: s="(((((((") +class Solution { + public boolean isValid(String s) { + Stack bracketStack = new Stack<>(); + + for (char ch : s.toCharArray()) { + if (ch == '(' || ch == '{' || ch == '[') { // open bracket + bracketStack.push(ch); + } else { // close bracket + if (bracketStack.empty()) { + return false; + } + + char sp = bracketStack.pop(); + if (!((sp == '(' && ch == ')') || (sp == '{' && ch == '}') || (sp == '[' && ch == ']'))) { + return false; + } + } + } + + return bracketStack.empty(); + } +}