Skip to content

Commit

Permalink
Updated tasks 221-1143
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Nov 17, 2024
1 parent a1dcd8e commit 7227461
Show file tree
Hide file tree
Showing 28 changed files with 169 additions and 160 deletions.
132 changes: 66 additions & 66 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g0001_0100.s0006_zigzag_conversion;

// #Medium #String #2024_01_04_Time_2_ms_(99.60%)_Space_44.7_MB_(38.67%)
// #Medium #String #2024_11_17_Time_2_ms_(99.71%)_Space_44.5_MB_(94.69%)

public class Solution {
public String convert(String s, int numRows) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0201_0300.s0221_maximal_square;

// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_16
// #Big_O_Time_O(m*n)_Space_O(m*n) #2022_07_04_Time_7_ms_(72.35%)_Space_59.5_MB_(10.55%)
// #Big_O_Time_O(m*n)_Space_O(m*n) #2024_11_16_Time_6_ms_(97.07%)_Space_60.3_MB_(39.55%)

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

// #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
// #Big_O_Time_O(n)_Space_O(n) #2022_07_04_Time_0_ms_(100.00%)_Space_42_MB_(20.73%)
// #Big_O_Time_O(n)_Space_O(n) #2024_11_16_Time_0_ms_(100.00%)_Space_40.6_MB_(95.51%)

import com_github_leetcode.TreeNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
// #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree #Big_O_Time_O(n)_Space_O(n)
// #2022_07_04_Time_1_ms_(78.91%)_Space_45.3_MB_(58.87%)
// #2024_11_16_Time_0_ms_(100.00%)_Space_44.3_MB_(63.70%)

import com_github_leetcode.TreeNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
// #2022_07_04_Time_6_ms_(76.07%)_Space_97.6_MB_(56.14%)
// #2024_11_16_Time_4_ms_(84.46%)_Space_69_MB_(17.17%)

import com_github_leetcode.ListNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree
// #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
// #2022_07_04_Time_10_ms_(56.51%)_Space_47.4_MB_(45.84%)
// #2024_11_16_Time_6_ms_(100.00%)_Space_44_MB_(98.99%)

import com_github_leetcode.TreeNode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package g0201_0300.s0238_product_of_array_except_self;

// #Medium #Top_100_Liked_Questions #Array #Prefix_Sum #Data_Structure_II_Day_5_Array #Udemy_Arrays
// #Big_O_Time_O(n^2)_Space_O(n) #2022_07_04_Time_1_ms_(100.00%)_Space_50.8_MB_(85.60%)
// #Big_O_Time_O(n^2)_Space_O(n) #2024_11_16_Time_1_ms_(99.66%)_Space_55.1_MB_(79.02%)

public class Solution {
public int[] productExceptSelf(int[] nums) {
int product = 1;
int[] ans = new int[nums.length];
for (int num : nums) {
product = product * num;
}
int[] res = new int[nums.length];
int prefixProduct = 1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
ans[i] = product / nums[i];
} else {
int p = 1;
for (int j = 0; j < nums.length; j++) {
if (j != i) {
p = p * nums[j];
}
}
ans[i] = p;
}
res[i] = prefixProduct;
prefixProduct *= nums[i];
}
int suffixProduct = 1;
for (int i = nums.length - 1; i >= 0; i--) {
res[i] *= suffixProduct;
suffixProduct *= nums[i];
}
return ans;
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
// #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2022_07_04_Time_58_ms_(52.28%)_Space_145_MB_(50.60%)
// #2024_11_16_Time_26_ms_(95.89%)_Space_59.6_MB_(38.70%)

import java.util.LinkedList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Divide_and_Conquer
// #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8 #Big_O_Time_O(n+m)_Space_O(1)
// #2022_07_04_Time_7_ms_(86.73%)_Space_58.4_MB_(9.95%)
// #2024_11_16_Time_5_ms_(99.92%)_Space_45.8_MB_(60.21%)

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

// #Easy #Top_100_Liked_Questions #Array #Two_Pointers #Algorithm_I_Day_3_Two_Pointers
// #Programming_Skills_I_Day_6_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
// #2022_07_06_Time_2_ms_(79.54%)_Space_55.7_MB_(5.98%)
// #2024_11_16_Time_2_ms_(83.99%)_Space_45.9_MB_(50.99%)

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

// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Two_Pointers #Bit_Manipulation
// #Binary_Search_II_Day_5 #Big_O_Time_O(n)_Space_O(n)
// #2022_07_06_Time_2_ms_(99.82%)_Space_61.1_MB_(83.92%)
// #2024_11_16_Time_2_ms_(97.52%)_Space_59.9_MB_(5.22%)

public class Solution {
public int findDuplicate(int[] nums) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0201_0300.s0295_find_median_from_data_stream;

// #Hard #Top_100_Liked_Questions #Sorting #Two_Pointers #Design #Heap_Priority_Queue #Data_Stream
// #Big_O_Time_O(n*log_n)_Space_O(n) #2022_07_06_Time_151_ms_(80.24%)_Space_125.2_MB_(44.11%)
// #Big_O_Time_O(n*log_n)_Space_O(n) #2024_11_16_Time_83_ms_(99.56%)_Space_63.4_MB_(77.85%)

import java.util.PriorityQueue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Binary_Search
// #Algorithm_II_Day_16_Dynamic_Programming #Binary_Search_II_Day_3 #Dynamic_Programming_I_Day_18
// #Udemy_Dynamic_Programming #Big_O_Time_O(n*log_n)_Space_O(n)
// #2022_07_06_Time_3_ms_(98.63%)_Space_44.3_MB_(60.27%)
// #2024_11_16_Time_3_ms_(95.75%)_Space_43.7_MB_(93.58%)

public class Solution {
public int lengthOfLIS(int[] nums) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0301_0400/s0322_coin_change/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount)
// #2022_07_09_Time_17_ms_(91.77%)_Space_41.8_MB_(95.50%)
// #2024_11_16_Time_12_ms_(92.59%)_Space_44.3_MB_(64.02%)

public class Solution {
public int coinChange(int[] coins, int amount) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0301_0400/s0338_counting_bits/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0301_0400.s0338_counting_bits;

// #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
// #Big_O_Time_O(num)_Space_O(num) #2022_07_10_Time_2_ms_(86.73%)_Space_48.3_MB_(31.59%)
// #Big_O_Time_O(num)_Space_O(num) #2024_11_16_Time_2_ms_(96.37%)_Space_46.4_MB_(70.53%)

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

// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting
// #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue
// #Big_O_Time_O(n*log(n))_Space_O(k) #2022_07_11_Time_9_ms_(97.93%)_Space_48.5_MB_(83.34%)
// #Big_O_Time_O(n*log(n))_Space_O(k) #2024_11_17_Time_9_ms_(97.30%)_Space_45.4_MB_(92.52%)

import java.util.Arrays;
import java.util.PriorityQueue;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0301_0400/s0394_decode_string/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0301_0400.s0394_decode_string;

// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
// #Big_O_Time_O(n)_Space_O(n) #2022_07_15_Time_1_ms_(87.68%)_Space_41.2_MB_(83.30%)
// #Big_O_Time_O(n)_Space_O(n) #2024_11_17_Time_0_ms_(100.00%)_Space_41.5_MB_(58.38%)

public class Solution {
private int i = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package g0401_0500.s0416_partition_equal_subset_sum;

// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2022_12_29_Time_27_ms_(94.53%)_Space_41.8_MB_(95.29%)
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_11_17_Time_5_ms_(99.88%)_Space_42.2_MB_(85.79%)

public class Solution {
public boolean canPartition(int[] nums) {
int sums = 0;
for (int num : nums) {
sums += num;
int sum = 0;
for (int val : nums) {
sum += val;
}
if (sums % 2 == 1) {
if (sum % 2 != 0) {
return false;
}
sums /= 2;
boolean[] dp = new boolean[sums + 1];
dp[0] = true;
for (int num : nums) {
for (int sum = sums; sum >= num; sum--) {
dp[sum] = dp[sum] || dp[sum - num];
sum /= 2;
boolean[] set = new boolean[sum + 1];
int[] arr = new int[sum + 2];
int top = 0;
for (int val : nums) {
for (int i = top; i > -1; i--) {
int tempSum = val + arr[i];
if (tempSum <= sum && !set[tempSum]) {
if (tempSum == sum) {
return true;
}
set[tempSum] = true;
arr[++top] = tempSum;
}
}
}
return dp[sums];
return false;
}
}
36 changes: 18 additions & 18 deletions src/main/java/g0401_0500/s0437_path_sum_iii/Solution.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package g0401_0500.s0437_path_sum_iii;

// #Medium #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree #Big_O_Time_O(n)_Space_O(n)
// #2022_07_16_Time_18_ms_(45.66%)_Space_42_MB_(88.96%)
// #2024_11_17_Time_2_ms_(100.00%)_Space_44.7_MB_(11.66%)

import com_github_leetcode.TreeNode;
import java.util.HashMap;

/*
* Definition for a binary tree node.
Expand All @@ -21,28 +22,27 @@
* }
*/
public class Solution {
private int count = 0;

public int pathSum(TreeNode root, int targetSum) {
if (root == null) {
return 0;
}
helper(root, targetSum, 0);
pathSum(root.left, targetSum);
pathSum(root.right, targetSum);
return count;
HashMap<Long, Integer> h = new HashMap<>();
return dfs(root, targetSum, h, 0L);
}

public void helper(TreeNode node, int targetSum, long currSum) {
currSum += node.val;
if (targetSum == currSum) {
count++;
int dfs(TreeNode root, int t, HashMap<Long, Integer> h, Long cs) {
int s = 0;
if (root == null) {
return 0;
}
if (node.left != null) {
helper(node.left, targetSum, currSum);
Long k = cs + root.val;
if (k == t) {
s += 1;
}
if (node.right != null) {
helper(node.right, targetSum, currSum);
if (h.getOrDefault(k - t, 0) > 0) {
s += h.get(k - t);
}
h.put(k, h.getOrDefault(k, 0) + 1);
s += dfs(root.left, t, h, k);
s += dfs(root.right, t, h, k);
h.put(k, h.getOrDefault(k, 0) - 1);
return s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// #Medium #Top_100_Liked_Questions #String #Hash_Table #Sliding_Window
// #Algorithm_II_Day_5_Sliding_Window #Programming_Skills_II_Day_12
// #Level_1_Day_12_Sliding_Window/Two_Pointer #Big_O_Time_O(n+m)_Space_O(1)
// #2022_07_16_Time_6_ms_(99.03%)_Space_47.9_MB_(50.50%)
// #2024_11_17_Time_3_ms_(99.83%)_Space_44.7_MB_(74.83%)

import java.util.ArrayList;
import java.util.List;
Expand Down
54 changes: 31 additions & 23 deletions src/main/java/g0401_0500/s0494_target_sum/Solution.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package g0401_0500.s0494_target_sum;

// #Medium #Array #Dynamic_Programming #Backtracking #Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s))
// #2022_07_21_Time_9_ms_(79.99%)_Space_45.2_MB_(32.79%)
// #2024_11_17_Time_4_ms_(92.28%)_Space_42.7_MB_(57.04%)

public class Solution {
public int findTargetSumWays(int[] nums, int s) {
int sum = 0;
s = Math.abs(s);
for (int num : nums) {
sum += num;
public int findTargetSumWays(int[] nums, int target) {
int totalSum = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
totalSum += nums[i];
}
// Invalid s, just return 0
if (s > sum || (sum + s) % 2 != 0) {
int sum = totalSum - target;
if (sum < 0 || sum % 2 == 1) {
return 0;
}
int[][] dp = new int[(sum + s) / 2 + 1][nums.length + 1];
dp[0][0] = 1;
// empty knapsack must be processed specially
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
dp[0][i + 1] = dp[0][i] * 2;
} else {
dp[0][i + 1] = dp[0][i];
}
return solve(nums, sum / 2);
}

private int solve(int[] nums, int target) {
int[] prev = new int[target + 1];
if (nums[0] == 0) {
prev[0] = 2;
} else {
prev[0] = 1;
}
if (nums[0] != 0 && nums[0] <= target) {
prev[nums[0]] = 1;
}
for (int i = 1; i < dp.length; i++) {
for (int j = 0; j < nums.length; j++) {
dp[i][j + 1] += dp[i][j];
if (nums[j] <= i) {
dp[i][j + 1] += dp[i - nums[j]][j];
int n = nums.length;
for (int i = 1; i < n; i++) {
int[] curr = new int[target + 1];
for (int j = 0; j <= target; j++) {
int taken = 0;
if (j >= nums[i]) {
taken = prev[j - nums[i]];
}
int nonTaken = prev[j];
curr[j] = taken + nonTaken;
}
prev = curr;
}
return dp[(sum + s) / 2][nums.length];
return prev[target];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Easy #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree
// #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
// #2022_08_02_Time_1_ms_(65.86%)_Space_43.5_MB_(33.52%)
// #2024_11_17_Time_0_ms_(100.00%)_Space_44.5_MB_(74.23%)

import com_github_leetcode.TreeNode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0501_0600.s0560_subarray_sum_equals_k;

// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Prefix_Sum #Data_Structure_II_Day_5_Array
// #Big_O_Time_O(n)_Space_O(n) #2022_08_03_Time_21_ms_(98.97%)_Space_46.8_MB_(88.27%)
// #Big_O_Time_O(n)_Space_O(n) #2024_11_17_Time_22_ms_(95.17%)_Space_47.2_MB_(6.13%)

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0601_0700.s0647_palindromic_substrings;

// #Medium #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n)
// #2022_03_21_Time_2_ms_(98.77%)_Space_41.7_MB_(75.10%)
// #2024_11_17_Time_2_ms_(99.31%)_Space_41.4_MB_(77.04%)

public class Solution {
private void expand(char[] a, int l, int r, int[] res) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0701_0800.s0739_daily_temperatures;

// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #Programming_Skills_II_Day_6
// #Big_O_Time_O(n)_Space_O(n) #2022_03_25_Time_10_ms_(94.99%)_Space_118.3_MB_(70.21%)
// #Big_O_Time_O(n)_Space_O(n) #2024_11_17_Time_8_ms_(96.83%)_Space_60.6_MB_(55.93%)

@SuppressWarnings("java:S135")
public class Solution {
Expand Down
Loading

0 comments on commit 7227461

Please sign in to comment.