Skip to content
Open
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
Empty file removed Problem1.cpp
Empty file.
Empty file removed Problem1.java
Empty file.
Empty file removed Problem2.cpp
Empty file.
Empty file removed Problem2.java
Empty file.
121 changes: 121 additions & 0 deletions src/Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import java.util.Arrays;

/*
0/1 KNAPSACK (DP-10)

Problem:
Given arrays values[] and weights[] for n items and capacity W, return the maximum total value
such that total weight <= W. Each item can be taken at most once (0/1 property).

Approaches included in this file:
1) Pure Recursion (exponential) – for understanding
2) Top-Down DP (Memoization) – cache (index, capacity)
3) Bottom-Up DP (2D Tabulation) – classic dp table
4) Bottom-Up DP (1D Optimized) – space optimized, iterate capacity backwards

Time Complexity:
- Recursion: exponential
- Top-Down: O(n * W)
- Bottom-Up 2D: O(n * W)
- Bottom-Up 1D: O(n * W)

Space Complexity:
- Top-Down: O(n * W) + recursion stack O(n)
- Bottom-Up 2D: O(n * W)
- Bottom-Up 1D: O(W)
*/

public class Problem1 {
private int[][] memo;

/* 1) PURE RECURSION (slow, for understanding)*/
public int maxValueRecursive(int[] values, int[] weights, int W) {
if (values == null || weights == null || values.length != weights.length) return -1;
return rec(values, weights, 0, W);
}

private int rec(int[] values, int[] weights, int index, int cap) {
if (index == values.length || cap == 0) return 0;

// skip
int skip = rec(values, weights, index + 1, cap);

// take (if fits)
int take = 0;
if (weights[index] <= cap) {
take = values[index] + rec(values, weights, index + 1, cap - weights[index]);
}

return Math.max(skip, take);
}

/* 2) TOP-DOWN DP (Memoization)
memo[index][cap] = max value from index..end with capacity cap
*/
public int maxValueTopDown(int[] values, int[] weights, int W) {
if (values == null || weights == null || values.length != weights.length) return -1;

int n = values.length;
memo = new int[n][W + 1];
for (int i = 0; i < n; i++) Arrays.fill(memo[i], -1);

return topDown(values, weights, 0, W);
}

private int topDown(int[] values, int[] weights, int index, int cap) {
if (index == values.length || cap == 0) return 0;
if (memo[index][cap] != -1) return memo[index][cap];

int skip = topDown(values, weights, index + 1, cap);

int take = 0;
if (weights[index] <= cap) {
take = values[index] + topDown(values, weights, index + 1, cap - weights[index]);
}

memo[index][cap] = Math.max(skip, take);
return memo[index][cap];
}

/* 3) BOTTOM-UP DP (2D Tabulation)
dp[i][w] = max value using first i items with capacity w */
public int maxValueBottomUp2D(int[] values, int[] weights, int W) {
if (values == null || weights == null || values.length != weights.length) return -1;

int n = values.length;
int[][] dp = new int[n + 1][W + 1];

// dp[0][w] = 0 (no items)
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= W; w++) {
// skip item i-1
dp[i][w] = dp[i - 1][w];

// take item i-1 if it fits
if (weights[i - 1] <= w) {
dp[i][w] = Math.max(dp[i][w],
values[i - 1] + dp[i - 1][w - weights[i - 1]]);
}
}
}

return dp[n][W];
}

/* 4) BOTTOM-UP DP (1D Optimized)
dp[w] = max value with capacity w (processed items so far)*/
public int maxValueBottomUp1D(int[] values, int[] weights, int W) {
if (values == null || weights == null || values.length != weights.length) return -1;

int n = values.length;
int[] dp = new int[W + 1];

for (int i = 0; i < n; i++) {
for (int w = W; w >= weights[i]; w--) {
dp[w] = Math.max(dp[w], values[i] + dp[w - weights[i]]);
}
}

return dp[W];
}
}
29 changes: 29 additions & 0 deletions src/Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Problem - Two Sum (Leet Code 1)
Approach - •
•Loop through the array and for each number, compute the difference from the target.
•If the complement is already in the map, return its index and the current one.
•Otherwise, store the number with its index for future checks.
Time Complexity O(n)
Space Complexity O(n)
*/

import java.util.HashMap;

class Problem2 {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];

if (map.containsKey(complement)) {
return new int[]{ map.get(complement), i };
}

map.put(nums[i], i);
}

return new int[]{-1, -1}; // guaranteed solution exists, so this won't run
}
}
33 changes: 33 additions & 0 deletions src/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Arrays;

public class Test {
public static void main(String[] args) {

// 0/1 Knapsack Tests
Problem1 ks = new Problem1();

int[] values = {60, 100, 120};
int[] weights = {10, 20, 30};
int W = 50;
int expectedKnapsack = 220;

System.out.println(ks.maxValueRecursive(values, weights, W) == expectedKnapsack);
System.out.println(ks.maxValueTopDown(values, weights, W) == expectedKnapsack);
System.out.println(ks.maxValueBottomUp2D(values, weights, W) == expectedKnapsack);
System.out.println(ks.maxValueBottomUp1D(values, weights, W) == expectedKnapsack);

//Two Sum Tests
Problem2 ts = new Problem2();

int[] nums1 = {2, 7, 11, 15};
int target1 = 9;
int[] res1 = ts.twoSum(nums1, target1);

int[] nums2 = {3, 2, 4};
int target2 = 6;
int[] res2 = ts.twoSum(nums2, target2);

System.out.println(Arrays.equals(res1, new int[]{0, 1}));
System.out.println(Arrays.equals(res2, new int[]{1, 2}));
}
}