Skip to content

Commit f8ac8e4

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents 01e785c + 260b0b3 commit f8ac8e4

6 files changed

+157
-4
lines changed

Diff for: README.md

+4-4
Large diffs are not rendered by default.

Diff for: c/0097-interleaving-string.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
bool isInterleave(char *s1, char *s2, char *s3) {
2+
int len1 = strlen(s1);
3+
int len2 = strlen(s2);
4+
int len3 = strlen(s3);
5+
6+
// If the lengths of the input strings don't add up correctly, return false
7+
if (len1 + len2 != len3) {
8+
return false;
9+
}
10+
11+
// Create a 2D DP array to store the results of subproblems
12+
bool dp[len1 + 1][len2 + 1];
13+
memset(dp, false, sizeof(dp));
14+
15+
// Base case: empty strings can always interleave to form an empty string
16+
dp[0][0] = true;
17+
18+
// Fill in the DP array
19+
for (int i = 0; i <= len1; i++) {
20+
for (int j = 0; j <= len2; j++) {
21+
// If s1 matches the interleaved portion of s3
22+
if (i > 0 && s1[i - 1] == s3[i + j - 1]) {
23+
dp[i][j] = dp[i][j] || dp[i - 1][j];
24+
}
25+
// If s2 matches the interleaved portion of s3
26+
if (j > 0 && s2[j - 1] == s3[i + j - 1]) {
27+
dp[i][j] = dp[i][j] || dp[i][j - 1];
28+
}
29+
}
30+
}
31+
32+
return dp[len1][len2];
33+
}

Diff for: c/0416-partition-equal-subset-sum.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bool canPartition(int* nums, int numsSize) {
2+
int totalSum = 0;
3+
for (int i = 0; i < numsSize; i++) {
4+
totalSum += nums[i];
5+
}
6+
7+
if (totalSum % 2 != 0) {
8+
return false; // If the total sum is odd, we cannot partition equally
9+
}
10+
11+
int targetSum = totalSum / 2;
12+
bool dp[targetSum + 1]; // dp[i] represents whether a subset with sum i is possible
13+
14+
// Initialize dp array
15+
for (int i = 0; i <= targetSum; i++) {
16+
dp[i] = false;
17+
}
18+
dp[0] = true; // Empty subset can always achieve sum 0
19+
20+
// Dynamic programming approach
21+
for (int i = 0; i < numsSize; i++) {
22+
for (int j = targetSum; j >= nums[i]; j--) {
23+
dp[j] = dp[j] || dp[j - nums[i]];
24+
}
25+
}
26+
27+
return dp[targetSum];
28+
}

Diff for: c/0494-target-sum.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
int findTargetSumWays(int* nums, int numsSize, int target) {
2+
int sum = 0;
3+
for (int i = 0; i < numsSize; i++) {
4+
sum += nums[i];
5+
}
6+
if (target > sum || target < -sum) {
7+
return 0;
8+
}
9+
10+
int n = numsSize;
11+
int dp[n + 1][2 * sum + 1];
12+
for (int i = 0; i <= n; i++) {
13+
for (int j = 0; j <= 2 * sum; j++) {
14+
dp[i][j] = 0;
15+
}
16+
}
17+
dp[0][sum] = 1;
18+
19+
for (int i = 0; i < n; i++) {
20+
for (int j = nums[i]; j <= 2 * sum - nums[i]; j++) {
21+
if (dp[i][j]) {
22+
dp[i + 1][j + nums[i]] += dp[i][j];
23+
dp[i + 1][j - nums[i]] += dp[i][j];
24+
}
25+
}
26+
}
27+
28+
return dp[n][sum + target];
29+
}

Diff for: c/1143-longest-common-subsequence.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
int longestCommonSubsequence(char * text1, char * text2) {
2+
int m = strlen(text1);
3+
int n = strlen(text2);
4+
5+
int dp[m + 1][n + 1]; // dp[i][j] represents the length of the LCS of text1[0...i-1] and text2[0...j-1]
6+
7+
// Initialize the first row and column to 0
8+
for (int i = 0; i <= m; i++) {
9+
dp[i][0] = 0;
10+
}
11+
for (int j = 0; j <= n; j++) {
12+
dp[0][j] = 0;
13+
}
14+
15+
// Dynamic programming approach to fill the dp array
16+
for (int i = 1; i <= m; i++) {
17+
for (int j = 1; j <= n; j++) {
18+
if (text1[i - 1] == text2[j - 1]) {
19+
dp[i][j] = dp[i - 1][j - 1] + 1;
20+
} else {
21+
dp[i][j] = (dp[i - 1][j] > dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
22+
}
23+
}
24+
}
25+
26+
return dp[m][n];
27+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
fun minimizeMax(nums: IntArray, p: Int): Int {
3+
if (p == 0) return 0
4+
5+
val n = nums.size
6+
nums.sort()
7+
8+
fun good(x: Int): Boolean {
9+
var i = 0
10+
var count = 0
11+
while (i < n - 1) {
12+
if ((nums[i + 1] - nums[i]) <= x) {
13+
count++
14+
i += 2
15+
} else {
16+
i++
17+
}
18+
if (count == p) return true
19+
}
20+
return false
21+
}
22+
23+
var l = 0
24+
var r = 1000000000
25+
while (l < r) {
26+
val m = l + (r - l) / 2
27+
if (good(m)) {
28+
r = m
29+
} else {
30+
l = m + 1
31+
}
32+
}
33+
34+
return l
35+
}
36+
}

0 commit comments

Comments
 (0)