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
25 changes: 25 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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[] {};
}
}
19 changes: 19 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -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


24 changes: 24 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
22 changes: 22 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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]