Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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;
}
}
13 changes: 13 additions & 0 deletions missing-number/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TC: O(n log n)
// SC: O(n)
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int idx = 1;
int n = nums.length;
for (; idx < n; idx++) {
if (nums[idx] - 1 != nums[idx-1]) return nums[idx] - 1;
}
return nums[idx-1] == n ? 0 : n;
}
}
37 changes: 37 additions & 0 deletions valid-palindrome/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// TC: O(n)
// SC: O(n)
class Solution {
public boolean isPalindrome(String s) {
String target = checkString(s);
return checkPalindrome(target);
}

private String checkString(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (c >= 'a' && c <= 'z') {
sb.append(c);
}
if (c >= 'A' && c <= 'Z') {
c = (char)(c - 'A' + 'a');
sb.append(c);
}
if (c >= '0' && c <= '9') {
sb.append(c);
}
}
return sb.toString();
}

private Boolean checkPalindrome(String target) {
int start = 0;
int end = target.length() - 1;

while (start < end) {
if (target.charAt(start) != target.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;
}
}