diff --git a/3Sum.kt b/3Sum.kt new file mode 100644 index 00000000..1c87912e --- /dev/null +++ b/3Sum.kt @@ -0,0 +1,41 @@ +// This solution is based on keeping 3 pointers, one for each number in the triplet. We first sort the array. We keep the first pointer fixed and use the other two pointers to pass through the array to find the sum which equals to zero. +// If we have found the triplet, we add it to the list and then increment the middle and right pointers. We also skip the duplicate elements to avoid duplicate triplets in the result. +// Time complexity: O(n^2), Space complexity: O(1) + + +class Solution { + fun threeSum(nums: IntArray): List> { + val n = nums.size + val result : MutableList> = mutableListOf() + Arrays.sort(nums) + + for(l in 0..n-1) { + if (nums[l] > 0) break + if (l > 0 && nums[l] == nums[l - 1]) continue + var m = l + 1 + var r = n - 1 + + while (m < r) { + val sum = nums[l] + nums[m] + nums[r] + if (sum == 0) { + // add to the list + val li = mutableListOf() + li.add(nums[l]) + li.add(nums[m]) + li.add(nums[r]) + result.add(li) + m++ + r-- + + while(m < r && nums[m] == nums[m-1]) m++ + while(m < r && nums[r] == nums[r+1]) r-- + } else if (sum < 0) { + m++ + } else { + r-- + } + } + } + return result + } +} \ No newline at end of file diff --git a/Container.kt b/Container.kt new file mode 100644 index 00000000..fb8887ab --- /dev/null +++ b/Container.kt @@ -0,0 +1,23 @@ +// In this solution, the idea is to keep two pointers and measure the area between them. We start with the left pointer at the beginning of the array and the right pointer at the end of the array. +//We calculate the area formed between the two pointers and keep track of the maximum area found so far. +// Time complexity: O(n), Space complexity: O(1) +class Solution { + fun maxArea(height: IntArray): Int { + var leftPtr = 0 + var rightPtr = height.lastIndex + var resultArea = 0 + while (leftPtr < rightPtr) { + val currentArea = minOf(height[leftPtr], height[rightPtr]) * (rightPtr - leftPtr) + + if (currentArea > resultArea) { + resultArea = currentArea + } + if (height[leftPtr] < height[rightPtr]) { + leftPtr++ + } else { + rightPtr-- + } + } + return resultArea + } +} \ No newline at end of file diff --git a/SortedColors.kt b/SortedColors.kt new file mode 100644 index 00000000..d7efd888 --- /dev/null +++ b/SortedColors.kt @@ -0,0 +1,31 @@ +// For this problem, we have three pointers, left, mid, right. The left, mid pointer start at the begining of the array and the right pointer starts at the end of the array. +// we iterate through the array with mid pointer. If the mid pointer points to 0, we swap the values at left and mid pointer and increment both pointers. +// If the mid pointer points to 2, we swap the values at mid and right pointer and decrement the right pointer. If the mid pointer points to 1, we just increment the mid pointer. +// Time complexity: O(n), Space complexity: O(1) + +class Solution { + fun sortColors(nums: IntArray): Unit { + var l = 0 + var m = 0 + var r = nums.size -1 + + while(m <= r) { + if (nums[m] == 2) { + swap(m, r, nums) + r-- + } else if (nums[m] == 0) { + swap(m, l, nums) + m++ + l++ + } else { + m++ + } + } + } + + fun swap(element1: Int, element2: Int, nums: IntArray) { + val temp = nums[element1] + nums[element1] = nums[element2] + nums[element2] = temp + } +} \ No newline at end of file