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
57 changes: 57 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Arrays;

// Bounded Knapsack
public class Problem1 {

public static void main() {
int target = 4;
int[] weights = {4, 5, 1};
int[] profits = {1, 2, 3};

int max = knapsackTabulation(target, weights, profits); //
System.out.println(max);

max = knapsackRecursive(target, weights, profits, 0); //
System.out.println(max);
}


public static int knapsackTabulation(int target, int[] weights, int[] profits) {
// Time Complexity : O(N^2)
// Space Complexity : O(N^2)
int rows = weights.length + 1;
int cols = target + 1;

int[][] table = new int[rows][cols];
for (int i = 1; i < rows; i++) {
int currentWeight = weights[i - 1];
for (int j = 1; j < cols; j++) {
if (currentWeight <= j) {
table[i][j] = profits[i - 1] + table[i - 1][j - currentWeight];
} else {
table[i][j] = table[i - 1][j];
}
}
}

return table[rows - 1][cols - 1];
}

static int knapsackRecursive(int target, int[] weights, int[] profits, int index) {
// Time Complexity : O(2^N), where N is the number of weights, at every recursion, we need to make choice whether to include weight or exclude weight
// Space Complexity : O(N)
if (index >= weights.length) return 0;
if (target <= 0) return 0;

int choose = 0;
if (weights[index] <= target) {
// choose the item only once and move the index to next item
choose = profits[index] + knapsackRecursive(target - weights[index], weights, profits, index + 1);
}

int notChoose = knapsackRecursive(target, weights, profits, index + 1);

return Math.max(choose, notChoose);
}

}
23 changes: 23 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.HashMap;
import java.util.Map;

public class Problem2{

public int[] twoSum(int[] nums, int target) {
// Time Complexity : O(N), N is number of elements in nums
// Space Complexity : O(N)
int [] result = new int[2];
Map<Integer, Integer> pairs = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int complement = target - nums[i];
Integer index = pairs.getOrDefault(complement, null);
if( index == null){
pairs.put(nums[i], i);
}else {
result[0] = index;
result[1] = i;
}
}
return result;
}
}