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
Empty file removed Problem1.java
Empty file.
21 changes: 21 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def knapsack(W, val, wt):

# Initializing dp list
dp = [0] * (W + 1)

# Taking first i elements
for i in range(1, len(wt) + 1):

# Starting from back, so that we also have data of
# previous computation of i-1 items
for j in range(W, wt[i - 1] - 1, -1):
dp[j] = max(dp[j], dp[j - wt[i - 1]] + val[i - 1])

return dp[W]

if __name__ == "__main__":
val = [1, 2, 3]
wt = [4, 5, 1]
W = 4

print(knapsack(W, val, wt))
Empty file removed Problem2.java
Empty file.
19 changes: 19 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# https://leetcode.com/problems/two-sum/description/

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