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
41 changes: 41 additions & 0 deletions 3Sum.kt
Original file line number Diff line number Diff line change
@@ -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<List<Int>> {
val n = nums.size
val result : MutableList<MutableList<Int>> = 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<Int>()
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
}
}
23 changes: 23 additions & 0 deletions Container.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions SortedColors.kt
Original file line number Diff line number Diff line change
@@ -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
}
}