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
19 changes: 19 additions & 0 deletions Knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TimeComplexity: O(n x W)
// SpaceComplexity: O(W)

static int knapsack(int W, int[] val, int[] wt) {

// Initializing dp array
int[] dp = new int[W + 1];

// Taking first i elements
for (int i = 1; i <= wt.length; i++) {

// Starting from back, so that we also have data of
// previous computation of i-1 items
for (int j = W; j >= wt[i - 1]; j--) {
dp[j] = Math.max(dp[j], dp[j - wt[i - 1]] + val[i - 1]);
}
}
return dp[W];
}
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.
18 changes: 18 additions & 0 deletions TwoSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// TimeComplexity: O(n)
// SpaceComplexity: O(n)
// Explanation: I am iterating through the array and storing the elemnet in the hashmap with its index if the compliment is not found. If the compliment is found I am returning the compliment index and the current index.

class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<n; i++) {
if(map.containsKey(target-nums[i])) {
return new int[]{map.get(target-nums[i]), i};
}
map.put(nums[i], i);
}
return new int[2];

}
}