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
32 changes: 32 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// Use two pointers to scan the array while keeping track of how many times the current number has appeared consecutively.
// If a number appears more than twice, skip it; otherwise, copy it into the correct position.
// Continue this process through the array and return the length of the filtered portion with at most two duplicates.

public class Solution {
public int removeDuplicates(int[] nums) {
int i =0;
int j =0;
int count = 1;
while (j < nums.length){
if (j !=0 && nums[j] == nums[j-1]){
count++;
}else{
count = 1;
}

if (count<=2){
nums[i] = nums[j];
i++;
}
j++;
}
return i;
}
}
25 changes: 25 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# Time Complexity :O(n)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Three line explanation of solution in plain english

# Your code here along with comments explaining your approach
# Use two pointers to scan the array while keeping track of how many times the current number has appeared consecutively.
# If a number appears more than twice, skip it; otherwise, copy it into the correct position.
# Continue this process through the array and return the length of the filtered portion with at most two duplicates.

class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow = 0
fast = 0
while fast < len(nums):
if fast != 0 and nums[fast] == nums[fast-1]:
count += 1
else:
count = 1
if count <= 2:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
35 changes: 35 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// Time Complexity : O(m+n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
//Start from the ends of both arrays and compare the largest remaining elements.
// Place the larger value at the end of nums1 and move the corresponding pointer backward.
// After one array is exhausted, copy any leftover elements from nums2 into nums1 since they are already sorted.
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1; // pointer for nums1
int j = n - 1; // pointer for nums2
int k = m + n - 1; // pointer for placement in nums1

while (i >= 0 && j >= 0) {
if (nums2[j] > nums1[i]) {
nums1[k] = nums2[j];
j--;
} else {
nums1[k] = nums1[i];
i--;
}
k--;
}

// Copy remaining elements from nums2
while (j >= 0) {
nums1[k] = nums2[j];
k--;
j--;
}
}
}
30 changes: 30 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Time Complexity : O(m+n)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Three line explanation of solution in plain english

# Your code here along with comments explaining your approach
#Start from the ends of both arrays and compare the largest remaining elements.
# Place the larger value at the end of nums1 and move the corresponding pointer backward.
# After one array is exhausted, copy any leftover elements from nums2 into nums1 since they are already sorted.

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.
"""
i = m-1
j = n-1
k = m+n-1
while i >=0 and j >=0:
if nums2[j]>nums1[i]:
nums1[k] =nums2[j]
j -= 1
else:
nums1[k] = nums1[i]
i -= 1
k -= 1
while j >=0:
nums1[k] = nums2[j]
k -= 1
j -= 1
28 changes: 28 additions & 0 deletions Problem-3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

// Time Complexity :O(mn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// Start from the top-right corner and compare the target with the current element.
// If the target is smaller, move left; if larger, move down.
// This eliminates one row or one column at each step, ensuring efficient search.
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int i =0;
int j = n-1;
while (j >=0 && i <m){
if (target == matrix[i][j]){
return true;
}else if(target < matrix[i][j]){
j--;
}else{
i++;
}
}
return false;
}
}
21 changes: 21 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time Complexity :O(mn)
# Space Complexity :O(1)
# Did this code successfully run on Leetcode :yes
# Three line explanation of solution in plain english

# Your code here along with comments explaining your approach
# Start from the top-right corner and compare the target with the current element.
# If the target is smaller, move left; if larger, move down.
# This eliminates one row or one column at each step, ensuring efficient search.
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
i = 0
j = len(matrix[0])-1
while i <len(matrix) and j>=0:
if target == matrix[i][j]:
return True
elif target < matrix[i][j]:
j -= 1
else:
i += 1
return False