Skip to content

[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 9 commits into from
Sep 4, 2024
Merged
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
21 changes: 21 additions & 0 deletions longest-consecutive-sequence/TonyKim9401.java
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;
}
}
24 changes: 24 additions & 0 deletions maximum-product-subarray/TonyKim9401.java
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;
}
}
15 changes: 15 additions & 0 deletions missing-number/TonyKim9401.java
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;
}
}
21 changes: 21 additions & 0 deletions valid-palindrome/TonyKim9401.java
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;
}
}
51 changes: 51 additions & 0 deletions word-search/TonyKim9401.java
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
Comment on lines +1 to +6
Copy link
Member

Choose a reason for hiding this comment

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

와, 지난 주 대비 복잡도 분석이 갑자기 확 느신 느낌입니다! 👍

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;
}
}