-
Notifications
You must be signed in to change notification settings - Fork 125
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
[jaejeong1] Week 04 Solutions #415
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92e634e
valid palindrome solution
wad-jangjaejeong 7c4ce77
missing number solution
wad-jangjaejeong a95ca38
longest consecutive sequence solution
wad-jangjaejeong b9db47b
valid palindrome 공간복잡도 개선
wad-jangjaejeong f757103
word search solution
wad-jangjaejeong c49237b
시간복잡도 표기 수정
jaejeong1 232f11a
word search 공간복잡도 업데이트
wad-jangjaejeong 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 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,46 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class SolutionLongestConsecutiveSequence { | ||
|
||
public int longestConsecutive(int[] nums) { | ||
// 정렬되지 않은 정수 nums 배열이 주어지면 가장 긴 연속 요소 시퀀스 길이를 반환 | ||
// O(N) 시간 내 실행되야함 | ||
// 전부 해시맵에 때려넣고, 키를 꺼내 연속 요소가 있는지 확인한다 | ||
// 연속 요소가 있으면 answer를 1 증가시키고, 연속 요소는 제거한다 | ||
// 시간복잡도: O(N), 공간복잡도: O(N) | ||
|
||
Set<Integer> set = new HashSet<>(); | ||
for (var num : nums) { | ||
set.add(num); | ||
} | ||
var answer = 0; | ||
for (var num : nums) { | ||
var length = 1; | ||
|
||
if (set.contains(num-1)) { | ||
set.remove(num); | ||
var minusKey = num; | ||
while (set.contains(--minusKey)) { | ||
length++; | ||
set.remove(minusKey); | ||
} | ||
} | ||
|
||
if (set.contains(num+1)) { | ||
set.remove(num); | ||
var plusKey = num; | ||
while (set.contains(++plusKey)) { | ||
length++; | ||
set.remove(plusKey); | ||
} | ||
} | ||
|
||
if (length > answer) { | ||
answer = length; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
} |
This file contains 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,15 @@ | ||
class SolutionMissingNumber { | ||
public int missingNumber(int[] nums) { | ||
// N = 배열의 길이 | ||
// 0 ~ N 의 합을 구하고, nums의 모든 값을 다 빼면 정답 | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
var N = nums.length; | ||
var sum = (N * (N+1)) / 2; | ||
|
||
for (var num : nums) { | ||
sum -= num; | ||
} | ||
|
||
return sum; | ||
} | ||
} |
This file contains 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,56 @@ | ||
class SolutionValidPalindrome { | ||
|
||
public boolean isPalindrome(String s) { | ||
// 대문자를 소문자로 변환 | ||
// 영문자와 숫자만 남기고 모두 제거 | ||
// 앞뒤로 읽어도 같은 경우 팰린드롬 | ||
// 종류 별 처리 방법 | ||
// 공백: 무시 | ||
// 소문자, 숫자: 통과 | ||
// 그 외: 무시 | ||
// 종료 조건: lt >= rt | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
var idxLt = 0; | ||
var idxRt = s.length() - 1; | ||
|
||
char charLt; | ||
char charRt; | ||
|
||
while (idxLt < idxRt) { | ||
charLt = s.charAt(idxLt); | ||
charRt = s.charAt(idxRt); | ||
|
||
// 영문 또는 숫자일때까지 lt++ | ||
while(!Character.isLetterOrDigit(charLt) && idxLt < s.length() - 1) { | ||
idxLt++; | ||
charLt = s.charAt(idxLt); | ||
} | ||
|
||
// 영문 또는 숫자일때까지 rt-- | ||
while(!Character.isLetterOrDigit(charRt) && idxRt > 0) { | ||
idxRt--; | ||
charRt = s.charAt(idxRt); | ||
} | ||
|
||
// 대문자면 소문자로 변환 + lt, rt 범위가 겹쳤거나 lt와 rt 가 다르면 false 반환 | ||
if (idxLt < idxRt) { | ||
if (Character.isUpperCase(charLt)) { | ||
charLt = Character.toLowerCase(charLt); | ||
} | ||
|
||
if (Character.isUpperCase(charRt)) { | ||
charRt = Character.toLowerCase(charRt); | ||
} | ||
|
||
if (charLt != charRt) { | ||
return false; | ||
} | ||
} | ||
|
||
idxLt++; | ||
idxRt--; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains 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,50 @@ | ||
class SolutionWordSearch { | ||
|
||
int[] dx = new int[]{0, 0, -1, 1}; | ||
int[] dy = new int[]{-1, 1, 0, 0}; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
// 상하좌우 방문 여부 체크하면서 DFS 탐색 | ||
// index 기준으로 word와 비교하면서 같을 때만 추가 탐색 | ||
// 시간복잡도: O(N) > 방문 여부 체크하면서 기방문한 곳은 탐색하지 않기 때문 | ||
// 공간복잡도: O(1) > 별도 메모리 할당하지 않음 | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[0].length; j++) { | ||
if (dfs(board, i, j, word, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public boolean dfs(char[][] board, int x, int y, String word, int index) { | ||
if (index >= word.length()) { | ||
return true; | ||
} | ||
// board 밖이면 return false | ||
if (x >= board.length || y >= board[0].length || x < 0 || y < 0) { | ||
return false; | ||
} | ||
// 이미 방문했거나 정답 조건에 맞지 않으면 return false | ||
if (board[x][y] != word.charAt(index)) { | ||
return false; | ||
} | ||
|
||
// 현재 위치의 문자를 임시로 변경하여 방문 처리 | ||
char temp = board[x][y]; | ||
board[x][y] = '#'; | ||
|
||
index++; | ||
for (int i = 0; i < 4; i++) { | ||
if (dfs(board, x + dx[i], y + dy[i], word, index)) { | ||
return true; | ||
} | ||
} | ||
|
||
// 방문 처리를 되돌림 | ||
board[x][y] = temp; | ||
|
||
return false; | ||
} | ||
} |
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.
기존 문자열을 수정하지 않고 이렇게도 할 수 있었군요! 많이 배웠습니다!