Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions container-with-most-water/njngwn.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

막대기에 따라 x,y 축을 구해서 면적을 구하는 로직과 투 포인터의 인덱스를 업데이트하는 로직을 분리해도 좋을 것 같아요!
다른 코드들도 풀이가 좋아서 많이 배웠습니다.

// 면적 구하는 로직
int y = Math.min(height[start], height[end]);
int x = end - start;
int calculatedArea = x * y;
area = Math.max(area, calculatedArea);

// 인덱스 업데이트 로직
if (height[start] <= height[end]) start++;
else end--;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그럼 코드가 더 깔끔해지겠군요..!! 피드백 감사합니다 👍

Original file line number Diff line number Diff line change
@@ -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;
}
}
63 changes: 63 additions & 0 deletions design-add-and-search-words-data-structure/njngwn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class WordDictionary {
private static class CharDictionary {
HashMap<Character, CharDictionary> 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);
*/
23 changes: 23 additions & 0 deletions longest-increasing-subsequence/njngwn.java
Original file line number Diff line number Diff line change
@@ -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<Integer> incSeqList = new ArrayList<Integer>(); // 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();
}
}
44 changes: 44 additions & 0 deletions spiral-matrix/njngwn.java
Original file line number Diff line number Diff line change
@@ -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<Integer> spiralOrder(int[][] matrix) {
int rowMin = 0;
int rowMax = matrix.length-1;
int colMin = 0;
int colMax = matrix[0].length-1;

ArrayList<Integer> 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;
}
}
24 changes: 24 additions & 0 deletions valid-parentheses/njngwn.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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();
}
}