-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
169 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 11 additions & 18 deletions
29
src/main/java/g0201_0300/s0238_product_of_array_except_self/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 20 additions & 12 deletions
32
src/main/java/g0401_0500/s0416_partition_equal_subset_sum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/g0501_0600/s0560_subarray_sum_equals_k/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/g0601_0700/s0647_palindromic_substrings/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/g0701_0800/s0739_daily_temperatures/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.