Skip to content

Tasks 49 79 #1860

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 8 commits into from
Nov 11, 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
44 changes: 22 additions & 22 deletions README.md

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/main/java/g0001_0100/s0049_group_anagrams/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2023_08_11_Time_6_ms_(92.28%)_Space_46.4_MB_(98.50%)
// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2024_11_11_Time_6_ms_(97.61%)_Space_47.7_MB_(69.56%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("java:S3824")
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> hm = new HashMap<>();
for (String s : strs) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
String temp = new String(ch);
hm.computeIfAbsent(temp, k -> new ArrayList<>());
hm.get(temp).add(s);
Map<String, List<String>> anagrams = new HashMap<>();
for (String word : strs) {
char[] freq = new char[26];
for (char c : word.toCharArray()) {
freq[c - 'a']++;
}
String keyString = new String(freq);
if (!anagrams.containsKey(keyString)) {
anagrams.put(keyString, new ArrayList<>());
}
anagrams.get(keyString).add(word);
}
return (new ArrayList<>(hm.values()));
return new ArrayList<>(anagrams.values());
}
}
72 changes: 42 additions & 30 deletions src/main/java/g0001_0100/s0051_n_queens/Solution.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
package g0001_0100.s0051_n_queens;

// #Hard #Top_100_Liked_Questions #Array #Backtracking #Big_O_Time_O(N!)_Space_O(N)
// #2023_08_11_Time_1_ms_(100.00%)_Space_43.6_MB_(97.17%)
// #2024_11_11_Time_1_ms_(99.77%)_Space_44.8_MB_(61.16%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution {
public List<List<String>> solveNQueens(int n) {
boolean[] pos = new boolean[n + 2 * n - 1 + 2 * n - 1];
int[] pos2 = new int[n];
List<List<String>> ans = new ArrayList<>();
helper(n, 0, pos, pos2, ans);
return ans;
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '.';
}
}
List<List<String>> res = new ArrayList<>();
int[] leftRow = new int[n];
int[] upperDiagonal = new int[2 * n - 1];
int[] lowerDiagonal = new int[2 * n - 1];
solve(0, board, res, leftRow, lowerDiagonal, upperDiagonal);
return res;
}

private void helper(int n, int row, boolean[] pos, int[] pos2, List<List<String>> ans) {
if (row == n) {
construct(n, pos2, ans);
void solve(
int col,
char[][] board,
List<List<String>> res,
int[] leftRow,
int[] lowerDiagonal,
int[] upperDiagonal) {
if (col == board.length) {
res.add(construct(board));
return;
}
for (int i = 0; i < n; i++) {
int index = n + 2 * n - 1 + n - 1 + i - row;
if (pos[i] || pos[n + i + row] || pos[index]) {
continue;
for (int row = 0; row < board.length; row++) {
if (leftRow[row] == 0
&& lowerDiagonal[row + col] == 0
&& upperDiagonal[board.length - 1 + col - row] == 0) {
board[row][col] = 'Q';
leftRow[row] = 1;
lowerDiagonal[row + col] = 1;
upperDiagonal[board.length - 1 + col - row] = 1;
solve(col + 1, board, res, leftRow, lowerDiagonal, upperDiagonal);
board[row][col] = '.';
leftRow[row] = 0;
lowerDiagonal[row + col] = 0;
upperDiagonal[board.length - 1 + col - row] = 0;
}
pos[i] = true;
pos[n + i + row] = true;
pos[index] = true;
pos2[row] = i;
helper(n, row + 1, pos, pos2, ans);
pos[i] = false;
pos[n + i + row] = false;
pos[index] = false;
}
}

private void construct(int n, int[] pos, List<List<String>> ans) {
List<String> sol = new ArrayList<>();
for (int r = 0; r < n; r++) {
char[] queenRow = new char[n];
Arrays.fill(queenRow, '.');
queenRow[pos[r]] = 'Q';
sol.add(new String(queenRow));
List<String> construct(char[][] board) {
List<String> res = new LinkedList<>();
for (char[] chars : board) {
String s = new String(chars);
res.add(s);
}
ans.add(sol);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Divide_and_Conquer #Data_Structure_I_Day_1_Array #Dynamic_Programming_I_Day_5
// #Udemy_Famous_Algorithm #Big_O_Time_O(n)_Space_O(1)
// #2023_08_11_Time_1_ms_(100.00%)_Space_57.7_MB_(90.58%)
// #2024_11_11_Time_1_ms_(99.32%)_Space_56.9_MB_(54.82%)

public class Solution {
public int maxSubArray(int[] nums) {
Expand Down
43 changes: 12 additions & 31 deletions src/main/java/g0001_0100/s0055_jump_game/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,22 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Greedy
// #Algorithm_II_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_4 #Udemy_Arrays
// #Big_O_Time_O(n)_Space_O(1) #2023_08_11_Time_2_ms_(79.47%)_Space_44.8_MB_(22.14%)
// #Big_O_Time_O(n)_Space_O(1) #2024_11_11_Time_1_ms_(100.00%)_Space_45.6_MB_(44.48%)

public class Solution {
public boolean canJump(int[] nums) {
int sz = nums.length;
// we set 1 so it won't break on the first iteration
int tmp = 1;
for (int i = 0; i < sz; i++) {
// we always deduct tmp for every iteration
tmp--;
if (tmp < 0) {
// if from previous iteration tmp is already 0, it will be <0 here
// leading to false value
return false;
}
// we get the maximum value because this value is supposed
// to be our iterator, if both values are 0, then the next
// iteration we will return false
// if either both or one of them are not 0 then we will keep doing this and check.

// We can stop the whole iteration with this condition. without this condition the code
// runs in 2ms 79.6%, adding this condition improves the performance into 1ms 100%
// because if the test case jump value is quite large, instead of just iterate, we can
// just check using this condition
// example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without
// iterating whole array
tmp = Math.max(tmp, nums[i]);
if (i + tmp >= sz - 1) {
return true;
if (nums.length == 1) {
return true;
}
if (nums[0] == 0) {
return false;
}
int fin = nums.length - 1;
for (int i = nums.length - 2; i >= 0; i--) {
if ((nums[i] + i) >= fin) {
fin = i;
}
}
// we can just return true at the end, because if tmp is 0 on previous
// iteration,
// even though the next iteration index is the last one, it will return false under the
// tmp<0 condition
return true;
return fin == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting
// #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix
// #Big_O_Time_O(n_log_n)_Space_O(n) #2023_08_11_Time_8_ms_(96.27%)_Space_45.2_MB_(90.13%)
// #Big_O_Time_O(n_log_n)_Space_O(n) #2024_11_11_Time_7_ms_(98.37%)_Space_46.8_MB_(11.43%)

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0062_unique_paths/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
// #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
// #2023_08_11_Time_0_ms_(100.00%)_Space_39.2_MB_(67.74%)
// #2024_11_11_Time_0_ms_(100.00%)_Space_40.7_MB_(12.56%)

public class Solution {
public int uniquePaths(int m, int n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
// #2023_08_11_Time_0_ms_(100.00%)_Space_44_MB_(58.56%)
// #2024_11_11_Time_1_ms_(99.73%)_Space_47.5_MB_(44.29%)

public class Solution {
public int minPathSum(int[][] grid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
// #2023_08_11_Time_0_ms_(100.00%)_Space_39.2_MB_(71.51%)
// #2024_11_11_Time_0_ms_(100.00%)_Space_40.3_MB_(41.06%)

public class Solution {
public int climbStairs(int n) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0072_edit_distance/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
// #Udemy_Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n2)
// #2023_08_11_Time_4_ms_(90.13%)_Space_41.8_MB_(99.78%)
// #2024_11_11_Time_3_ms_(97.19%)_Space_43.2_MB_(98.23%)

@SuppressWarnings("java:S2234")
public class Solution {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(m*n)_Space_O(1)
// #2023_08_11_Time_1_ms_(79.07%)_Space_44.4_MB_(94.19%)
// #2024_11_11_Time_0_ms_(100.00%)_Space_45.6_MB_(50.86%)

public class Solution {
// Approach: Use first row and first column for storing whether in future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1)
// #2023_08_11_Time_0_ms_(100.00%)_Space_40.9_MB_(71.91%)
// #2024_11_11_Time_0_ms_(100.00%)_Space_42.2_MB_(40.02%)

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0075_sort_colors/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
// #Data_Structure_II_Day_2_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
// #2023_08_11_Time_0_ms_(100.00%)_Space_41_MB_(50.59%)
// #2024_11_11_Time_0_ms_(100.00%)_Space_41.5_MB_(91.22%)

public class Solution {
public void sortColors(int[] nums) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
// #Level_2_Day_14_Sliding_Window/Two_Pointer #Big_O_Time_O(s.length())_Space_O(1)
// #2023_08_11_Time_2_ms_(99.94%)_Space_43.6_MB_(93.87%)
// #2024_11_11_Time_2_ms_(99.83%)_Space_44.5_MB_(89.46%)

public class Solution {
public String minWindow(String s, String t) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0078_subsets/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation #Backtracking
// #Algorithm_II_Day_9_Recursion_Backtracking #Udemy_Backtracking/Recursion
// #Big_O_Time_O(2^n)_Space_O(n*2^n) #2023_08_11_Time_1_ms_(70.60%)_Space_41.8_MB_(71.73%)
// #Big_O_Time_O(2^n)_Space_O(n*2^n) #2024_11_11_Time_0_ms_(100.00%)_Space_43_MB_(12.48%)

import java.util.ArrayList;
import java.util.List;
Expand Down
71 changes: 40 additions & 31 deletions src/main/java/g0001_0100/s0079_word_search/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,52 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n)
// #2023_08_11_Time_157_ms_(78.97%)_Space_40.5_MB_(84.41%)
// #2024_11_11_Time_64_ms_(98.51%)_Space_41.6_MB_(51.63%)

public class Solution {
private boolean backtrace(
char[][] board, boolean[][] visited, String word, int index, int x, int y) {
if (index == word.length()) {
return true;
}
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y]) {
return false;
}
visited[x][y] = true;
if (word.charAt(index) == board[x][y]) {
boolean res =
backtrace(board, visited, word, index + 1, x, y + 1)
|| backtrace(board, visited, word, index + 1, x, y - 1)
|| backtrace(board, visited, word, index + 1, x + 1, y)
|| backtrace(board, visited, word, index + 1, x - 1, y);
if (!res) {
visited[x][y] = false;
}
return res;
} else {
visited[x][y] = false;
return false;
}
}
private boolean exists = false;

public boolean exist(char[][] board, String word) {
boolean[][] visited = 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 (backtrace(board, visited, word, 0, i, j)) {
return true;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
dfs(board, word, 1, i, j);
}
}
}
return false;
return exists;
}

private void dfs(char[][] board, String word, int wordIndex, int i, int j) {
if (wordIndex == word.length()) {
exists = true;
return;
}
char currentChar = board[i][j];
char nextChar = word.charAt(wordIndex);
if (i > 0 && board[i - 1][j] == nextChar) {
// go up
board[i][j] = '-';
dfs(board, word, wordIndex + 1, i - 1, j);
board[i][j] = currentChar;
}
if (j > 0 && board[i][j - 1] == nextChar) {
// go left
board[i][j] = '-';
dfs(board, word, wordIndex + 1, i, j - 1);
board[i][j] = currentChar;
}
if (i < board.length - 1 && board[i + 1][j] == nextChar) {
// go down
board[i][j] = '-';
dfs(board, word, wordIndex + 1, i + 1, j);
board[i][j] = currentChar;
}
if (j < board[0].length - 1 && board[i][j + 1] == nextChar) {
// go right
board[i][j] = '-';
dfs(board, word, wordIndex + 1, i, j + 1);
board[i][j] = currentChar;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class SolutionTest {
@Test
void groupAnagrams() {
List<List<String>> expected = new ArrayList<>();
expected.add(Arrays.asList("tan", "nat"));
expected.add(Arrays.asList("eat", "tea", "ate"));
expected.add(Arrays.asList("bat"));
expected.add(Arrays.asList("tan", "nat"));
List<List<String>> actual =
new Solution()
.groupAnagrams(new String[] {"eat", "tea", "tan", "ate", "nat", "bat"});
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/g0001_0100/s0051_n_queens/SolutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ void solveNQueens() {
equalTo(
ArrayUtils.getLists(
new String[][] {
{".Q..", "...Q", "Q...", "..Q."},
{"..Q.", "Q...", "...Q", ".Q.."}
{"..Q.", "Q...", "...Q", ".Q.."},
{".Q..", "...Q", "Q...", "..Q."}
})));
}

Expand Down
Loading