From aef338b6a8b268c8f400c772f607744f473e9468 Mon Sep 17 00:00:00 2001 From: prenastro Date: Mon, 6 Jul 2026 16:30:06 -0700 Subject: [PATCH] Completed S30 Completitive-Coding-2 --- 01knapsack.py | 16 ++++++++++++++++ twosum.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 01knapsack.py create mode 100644 twosum.py diff --git a/01knapsack.py b/01knapsack.py new file mode 100644 index 00000000..bd19034a --- /dev/null +++ b/01knapsack.py @@ -0,0 +1,16 @@ +class Solution: + + @staticmethod + def findMax(weights, profit, totalCapacity): + n = totalCapacity + m = len(weights) + dp = [0] * (n + 1) + + for i in range(m): + for j in range(n, weights[i] - 1, -1): + dp[j] = max(dp[j], profit[i] + dp[j - weights[i]]) + + return dp[n] + + # TC - O(m*n) + # SC - O(n) diff --git a/twosum.py b/twosum.py new file mode 100644 index 00000000..0bb66354 --- /dev/null +++ b/twosum.py @@ -0,0 +1,14 @@ +class Solution: + def twoSum(self, nums, target): + map = {} + + for i in range(len(nums)): + cmp = target - nums[i] + if cmp in map: + return [map[cmp], i] + map[nums[i]] = i + + return [] + +# TC - O(n) +# SC - O(n) \ No newline at end of file