Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 291363b

Browse files
Updated tasks 49-79
1 parent 4f82bb4 commit 291363b

File tree

18 files changed

+144
-138
lines changed

18 files changed

+144
-138
lines changed

‎README.md

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

‎src/main/java/g0001_0100/s0049_group_anagrams/Solution.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
44
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
5-
// #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%)
5+
// #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%)
66

77
import java.util.ArrayList;
8-
import java.util.Arrays;
98
import java.util.HashMap;
109
import java.util.List;
1110
import java.util.Map;
1211

12+
@SuppressWarnings("java:S3824")
1313
public class Solution {
1414
public List<List<String>> groupAnagrams(String[] strs) {
15-
Map<String, List<String>> hm = new HashMap<>();
16-
for (String s : strs) {
17-
char[] ch = s.toCharArray();
18-
Arrays.sort(ch);
19-
String temp = new String(ch);
20-
hm.computeIfAbsent(temp, k -> new ArrayList<>());
21-
hm.get(temp).add(s);
15+
Map<String, List<String>> anagrams = new HashMap<>();
16+
for (String word : strs) {
17+
char[] freq = new char[26];
18+
for (char c : word.toCharArray()) {
19+
freq[c - 'a']++;
20+
}
21+
String keyString = new String(freq);
22+
if (!anagrams.containsKey(keyString)) {
23+
anagrams.put(keyString, new ArrayList<>());
24+
}
25+
anagrams.get(keyString).add(word);
2226
}
23-
return (new ArrayList<>(hm.values()));
27+
return new ArrayList<>(anagrams.values());
2428
}
2529
}
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
11
package g0001_0100.s0051_n_queens;
22

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

66
import java.util.ArrayList;
7-
import java.util.Arrays;
7+
import java.util.LinkedList;
88
import java.util.List;
99

1010
public class Solution {
1111
public List<List<String>> solveNQueens(int n) {
12-
boolean[] pos = new boolean[n + 2 * n - 1 + 2 * n - 1];
13-
int[] pos2 = new int[n];
14-
List<List<String>> ans = new ArrayList<>();
15-
helper(n, 0, pos, pos2, ans);
16-
return ans;
12+
char[][] board = new char[n][n];
13+
for (int i = 0; i < n; i++) {
14+
for (int j = 0; j < n; j++) {
15+
board[i][j] = '.';
16+
}
17+
}
18+
List<List<String>> res = new ArrayList<>();
19+
int[] leftRow = new int[n];
20+
int[] upperDiagonal = new int[2 * n - 1];
21+
int[] lowerDiagonal = new int[2 * n - 1];
22+
solve(0, board, res, leftRow, lowerDiagonal, upperDiagonal);
23+
return res;
1724
}
1825

19-
private void helper(int n, int row, boolean[] pos, int[] pos2, List<List<String>> ans) {
20-
if (row == n) {
21-
construct(n, pos2, ans);
26+
void solve(
27+
int col,
28+
char[][] board,
29+
List<List<String>> res,
30+
int[] leftRow,
31+
int[] lowerDiagonal,
32+
int[] upperDiagonal) {
33+
if (col == board.length) {
34+
res.add(construct(board));
2235
return;
2336
}
24-
for (int i = 0; i < n; i++) {
25-
int index = n + 2 * n - 1 + n - 1 + i - row;
26-
if (pos[i] || pos[n + i + row] || pos[index]) {
27-
continue;
37+
for (int row = 0; row < board.length; row++) {
38+
if (leftRow[row] == 0
39+
&& lowerDiagonal[row + col] == 0
40+
&& upperDiagonal[board.length - 1 + col - row] == 0) {
41+
board[row][col] = 'Q';
42+
leftRow[row] = 1;
43+
lowerDiagonal[row + col] = 1;
44+
upperDiagonal[board.length - 1 + col - row] = 1;
45+
solve(col + 1, board, res, leftRow, lowerDiagonal, upperDiagonal);
46+
board[row][col] = '.';
47+
leftRow[row] = 0;
48+
lowerDiagonal[row + col] = 0;
49+
upperDiagonal[board.length - 1 + col - row] = 0;
2850
}
29-
pos[i] = true;
30-
pos[n + i + row] = true;
31-
pos[index] = true;
32-
pos2[row] = i;
33-
helper(n, row + 1, pos, pos2, ans);
34-
pos[i] = false;
35-
pos[n + i + row] = false;
36-
pos[index] = false;
3751
}
3852
}
3953

40-
private void construct(int n, int[] pos, List<List<String>> ans) {
41-
List<String> sol = new ArrayList<>();
42-
for (int r = 0; r < n; r++) {
43-
char[] queenRow = new char[n];
44-
Arrays.fill(queenRow, '.');
45-
queenRow[pos[r]] = 'Q';
46-
sol.add(new String(queenRow));
54+
List<String> construct(char[][] board) {
55+
List<String> res = new LinkedList<>();
56+
for (char[] chars : board) {
57+
String s = new String(chars);
58+
res.add(s);
4759
}
48-
ans.add(sol);
60+
returnres;
4961
}
5062
}

‎src/main/java/g0001_0100/s0053_maximum_subarray/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
44
// #Divide_and_Conquer #Data_Structure_I_Day_1_Array #Dynamic_Programming_I_Day_5
55
// #Udemy_Famous_Algorithm #Big_O_Time_O(n)_Space_O(1)
6-
// #2023_08_11_Time_1_ms_(100.00%)_Space_57.7_MB_(90.58%)
6+
// #2024_11_11_Time_1_ms_(99.32%)_Space_56.9_MB_(54.82%)
77

88
public class Solution {
99
public int maxSubArray(int[] nums) {

‎src/main/java/g0001_0100/s0055_jump_game/Solution.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,22 @@
22

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

77
public class Solution {
88
public boolean canJump(int[] nums) {
9-
int sz = nums.length;
10-
// we set 1 so it won't break on the first iteration
11-
int tmp = 1;
12-
for (int i = 0; i < sz; i++) {
13-
// we always deduct tmp for every iteration
14-
tmp--;
15-
if (tmp < 0) {
16-
// if from previous iteration tmp is already 0, it will be <0 here
17-
// leading to false value
18-
return false;
19-
}
20-
// we get the maximum value because this value is supposed
21-
// to be our iterator, if both values are 0, then the next
22-
// iteration we will return false
23-
// if either both or one of them are not 0 then we will keep doing this and check.
24-
25-
// We can stop the whole iteration with this condition. without this condition the code
26-
// runs in 2ms 79.6%, adding this condition improves the performance into 1ms 100%
27-
// because if the test case jump value is quite large, instead of just iterate, we can
28-
// just check using this condition
29-
// example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without
30-
// iterating whole array
31-
tmp = Math.max(tmp, nums[i]);
32-
if (i + tmp >= sz - 1) {
33-
return true;
9+
if (nums.length == 1) {
10+
return true;
11+
}
12+
if (nums[0] == 0) {
13+
return false;
14+
}
15+
int fin = nums.length - 1;
16+
for (int i = nums.length - 2; i >= 0; i--) {
17+
if ((nums[i] + i) >= fin) {
18+
fin = i;
3419
}
3520
}
36-
// we can just return true at the end, because if tmp is 0 on previous
37-
// iteration,
38-
// even though the next iteration index is the last one, it will return false under the
39-
// tmp<0 condition
40-
return true;
21+
return fin == 0;
4122
}
4223
}

‎src/main/java/g0001_0100/s0056_merge_intervals/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
import java.util.ArrayList;
88
import java.util.Arrays;

‎src/main/java/g0001_0100/s0062_unique_paths/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
44
// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
55
// #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
6-
// #2023_08_11_Time_0_ms_(100.00%)_Space_39.2_MB_(67.74%)
6+
// #2024_11_11_Time_0_ms_(100.00%)_Space_40.7_MB_(12.56%)
77

88
public class Solution {
99
public int uniquePaths(int m, int n) {

‎src/main/java/g0001_0100/s0064_minimum_path_sum/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
public class Solution {
88
public int minPathSum(int[][] grid) {

‎src/main/java/g0001_0100/s0070_climbing_stairs/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
44
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
55
// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
6-
// #2023_08_11_Time_0_ms_(100.00%)_Space_39.2_MB_(71.51%)
6+
// #2024_11_11_Time_0_ms_(100.00%)_Space_40.3_MB_(41.06%)
77

88
public class Solution {
99
public int climbStairs(int n) {

‎src/main/java/g0001_0100/s0072_edit_distance/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
44
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
55
// #Udemy_Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n2)
6-
// #2023_08_11_Time_4_ms_(90.13%)_Space_41.8_MB_(99.78%)
6+
// #2024_11_11_Time_3_ms_(97.19%)_Space_43.2_MB_(98.23%)
77

88
@SuppressWarnings("java:S2234")
99
public class Solution {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /