feat(work): add Python solutions for Two Sum, 3Sum, Two Sum II and remove unused JavaScript stubs

This commit is contained in:
Prad Nukala
2026-08-24 15:32:33 -04:00
parent fe7556bf1e
commit af03ecb9a4
7 changed files with 222 additions and 271 deletions
-55
View File
@@ -1,55 +0,0 @@
/*
* 15. 3Sum
* Difficulty: Medium
* https://leetcode.com/problems/3sum/
*
* ──────────────────────────────────────────────────
*
* Given an integer array nums, return all the triplets [nums[i],
* nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] +
* nums[j] + nums[k] == 0.
*
* Notice that the solution set must not contain duplicate triplets.
*
*
*
* Example 1:
*
* Input: nums = [-1,0,1,2,-1,-4]
* Output: [[-1,-1,2],[-1,0,1]]
* Explanation:
* nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
* nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
* nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
* The distinct triplets are [-1,0,1] and [-1,-1,2].
* Notice that the order of the output and the order of the triplets
* does not matter.
*
* Example 2:
*
* Input: nums = [0,1,1]
* Output: []
* Explanation: The only possible triplet does not sum up to 0.
*
* Example 3:
*
* Input: nums = [0,0,0]
* Output: [[0,0,0]]
* Explanation: The only possible triplet sums up to 0.
*
*
*
* Constraints:
*
* • 3 <= nums.length <= 3000
*
* • -10^5 <= nums[i] <= 10^5
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
};
+89
View File
@@ -0,0 +1,89 @@
"""
15. 3Sum
Difficulty: Medium
https://leetcode.com/problems/3sum/
──────────────────────────────────────────────────
Given an integer array nums, return all the triplets [nums[i],
nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] +
nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets
does not matter.
Example 2:
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
Constraints:
• 3 <= nums.length <= 3000
• -10^5 <= nums[i] <= 10^5
"""
class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
nums.sort()
result = []
n = len(nums)
for i in range(n):
# skip all zero
if i > 0 and nums[i] == nums[i - 1]:
continue
# two pointers
left = i + 1
right = n - 1
target = -nums[i]
while left < right:
current = nums[left] + nums[right]
if current == target:
result.append([nums[i], nums[left], nums[right]])
# skip duplicates
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
# shift pointers
left += 1
right -= 1
# since sorted, if current < target, then move left
elif current < target:
left += 1
# since sorted, if current > target, then move right
else:
right -= 1
return result
@@ -1,80 +0,0 @@
/*
* 167. Two Sum II - Input Array Is Sorted
* Difficulty: Medium
* https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
*
* ──────────────────────────────────────────────────
*
* Given a 1-indexed array of integers numbers that is already sorted in
* non-decreasing order, find two numbers such that they add up to a
* specific target number. Let these two numbers be numbers[index1] and
* numbers[index2] where 1 <= index1 < index2 <= numbers.length.
*
* Return the indices of the two numbers index1 and index2, each
* incremented by one, as an integer array [index1, index2] of length 2.
*
* The tests are generated such that there is exactly one solution. You
* may not use the same element twice.
*
* Your solution must use only constant extra space.
*
*
*
* Example 1:
*
* Input: numbers = [2,7,11,15], target = 9
* Output: [1,2]
* Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 =
* 2. We return [1, 2].
*
* Example 2:
*
* Input: numbers = [2,3,4], target = 6
* Output: [1,3]
* Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 =
* 3. We return [1, 3].
*
* Example 3:
*
* Input: numbers = [-1,0], target = -1
* Output: [1,2]
* Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2
* = 2. We return [1, 2].
*
*
*
* Constraints:
*
* • 2 <= numbers.length <= 3 * 10^4
*
* • -1000 <= numbers[i] <= 1000
*
* • numbers is sorted in non-decreasing order.
*
* • -1000 <= target <= 1000
*
* • The tests are generated such that there is exactly one solution.
*/
/**
* @param {number[]} numbers
* @param {number} target
* @return {number[]}
*/
var twoSum = function(numbers, target) {
let i = 0, j = numbers.length - 1;
while (i < j) {
const curr = numbers[i] + numbers[j];
if (curr === target){
return [i + 1, j + 1];
}else{
if (curr < target){
i++;
}else{
j--;
}
}
}
return [];
};
@@ -0,0 +1,73 @@
"""
167. Two Sum II - Input Array Is Sorted
Difficulty: Medium
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
──────────────────────────────────────────────────
Given a 1-indexed array of integers numbers that is already sorted in
non-decreasing order, find two numbers such that they add up to a
specific target number. Let these two numbers be numbers[index1] and
numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers index1 and index2, each
incremented by one, as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You
may not use the same element twice.
Your solution must use only constant extra space.
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 =
2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 =
3. We return [1, 3].
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2
= 2. We return [1, 2].
Constraints:
• 2 <= numbers.length <= 3 * 10^4
• -1000 <= numbers[i] <= 1000
• numbers is sorted in non-decreasing order.
• -1000 <= target <= 1000
• The tests are generated such that there is exactly one solution.
"""
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i = 0
j = len(numbers) - 1
while i < j:
c = numbers[i] + numbers[j]
if c == target:
return [i + 1, j + 1]
elif c < target:
i+=1
else:
j-=1
return []
-57
View File
@@ -1,57 +0,0 @@
/*
* 189. Rotate Array
* Difficulty: Medium
* https://leetcode.com/problems/rotate-array/
*
* ──────────────────────────────────────────────────
*
* Given an integer array nums, rotate the array to the right by k
* steps, where k is non-negative.
*
*
*
* Example 1:
*
* Input: nums = [1,2,3,4,5,6,7], k = 3
* Output: [5,6,7,1,2,3,4]
* Explanation:
* rotate 1 steps to the right: [7,1,2,3,4,5,6]
* rotate 2 steps to the right: [6,7,1,2,3,4,5]
* rotate 3 steps to the right: [5,6,7,1,2,3,4]
*
* Example 2:
*
* Input: nums = [-1,-100,3,99], k = 2
* Output: [3,99,-1,-100]
* Explanation:
* rotate 1 steps to the right: [99,-1,-100,3]
* rotate 2 steps to the right: [3,99,-1,-100]
*
*
*
* Constraints:
*
* • 1 <= nums.length <= 10^5
*
* • -2^31 <= nums[i] <= 2^31 - 1
*
* • 0 <= k <= 10^5
*
*
*
* Follow up:
*
* • Try to come up with as many solutions as you can. There are at
* least three different ways to solve this problem.
*
* • Could you do it in-place with O(1) extra space?
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
};
@@ -1,79 +0,0 @@
/*
* 2090. K Radius Subarray Averages
* Difficulty: Medium
* https://leetcode.com/problems/k-radius-subarray-averages/
*
* ──────────────────────────────────────────────────
*
* You are given a 0-indexed array nums of n integers, and an integer k.
*
* The k-radius average for a subarray of nums centered at some index i
* with the radius k is the average of all elements in nums between the
* indices i - k and i + k (inclusive). If there are less than k elements
* before or after the index i, then the k-radius average is -1.
*
* Build and return an array avgs of length n where avgs[i] is the
* k-radius average for the subarray centered at index i.
*
* The average of x elements is the sum of the x elements divided by x,
* using integer division. The integer division truncates toward zero,
* which means losing its fractional part.
*
* • For example, the average of four elements 2, 3, 1, and 5 is (2 + 3
* + 1 + 5) / 4 = 11 / 4 = 2.75, which truncates to 2.
*
*
*
* Example 1:
*
* Input: nums = [7,4,3,9,1,8,5,2,6], k = 3
* Output: [-1,-1,-1,5,4,4,-1,-1,-1]
* Explanation:
* - avg[0], avg[1], and avg[2] are -1 because there are less than k
* elements before each index.
* - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4
* + 3 + 9 + 1 + 8 + 5 = 37.
* Using integer division, avg[3] = 37 / 7 = 5.
* - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 +
* 5 + 2) / 7 = 4.
* - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 +
* 2 + 6) / 7 = 4.
* - avg[6], avg[7], and avg[8] are -1 because there are less than k
* elements after each index.
*
* Example 2:
*
* Input: nums = [100000], k = 0
* Output: [100000]
* Explanation:
* - The sum of the subarray centered at index 0 with radius 0 is:
* 100000.
* avg[0] = 100000 / 1 = 100000.
*
* Example 3:
*
* Input: nums = [8], k = 100000
* Output: [-1]
* Explanation:
* - avg[0] is -1 because there are less than k elements before and
* after index 0.
*
*
*
* Constraints:
*
* • n == nums.length
*
* • 1 <= n <= 10^5
*
* • 0 <= nums[i], k <= 10^5
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var getAverages = function(nums, k) {
};