-
-
Notifications
You must be signed in to change notification settings - Fork 195
[TONY] WEEK 04 Solutions #407
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51e59ce
Valid Palindrome
TonyKim9401 cc47d80
Missing Number
TonyKim9401 ddc9916
Longest Consecutive Sequence
TonyKim9401 790350b
Word Search
TonyKim9401 ce5152f
Maximum Product Subarray
TonyKim9401 03bcdeb
Word Search
TonyKim9401 a31c5f6
Missing Number
TonyKim9401 aaf3487
Valid Palindrome
TonyKim9401 c6ad16b
Valid Palindrome
TonyKim9401 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,21 @@ | ||
// TC: O(n) | ||
// SC: O(n) | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
int output = 0; | ||
Set<Integer> set = new HashSet<>(); | ||
|
||
for (int num : nums) set.add(num); | ||
|
||
for (int num : nums) { | ||
int count = 1; | ||
if (!set.contains(num - count)){ | ||
while (set.contains(num + count)) { | ||
count += 1; | ||
} | ||
} | ||
output = Math.max(output, count); | ||
} | ||
return output; | ||
} | ||
} |
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 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public int maxProduct(int[] nums) { | ||
int currentMax = nums[0]; | ||
int currentMin = nums[0]; | ||
int maxProduct = nums[0]; | ||
|
||
for (int i = 1; i < nums.length; i++) { | ||
if (nums[i] < 0) { | ||
int temp = currentMax; | ||
currentMax = currentMin; | ||
currentMin = temp; | ||
} | ||
|
||
currentMax = Math.max(nums[i], currentMax * nums[i]); | ||
currentMin = Math.min(nums[i], currentMin * nums[i]); | ||
|
||
maxProduct = Math.max(maxProduct, currentMax); | ||
} | ||
|
||
return maxProduct; | ||
} | ||
} |
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,15 @@ | ||
// TC: O(n) | ||
// -> add all nums into set | ||
// SC: O(n) | ||
// -> set contains all nums' elements | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
for (int num : nums) set.add(num); | ||
|
||
int output = 0; | ||
while (set.contains(output)) output += 1; | ||
|
||
return output; | ||
} | ||
} |
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,21 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
int start = 0; | ||
int end = s.length() - 1; | ||
|
||
while (start < end) { | ||
while (!Character.isLetterOrDigit(s.charAt(start)) && start < end) start += 1; | ||
while (!Character.isLetterOrDigit(s.charAt(end)) && start < end) end -= 1; | ||
|
||
if (Character.toLowerCase(s.charAt(start)) | ||
!= Character.toLowerCase( s.charAt(end))) return false; | ||
|
||
start += 1; | ||
end -= 1; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,51 @@ | ||
// TC: O(n * m * 4^k); | ||
// -> The size of board: n * m | ||
// -> Check 4 directions by the given word's length: 4^k | ||
// SC: O(n * m + k) | ||
// -> boolean 2D array: n * M | ||
// -> recursive max k spaces | ||
class Solution { | ||
public boolean exist(char[][] board, String word) { | ||
// Mark visited path to do not go back. | ||
boolean[][] visit = new boolean[board.length][board[0].length]; | ||
|
||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[0].length; j++) { | ||
if (wordSearch(i, j, 0, word, board, visit)) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean wordSearch(int i, int j, int idx, String word, char[][] board, boolean[][] visit) { | ||
|
||
// When idx checking reach to the end of the length of the word then, return true | ||
if (idx == word.length()) return true; | ||
|
||
// Check if i and j are inside of the range | ||
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false; | ||
|
||
// Check if the coordinate equals to the charactor value | ||
if (board[i][j] != word.charAt(idx)) return false; | ||
if (visit[i][j]) return false; | ||
|
||
// Mark the coordinate as visited | ||
visit[i][j] = true; | ||
|
||
// If visited, the target is gonna be the next charactor | ||
idx += 1; | ||
|
||
// If any direction returns true then it is true | ||
if ( | ||
wordSearch(i+1, j, idx, word, board, visit) || | ||
wordSearch(i-1, j, idx, word, board, visit) || | ||
wordSearch(i, j+1, idx, word, board, visit) || | ||
wordSearch(i, j-1, idx, word, board, visit) | ||
) return true; | ||
|
||
// If visited wrong direction, turns it as false | ||
visit[i][j] = false; | ||
|
||
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.
와, 지난 주 대비 복잡도 분석이 갑자기 확 느신 느낌입니다! 👍