-
-
Notifications
You must be signed in to change notification settings - Fork 247
[njngwn] WEEK 06 solutions #1866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
njngwn
wants to merge
9
commits into
DaleStudy:main
Choose a base branch
from
njngwn:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ba08f52
solution: valid parentheses
e037d9e
solution: container with most water
1bd18b6
solution: design add and search words and data structure
f395df8
add inline
93385ae
add inline
a69f7b5
Revert "add inline"
f3b689d
add inline
621834d
solution: longest increasing subsequence
5bc4b26
solution: spiral matrix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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--;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 그럼 코드가 더 깔끔해지겠군요..!! 피드백 감사합니다 👍