algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

commit f8ff0d66dd47a6168b8a846f27be71da852b6733
parent f6f567002b8b31de257de3eaeda8fa9a7e3c524e
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 20 Aug 2026 18:38:15 -0500

More problems

Diffstat:
Acontains-duplicate-ii/contains-duplicate.py | 19+++++++++++++++++++
Adistribute-elements-into-two-arrays-i/distribute-elements-into-two-arrays-i.py | 17+++++++++++++++++
Alongest-substring-without-repeating-characters/longest-substring-recursive.py | 17+++++++++++++++++
Aminimum-size-subarray-sum/minimum-size-subarray-sum-v2.py | 34++++++++++++++++++++++++++++++++++
Aminimum-size-subarray-sum/minimum-size-subarray-sum.py | 29+++++++++++++++++++++++++++++
Anearest-available-drone/nearest-available-drone.py | 17+++++++++++++++++
Anew-21-game/new-21-game-v2.py | 22++++++++++++++++++++++
Anew-21-game/new-21-game-v3.py | 32++++++++++++++++++++++++++++++++
Anew-21-game/new-21-game.py | 31+++++++++++++++++++++++++++++++
9 files changed, 218 insertions(+), 0 deletions(-)

diff --git a/contains-duplicate-ii/contains-duplicate.py b/contains-duplicate-ii/contains-duplicate.py @@ -0,0 +1,19 @@ +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + + num_set = set() + left = 0 + + for right in range(len(nums)): + + if right - left > k: + num_set.remove(nums[left]) + left += 1 + + if nums[right] in num_set: + return True + + num_set.add(nums[right]) + + + return False diff --git a/distribute-elements-into-two-arrays-i/distribute-elements-into-two-arrays-i.py b/distribute-elements-into-two-arrays-i/distribute-elements-into-two-arrays-i.py @@ -0,0 +1,17 @@ +class Solution: + def resultArray(self, nums: List[int]) -> List[int]: + arr1 = [] + arr2 = [] + + arr1.append(nums[0]) + arr2.append(nums[1]) + + for i in range(2, len(nums)): + if arr1[-1] > arr2[-1]: + arr1.append(nums[i]) + else: + arr2.append(nums[i]) + + arr1.extend(arr2) + + return arr1 diff --git a/longest-substring-without-repeating-characters/longest-substring-recursive.py b/longest-substring-without-repeating-characters/longest-substring-recursive.py @@ -0,0 +1,17 @@ +def recurse(s, left, right, char_set, best): + if right == len(s): + return best + if s[right] in char_set: + char_set.remove(s[left]) + return recurse(s, left + 1, right, char_set, best) + if (right - left) + 1 > best: + char_set.add(s[right]) + return recurse(s, left, right + 1, char_set, best + 1) + else: + char_set.add(s[right]) + return recurse(s, left, right + 1, char_set, best) + +class Solution: + + def lengthOfLongestSubstring(self, s: str) -> int: + return recurse(s, 0, 0, set(), 0) diff --git a/minimum-size-subarray-sum/minimum-size-subarray-sum-v2.py b/minimum-size-subarray-sum/minimum-size-subarray-sum-v2.py @@ -0,0 +1,34 @@ +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + + left = 0 + right = 0 + current_sum = nums[0] + shortest = 0 + + def move_left(): + nonlocal current_sum, left + current_sum -= nums[left] + left += 1 + + def move_right(): + nonlocal current_sum, right + right += 1 + if right < len(nums): + current_sum += nums[right] + + while right < len(nums): + if current_sum >= target: + current_len = right - left + 1 + if current_len < shortest or shortest == 0: + shortest = current_len + if left < right: + move_left() + else: + move_right() + else: + move_left() + else: + move_right() + + return shortest diff --git a/minimum-size-subarray-sum/minimum-size-subarray-sum.py b/minimum-size-subarray-sum/minimum-size-subarray-sum.py @@ -0,0 +1,29 @@ +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + + left = 0 + right = 0 + + current_sum = nums[0] + best_length = 0 + + while right < len(nums): + if current_sum >= target: + current = right - left + if best_length == 0 or best_length > current + 1: + best_length = current + 1 + if left < right: + current_sum -= nums[left] + left += 1 + + else: + right += 1 + if right < len(nums): + current_sum += nums[right] + else: + right += 1 + if right < len(nums): + current_sum += nums[right] + + + return best_length diff --git a/nearest-available-drone/nearest-available-drone.py b/nearest-available-drone/nearest-available-drone.py @@ -0,0 +1,17 @@ +def drone_distance(drone, target): + return abs(drone[0] - target[0]) + abs(drone[1] - target[1]) + +class solution: + def nearestdrone(self, drones: list[list[int]], target: list[int]) -> int: + # <= manhattan + min_index = -1 + min_distance = -1 + + for idx in range(len(drones)): + drone = drones[idx] + distance = drone_distance(drone, target) + if distance <= drone[2] and (min_index == -1 or min_distance > distance): + min_index = idx + min_distance = distance + + return min_index diff --git a/new-21-game/new-21-game-v2.py b/new-21-game/new-21-game-v2.py @@ -0,0 +1,22 @@ +class Solution: + + def new21Game(self, n: int, k: int, maxPts: int) -> float: + + # while less than k she draws + # stops when she has k or more points + # return probability that alice has n or fewer points + + probabilities = [1.00] + + jump_prob = 1 / maxPts + + for i in range(0, k): + current_prob = probabilities[i] + for x in range(1,maxPts + 1): + if len(probabilities) < i + x + 1: + probabilities.append(0) + probabilities[i+x] += jump_prob * current_prob + + print(probabilities) + + return 1 - sum(probabilities[n + 1:]) diff --git a/new-21-game/new-21-game-v3.py b/new-21-game/new-21-game-v3.py @@ -0,0 +1,32 @@ +class Solution: + + def new21Game(self, n: int, k: int, maxPts: int) -> float: + + # while less than k she draws + # stops when she has k or more points + # return probability that alice has n or fewer points + + if k == 0: + return 1 + + left = 0 + right = 1 + csum = 1.0000 + jump_prob = 1/maxPts + + probs = [1.000000] + + while right < maxPts + k: + + probs.append(csum * jump_prob) + if len(probs) - 1 < k: + csum += probs[len(probs) - 1] + + if right - left + 1 > maxPts: + csum -= probs[left] + left += 1 + + right += 1 + + print(probs) + return 1 - sum(probs[n+1:]) diff --git a/new-21-game/new-21-game.py b/new-21-game/new-21-game.py @@ -0,0 +1,31 @@ +import random + +def simulate(n,k,maxPts): + count = 0 + while count < k: + count += random.randrange(1,maxPts + 1) + if count > n: + return False + return True + +class Solution: + def new21Game(self, n: int, k: int, maxPts: int) -> float: + + # while less than k she draws + # stops when she has k or more points + # return probability that alice has n or fewer points + + avg = 0.00 + count = 0 + + for _ in range(200000): + eval = 0 + + if simulate(n,k,maxPts): + eval = 1 + + avg = avg + ((eval - avg) / (count + 1)) + + count += 1 + + return avg