Skip to content

Commit 08a3a2b

Browse files
authored
Merge pull request #407 from TonyKim9401/main
[TONY] WEEK 04 Solutions
2 parents 7c3afab + c6ad16b commit 08a3a2b

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
int output = 0;
6+
Set<Integer> set = new HashSet<>();
7+
8+
for (int num : nums) set.add(num);
9+
10+
for (int num : nums) {
11+
int count = 1;
12+
if (!set.contains(num - count)){
13+
while (set.contains(num + count)) {
14+
count += 1;
15+
}
16+
}
17+
output = Math.max(output, count);
18+
}
19+
return output;
20+
}
21+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public int maxProduct(int[] nums) {
5+
int currentMax = nums[0];
6+
int currentMin = nums[0];
7+
int maxProduct = nums[0];
8+
9+
for (int i = 1; i < nums.length; i++) {
10+
if (nums[i] < 0) {
11+
int temp = currentMax;
12+
currentMax = currentMin;
13+
currentMin = temp;
14+
}
15+
16+
currentMax = Math.max(nums[i], currentMax * nums[i]);
17+
currentMin = Math.min(nums[i], currentMin * nums[i]);
18+
19+
maxProduct = Math.max(maxProduct, currentMax);
20+
}
21+
22+
return maxProduct;
23+
}
24+
}

missing-number/TonyKim9401.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(n)
2+
// -> add all nums into set
3+
// SC: O(n)
4+
// -> set contains all nums' elements
5+
class Solution {
6+
public int missingNumber(int[] nums) {
7+
Set<Integer> set = new HashSet<>();
8+
for (int num : nums) set.add(num);
9+
10+
int output = 0;
11+
while (set.contains(output)) output += 1;
12+
13+
return output;
14+
}
15+
}

valid-palindrome/TonyKim9401.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public boolean isPalindrome(String s) {
5+
int start = 0;
6+
int end = s.length() - 1;
7+
8+
while (start < end) {
9+
while (!Character.isLetterOrDigit(s.charAt(start)) && start < end) start += 1;
10+
while (!Character.isLetterOrDigit(s.charAt(end)) && start < end) end -= 1;
11+
12+
if (Character.toLowerCase(s.charAt(start))
13+
!= Character.toLowerCase( s.charAt(end))) return false;
14+
15+
start += 1;
16+
end -= 1;
17+
}
18+
19+
return true;
20+
}
21+
}

word-search/TonyKim9401.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// TC: O(n * m * 4^k);
2+
// -> The size of board: n * m
3+
// -> Check 4 directions by the given word's length: 4^k
4+
// SC: O(n * m + k)
5+
// -> boolean 2D array: n * M
6+
// -> recursive max k spaces
7+
class Solution {
8+
public boolean exist(char[][] board, String word) {
9+
// Mark visited path to do not go back.
10+
boolean[][] visit = new boolean[board.length][board[0].length];
11+
12+
for (int i = 0; i < board.length; i++) {
13+
for (int j = 0; j < board[0].length; j++) {
14+
if (wordSearch(i, j, 0, word, board, visit)) return true;
15+
}
16+
}
17+
return false;
18+
}
19+
20+
private boolean wordSearch(int i, int j, int idx, String word, char[][] board, boolean[][] visit) {
21+
22+
// When idx checking reach to the end of the length of the word then, return true
23+
if (idx == word.length()) return true;
24+
25+
// Check if i and j are inside of the range
26+
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false;
27+
28+
// Check if the coordinate equals to the charactor value
29+
if (board[i][j] != word.charAt(idx)) return false;
30+
if (visit[i][j]) return false;
31+
32+
// Mark the coordinate as visited
33+
visit[i][j] = true;
34+
35+
// If visited, the target is gonna be the next charactor
36+
idx += 1;
37+
38+
// If any direction returns true then it is true
39+
if (
40+
wordSearch(i+1, j, idx, word, board, visit) ||
41+
wordSearch(i-1, j, idx, word, board, visit) ||
42+
wordSearch(i, j+1, idx, word, board, visit) ||
43+
wordSearch(i, j-1, idx, word, board, visit)
44+
) return true;
45+
46+
// If visited wrong direction, turns it as false
47+
visit[i][j] = false;
48+
49+
return false;
50+
}
51+
}

0 commit comments

Comments
 (0)