-
-
Notifications
You must be signed in to change notification settings - Fork 245
[eunhwa99] Week 6 #889
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
Merged
Merged
[eunhwa99] Week 6 #889
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8c4af7c
valid parentheses
eunhwa99 02f4602
Add comment
eunhwa99 0a4c99a
container with most water
eunhwa99 1ebb8b2
longest increasing subsequence
eunhwa99 020c619
spiral matrix
eunhwa99 65c2173
design add and search words data structure
eunhwa99 ebd2054
복잡도 수정
eunhwa99 6f30cc7
Merge branch 'DaleStudy:main' into main
eunhwa99 6a3ef60
Remove unnecessary folder
eunhwa99 8ed7e2e
fix comment for two-pointer
eunhwa99 047a415
add line
eunhwa99 fa625bd
reverse linked list
eunhwa99 c68a5b0
longest subsring without repeating characters
eunhwa99 390832a
Merge branch 'DaleStudy:main' into main
eunhwa99 61e5d36
Revert "longest subsring without repeating characters"
eunhwa99 a26f8ee
Revert "reverse linked list"
eunhwa99 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,22 @@ | ||
// two pointer | ||
|
||
// 시간 복잡도: O(nlogn) - 투 포인터 | ||
// 공간 복잡도: O(n) - height 배열 크기 | ||
|
||
class Solution { | ||
public int maxArea(int[] height) { | ||
int left=0; | ||
int right = height.length-1; | ||
|
||
int maxContainer = 0; | ||
while(left<right){ | ||
int container = (Math.min(height[left], height[right])) * (right-left); | ||
|
||
maxContainer = Math.max(maxContainer, container); | ||
if(height[left]< height[right]) left++; | ||
else right--; | ||
} | ||
|
||
return maxContainer; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
design-add-and-search-words-data-structure/eunhwa99.java
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,109 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
// 풀이 1 : 단순 반복문 | ||
// 시간 복잡도: N(단어 길이), M(단어 개수) -> O(N*M) | ||
// 공간 복잡도: O(M) | ||
class WordDictionary { | ||
|
||
Set<String> wordSet; | ||
public WordDictionary() { | ||
wordSet = new HashSet<>(); | ||
} | ||
|
||
public void addWord(String word) { | ||
wordSet.add(word); | ||
} | ||
|
||
public boolean search(String word) { | ||
if(word.contains(".")){ | ||
char[] wordArr = word.toCharArray(); | ||
boolean found = false; | ||
for(String w: wordSet){ | ||
if(word.length()!=w.length()) continue; | ||
|
||
char[] charArr = w.toCharArray(); | ||
for(int i=0;i<charArr.length;i++){ | ||
if(wordArr[i]=='.') { | ||
found=true; | ||
continue; | ||
} | ||
if(wordArr[i]!=charArr[i]) { | ||
found=false; | ||
break; | ||
} | ||
found=true; | ||
} | ||
if(found) return true; | ||
} | ||
return false; | ||
} | ||
else{ | ||
return wordSet.contains(word); | ||
} | ||
} | ||
} | ||
|
||
|
||
// 풀이2: trie | ||
// 시간 복잡도: N(단어 길이) -> O(26^N * N) | ||
// 공간 복잡도: O(N * M) - M(단어 개수) | ||
class TrieNode { | ||
TrieNode[] child; | ||
boolean isEnd; // flag if the node is end. | ||
|
||
public TrieNode() { | ||
child = new TrieNode[26]; // 알파벳 개수 | ||
isEnd = false; | ||
} | ||
} | ||
|
||
class WordDictionary { | ||
TrieNode root; | ||
|
||
public WordDictionary() { | ||
root = new TrieNode(); // trie 루트 생성 | ||
} | ||
|
||
public void addWord(String word) { | ||
TrieNode cur = root; | ||
for (char w : word.toCharArray()) { | ||
if (cur.child[w - 'a'] == null) { | ||
cur.child[w - 'a'] = new TrieNode(); // 자식 생성 | ||
} | ||
cur = cur.child[w - 'a']; | ||
} | ||
cur.isEnd = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
return dfs(root, word, 0); // dfs 호출 시, index도 전달 | ||
} | ||
|
||
// dfs를 수행하며, 현재 인덱스까지 탐색한 상태에서 단어를 검색 | ||
private boolean dfs(TrieNode cur, String word, int index) { | ||
// 단어 끝에 도달했으면, 현재 노드가 끝을 나타내는지 확인 | ||
if (index == word.length()) { | ||
return cur.isEnd; | ||
} | ||
|
||
char c = word.charAt(index); | ||
|
||
// '.' 이면 모든 자식 노드에 대해 재귀적으로 탐색 | ||
if (c == '.') { | ||
for (TrieNode child : cur.child) { | ||
if (child != null && dfs(child, word, index + 1)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} else { | ||
// '.'이 아닌 경우, 해당 문자에 해당하는 자식으로 계속 내려감 | ||
if (cur.child[c - 'a'] == null) { | ||
return false; | ||
} | ||
return dfs(cur.child[c - 'a'], word, index + 1); | ||
} | ||
} | ||
} | ||
|
Submodule leetcode-study
added at
77bcf7
Submodule leetcode-study-1
added at
58e290
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,55 @@ | ||
class Solution { | ||
|
||
// lenArr[i] : i를 마지막 원소로 하는 최장 부분 수열 길이 | ||
// 시간 복잡도: O(N*N) -> 이중 반복문 | ||
// 공간 복잡도: O(N) -> 배열 크기 | ||
public int lengthOfLIS(int[] nums) { | ||
int[] lenArr = new int[nums.length]; | ||
for(int i=0;i<nums.length;i++){ | ||
lenArr[i] = 1; | ||
for(int j=0;j<i;j++){ | ||
if(nums[i]>nums[j]){ | ||
lenArr[i] = Math.max(lenArr[j] +1, lenArr[i]); | ||
// j번째 숫자를 수열에 포함시키거나 포함시키지 않음 -> 두 개 비교해서 Max 값 찾는다. | ||
} | ||
} | ||
} | ||
|
||
int result = 0; | ||
for(int i=0;i<nums.length;++i){ | ||
result = Math.max(lenArr[i], result); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
// 2번째 풀이 -> BinarySearch 로도 가능하다...! | ||
// 시간 복잡도 : O(NlogN) | ||
// 공간 복잡도: O(N) | ||
class Solution { | ||
public int lengthOfLIS(int[] nums) { | ||
int[] list = new int[nums.length]; | ||
int j = -1; | ||
for(int i=0;i<nums.length;i++){ | ||
if (j == -1 || list[j] < nums[i]) { | ||
list[++j] = nums[i]; | ||
} | ||
else{ | ||
int left = 0, right = j; | ||
while(left<right){ | ||
int mid = left + (right - left) / 2; | ||
|
||
if(list[mid]<nums[i]) left = mid + 1; | ||
// nums[i]는 list[mid]보다 크므로 nums[i]는 mid 오른쪽에 위치해야 함 -> 따라서 left = mid + 1로 범위를 좁힌다. | ||
else right = mid; | ||
} | ||
list[left] = nums[i]; | ||
|
||
} | ||
} | ||
|
||
return j + 1; | ||
} | ||
|
||
} |
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,47 @@ | ||
// 시간 복잡도: O(N*N) | ||
// 공간 복잡도: O(N*N) | ||
|
||
// 이동하는 방향을 바꾸는 조건 (아래 중 하나 만족) | ||
// 1. 다음에 갈 곳이 이미 방문한 곳 | ||
// 2. 다음에 갈 곳이 배열 범위를 벗어난 곳 | ||
class Solution { | ||
public List<Integer> spiralOrder(int[][] matrix) { | ||
int row = matrix.length; | ||
int col = matrix[0].length; | ||
boolean[][] visited = new boolean[row][col]; | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// right, down, left, up | ||
int[] dirY = new int[]{1,0,-1,0}; // 열 방향 | ||
int[] dirX = new int[]{0,1,0,-1}; // 행 방향 | ||
int dirPointer = 0; | ||
|
||
List<Integer> result = new ArrayList<>(); | ||
|
||
int x = 0, y = 0; | ||
int cnt = 0; int total = row*col; | ||
while(cnt < total){ | ||
|
||
result.add(matrix[x][y]); | ||
visited[x][y]=true; | ||
++cnt; | ||
// 다음 좌표로 이동 | ||
int nextX = x + dirX[dirPointer]; | ||
int nextY = y + dirY[dirPointer]; | ||
|
||
// 경계 조건 체크 및 방향 전환 | ||
if (nextX < 0 || nextX >= row || nextY < 0 || nextY >= col || visited[nextX][nextY]) { | ||
// 현재 방향에서 벗어나면 방향을 변경 | ||
dirPointer = (dirPointer + 1) % 4; | ||
nextX = x + dirX[dirPointer]; | ||
nextY = y + dirY[dirPointer]; | ||
} | ||
|
||
// 좌표 업데이트 | ||
x = nextX; | ||
y = nextY; | ||
} | ||
|
||
|
||
return result; | ||
} | ||
} |
Submodule leetcode-study
added at
32ed2b
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,36 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
|
||
// 열린 괄호일 경우. 스택에 푸쉬 | ||
// 닫힌 괄호일 경우, 스택의 top 부분이 같은 종류의 열린 괄호이면 pop, 다른 종류 열린 괄호이면 invalid한 문자열 | ||
|
||
// 시간 복잡도: 스택의 크기 = 문자열의 크기 O(N) | ||
// 공간 복잡도: 스택의 크기 = O(N) | ||
class Solution { | ||
public boolean isValid(String s) { | ||
char[] array = s.toCharArray(); | ||
Map<Character, Character> pMap = new HashMap<>(); | ||
pMap.put(')','('); | ||
pMap.put('}','{'); | ||
pMap.put(']','['); | ||
|
||
Stack<Character> stack = new Stack<>(); | ||
for(char parenthes: array){ | ||
if((parenthes == ')') || (parenthes=='}') || (parenthes==']')){ | ||
if(stack.isEmpty()) return false; // invalid | ||
|
||
char myPair = pMap.get(parenthes); | ||
if(stack.peek()==myPair){ | ||
stack.pop(); | ||
} | ||
else return false; | ||
} | ||
else{ | ||
stack.push(parenthes); | ||
} | ||
} | ||
|
||
return stack.empty(); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.