From 5751e99ecc0417b300378111052413f3033adc0d Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 22 Feb 2026 14:12:40 -0800 Subject: [PATCH] Add implementation for leetcode problems 80, 88, 240 --- leetcode_240.py | 62 +++++++++++++++++++++++++++++++++++++++++++ leetcode_80.py | 58 ++++++++++++++++++++++++++++++++++++++++ leetcode_88.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 leetcode_240.py create mode 100644 leetcode_80.py create mode 100644 leetcode_88.py diff --git a/leetcode_240.py b/leetcode_240.py new file mode 100644 index 00000000..4a66f805 --- /dev/null +++ b/leetcode_240.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 22 14:11:49 2026 + +@author: rishigoswamy + + LeetCode 240: Search a 2D Matrix II + Link: https://leetcode.com/problems/search-a-2d-matrix-ii/ + + Approach: + Start from bottom-left (or top-right). + + Matrix properties: + - Each row is sorted left → right. + - Each column is sorted top → bottom. + + Strategy: + Start at bottom-left: + row = m - 1 + col = 0 + + If current value < target: + move right (col++) + If current value > target: + move up (row--) + If equal: + return True + + Why this works: + From bottom-left: + - Moving right increases values. + - Moving up decreases values. + So we eliminate one row or one column every step. + + // Time Complexity : O(m + n) + At most m upward moves and n rightward moves. + // Space Complexity : O(1) + Constant extra space. +""" + +from typing import List + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + rowLimit = len(matrix) + columnLimit = len(matrix[0]) + row = len(matrix)-1 + col = 0 + if target > matrix[-1][-1]: + return False + while row >= 0 and col < len(matrix[0]): + if matrix[row][col] == target: + return True + if matrix[row][col] < target: + col+=1 + else: + row-=1 + + + + diff --git a/leetcode_80.py b/leetcode_80.py new file mode 100644 index 00000000..04c22ce8 --- /dev/null +++ b/leetcode_80.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 22 14:07:39 2026 + +@author: rishigoswamy + + LeetCode 80: Remove Duplicates from Sorted Array II + Link: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ + + Approach: + Two Pointers + Count Tracking. + + Since the array is sorted, duplicates are consecutive. + + Maintain: + slow → position to write next valid element + fast → scans the array + count → number of times current value has appeared + + Allow each number to appear at most k = 2 times. + + If nums[fast] == nums[fast - 1]: + increment count + write only if count <= k + Else: + reset count to 1 and write the new number. + + // Time Complexity : O(n) + Each element is processed once. + // Space Complexity : O(1) + In-place modification. +""" + +from typing import List + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + k = 2 + count = 1 + slow = 1 + fast = 1 + while fast < len(nums): + if nums[fast] == nums[fast-1]: + count +=1 + if count <= k: + nums[slow] = nums[fast] + slow += 1 + + else: + nums[slow] = nums[fast] + count = 1 + slow+=1 + fast+=1 + return slow + + + diff --git a/leetcode_88.py b/leetcode_88.py new file mode 100644 index 00000000..d24bfffa --- /dev/null +++ b/leetcode_88.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 22 14:10:28 2026 + +@author: rishigoswamy + + LeetCode 88: Merge Sorted Array + Link: https://leetcode.com/problems/merge-sorted-array/ + + Approach: + Three pointers starting from the end. + + nums1 has length m + n, where the last n slots are empty (0 placeholders). + To merge in-place without overwriting useful values in nums1, we fill nums1 + from the back. + + Pointers: + ptr1 -> last valid element in nums1 (index m-1) + ptr2 -> last element in nums2 (index n-1) + idx -> last position in nums1 (index m+n-1) + + At each step: + Place the larger of nums1[ptr1] and nums2[ptr2] into nums1[idx], + then move pointers accordingly. + + After the main loop, if nums2 still has remaining elements, copy them. + + // Time Complexity : O(m + n) + Each pointer only moves left across its array once. + // Space Complexity : O(1) + In-place merge. +""" + +from typing import List + +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + ptr1 = m-1 + ptr2 = n-1 + idx = len(nums1)-1 + if m == 0 and n > 0: + while ptr2 >= 0: + nums1[idx] = nums2[ptr2] + ptr2-=1 + idx-=1 + while (ptr1 >= 0 and ptr2 >= 0): + if nums1[ptr1] > nums2[ptr2]: + nums1[idx] = nums1[ptr1] + ptr1-=1 + idx-=1 + else: + nums1[idx] = nums2[ptr2] + ptr2-=1 + idx-=1 + + if ptr1 < 0 and ptr2>=0: + while ptr2 >= 0: + nums1[idx] = nums2[ptr2] + ptr2-=1 + idx-=1 + + + + + +