diff --git a/Problem1.java b/Problem1.java index e69de29b..1af3cc1c 100644 --- a/Problem1.java +++ b/Problem1.java @@ -0,0 +1,25 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// created array to track the previously visited elements with index +// then checked if the subtraction of target and nums[i] in the prev tracked elements +// if there returned that index with the current index +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap prev_visited_elements = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int complement = target - nums[i]; + if (prev_visited_elements.containsKey(complement)) { + return new int[] {prev_visited_elements.get(complement), i}; + } + prev_visited_elements.put(nums[i], i); + } + + return new int[] {}; + } +} diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..7cc4ae06 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,19 @@ +# Time Complexity : O(n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode :yes +# Any problem you faced while coding this :no + + +# Your code here along with comments explaining your approach +# created array to track the previously visited elements with index +# then checked if the subtraction of target and nums[i] in the prev tracked elements +# if there returned that index with the current index +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + prev_visited_elements = {} + for i in range (len(nums)): + if target-nums[i] in prev_visited_elements: + return [prev_visited_elements[target - nums[i]], i] + prev_visited_elements[nums[i]]=i + + \ No newline at end of file diff --git a/Problem2.java b/Problem2.java index e69de29b..e9e6093e 100644 --- a/Problem2.java +++ b/Problem2.java @@ -0,0 +1,24 @@ +// Time Complexity : O(n*totalCapacity) +// Space Complexity : O(totalCapacity) +// Did this code successfully run on Leetcode :no +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// loop through the weights and total capacity +// looping through dp in reverse because the initial elements are changed +// for each element the max is dp[j-1] and profit of that element + dp[j-weight[i]] as we have to move weight[i] steps backward +class Solution { + + public static int findMax(int[] weights, int[] profit, int totalCapacity) { + int n = totalCapacity; + int m = weights.length; + int[] dp = new int[n+1]; + for (int i = 0; i < m; i++){ + for (int j = n; j>= weights[i]; j--){ + dp[j] = Math.max(dp[j], profit[i]+dp[j-weights[i]]); + } + } + return dp[n]; + } +} \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..c598cda6 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,22 @@ +# Time Complexity : O(n*totalCapacity) +# Space Complexity : O(totalCapacity) +# Did this code successfully run on Leetcode :no +# Any problem you faced while coding this :no + + +# Your code here along with comments explaining your approach +# loop through the weights and total capacity +# looping through dp in reverse because the initial elements are changed +# for each element the max is dp[j-1] and profit of that element + dp[j-weight[i]] as we have to move weight[i] steps backward + +class Solution: + @staticmethod + def findMax(weights, profit, totalCapacity): + m = len(weights) + dp = [0]*(totalCapacity+1) + if m ==0: + return 0 + for i in range(m): + for j in range(totalCapacity,weights[i]-1,-1): + dp[j] = max(dp[j],profit[i]+dp[j-weights[i]]) + return dp[totalCapacity] \ No newline at end of file