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
18 changes: 18 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Problem1 {

// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
public int[] twoSum(int[] nums, int target) {
Map<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[] {};
}
}
17 changes: 17 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Problem2 {

// Time Complexity : O(n * W_capacity)
// Space Complexity : O(W_capacity)
// Did this code successfully run on Leetcode : yes
public int knapsack(int W, int val[], int wt[]) {
int n = val.length;
int[] dp = new int[W + 1];
for (int i = 0; i < n; i++) {
for (int j = W; j >= wt[i]; j--) {
dp[j] = Math.max(dp[j], val[i] + dp[j - wt[i]]);
}
}

return dp[W];
}
}