diff --git a/Problem1.java b/Problem1.java index e69de29b..1bd70288 100644 --- a/Problem1.java +++ b/Problem1.java @@ -0,0 +1,34 @@ +import java.util.HashMap; +import java.util.Map; +// Time Complexity : O(n) where n is the number of elements in the array +// Space Complexity : O(n) where n is the number of elements in the array +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this : none + + +// Your code here along with comments explaining your approach +// If we go by brute force approach, we can check for all the combinations of numbers to get the target, +// but that will be O(n^2) time complexity. +// So we can use hashmap or hashset +// Here we used hashmap to store the numbers and their indices, +// and check for the complement of the current number in the hashmap, +// if it exists, we return the indices of the current number and the complement. + +class Solution { + public int[] twoSum(int[] nums, int target) { + Map hashnum = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int num = target - nums[i]; + + if (hashnum.containsKey(num)) { + return new int[] { hashnum.get(num), i }; + } else { + if (!hashnum.containsKey(nums[i])) { + hashnum.put(nums[i], i); + } + } + } + return null; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java index e69de29b..44bb6741 100644 --- a/Problem2.java +++ b/Problem2.java @@ -0,0 +1,43 @@ +// Time Complexity : O(n*W) where n is the number of items and W is the capacity of the knapsack +// Space Complexity : O(n*W) where n is the number of items and W is the capacity of the knapsack +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this : A bit of confusion in buildin the dp table, but got it after some time. + + +// Your code here along with comments explaining your approach +// Greedy or selective approach will not work here, +// We need to go exhaustive and check for all the combinations of items to get the maximum value. +// We can use a 2D dp table to store the items and the capacity of the knapsack, and fill the table in a bottom up manner. +// We fill with the value of the item if we pick it, and the value of the previous item if we don't pick it, +// Add the value of the item to the value of the remaining capacity if we pick it, and take the maximum of both. +class Knapsack { + + static int knapsack(int W, int[] val, int[] wt) { + int n = wt.length; + int[][] dp = new int[n + 1][W + 1]; + + // Build table dp[][] in bottom-up manner + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= W; j++) { + + // If there is no item or the knapsack's capacity is 0 + if (i == 0 || j == 0) + dp[i][j] = 0; + else { + int pick = 0; + + // Pick ith item if it does not exceed the capacity of knapsack + if (wt[i - 1] <= j) + pick = val[i - 1] + dp[i - 1][j - wt[i - 1]]; + + // Don't pick the ith item + int notPick = dp[i - 1][j]; + + dp[i][j] = Math.max(pick, notPick); + } + } + } + return dp[n][W]; + } +} +