Skip to content

Commit 657c25c

Browse files
committed
Remove class wrapped
1 parent eeae5ff commit 657c25c

10 files changed

+184
-207
lines changed
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
class IsPalindromeValid {
2-
3-
fun isPalindromeValid(s: String): Boolean {
4-
var left = 0
5-
var right = s.length - 1
6-
while (left < right) {
7-
// Skip non-alphanumeric characters from the left.
8-
while (left < right && !s[left].isLetterOrDigit()) {
9-
left++
10-
}
11-
// Skip non-alphanumeric characters from the right.
12-
while (left < right && !s[right].isLetterOrDigit()) {
13-
right--
14-
}
15-
// If the characters at the left and right pointers don't
16-
// match, the string is not a palindrome.
17-
if (s[left] != s[right]) {
18-
return false
19-
}
1+
fun isPalindromeValid(s: String): Boolean {
2+
var left = 0
3+
var right = s.length - 1
4+
while (left < right) {
5+
// Skip non-alphanumeric characters from the left.
6+
while (left < right && !s[left].isLetterOrDigit()) {
207
left++
8+
}
9+
// Skip non-alphanumeric characters from the right.
10+
while (left < right && !s[right].isLetterOrDigit()) {
2111
right--
2212
}
23-
return true
13+
// If the characters at the left and right pointers don't
14+
// match, the string is not a palindrome.
15+
if (s[left] != s[right]) {
16+
return false
17+
}
18+
left++
19+
right--
2420
}
21+
return true
2522
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
class LargestContainer {
1+
import kotlin.math.min
2+
import kotlin.math.max
23

3-
fun largestContainer(heights: List<Int>): Int {
4-
var maxWater = 0
5-
var left = 0
6-
var right = heights.size - 1
7-
while (left < right) {
8-
// Calculate the water contained between the current pair of
9-
// lines.
10-
val water = min(heights[left], heights[right]) * (right - left)
11-
maxWater = max(maxWater, water)
12-
// Move the pointers inward, always moving the pointer at the
13-
// shorter line. If both lines have the same height, move both
14-
// pointers inward.
15-
if (heights[left] < heights[right]) {
16-
left++
17-
} else if (heights[left] > heights[right]) {
18-
right--
19-
} else {
20-
left++
21-
right--
22-
}
4+
fun largestContainer(heights: List<Int>): Int {
5+
var maxWater = 0
6+
var left = 0
7+
var right = heights.size - 1
8+
while (left < right) {
9+
// Calculate the water contained between the current pair of
10+
// lines.
11+
val water = min(heights[left], heights[right]) * (right - left)
12+
maxWater = max(maxWater, water)
13+
// Move the pointers inward, always moving the pointer at the
14+
// shorter line. If both lines have the same height, move both
15+
// pointers inward.
16+
if (heights[left] < heights[right]) {
17+
left++
18+
} else if (heights[left] > heights[right]) {
19+
right--
20+
} else {
21+
left++
22+
right--
2323
}
24-
return maxWater
2524
}
25+
return maxWater
2626
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class LargestContainerBruteForce {
1+
import kotlin.math.max
2+
import kotlin.math.min
23

3-
fun largestContainerBruteForce(heights: List<Int>): Int {
4-
val n = heights.size
5-
var maxWater = 0
6-
// Find the maximum amount of water stored between all pairs of lines.
7-
for (i in 0 until n) {
8-
for (j in i + 1 until heights.size) {
9-
val water = min(heights[i], heights[j]) * (j - i)
10-
maxWater = max(maxWater, water)
11-
}
4+
fun largestContainerBruteForce(heights: List<Int>): Int {
5+
val n = heights.size
6+
var maxWater = 0
7+
// Find the maximum amount of water stored between all pairs of lines.
8+
for (i in 0 until n) {
9+
for (j in i + 1 until heights.size) {
10+
val water = min(heights[i], heights[j]) * (j - i)
11+
maxWater = max(maxWater, water)
1212
}
13-
return maxWater
1413
}
15-
}
14+
return maxWater
15+
}
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1-
class NextLexicographicalSequence {
1+
fun nextLexicographicalSequence(s: String): String {
2+
val letters = s.toCharArray()
23

3-
fun nextLexicographicalSequence(s: String): String {
4-
val letters = s.toCharArray()
5-
6-
// Locate the pivot, which is the first character from the right that breaks
7-
// non-increasing order. Start searching from the second-to-last position,
8-
// since the last character is neither increasing nor decreasing.
9-
var pivot = letters.size - 2
10-
while (pivot >= 0 && letters[pivot] >= letters[pivot + 1]) {
11-
pivot--
12-
}
4+
// Locate the pivot, which is the first character from the right that breaks
5+
// non-increasing order. Start searching from the second-to-last position,
6+
// since the last character is neither increasing nor decreasing.
7+
var pivot = letters.size - 2
8+
while (pivot >= 0 && letters[pivot] >= letters[pivot + 1]) {
9+
pivot--
10+
}
1311

14-
// If pivot is not found, the string is already in its largest permutation. In
15-
// this case, reverse the string to obtain the smallest permutation.
16-
if (pivot == -1) {
17-
letters.reverse()
18-
return letters.joinToString("")
19-
}
12+
// If pivot is not found, the string is already in its largest permutation. In
13+
// this case, reverse the string to obtain the smallest permutation.
14+
if (pivot == -1) {
15+
letters.reverse()
16+
return letters.joinToString("")
17+
}
2018

21-
// Find the rightmost successor to the pivot.
22-
var rightmostSuccessor = letters.size - 1
23-
while (letters[rightmostSuccessor] <= letters[pivot]) {
24-
rightmostSuccessor--
25-
}
19+
// Find the rightmost successor to the pivot.
20+
var rightmostSuccessor = letters.size - 1
21+
while (letters[rightmostSuccessor] <= letters[pivot]) {
22+
rightmostSuccessor--
23+
}
2624

27-
// Swap the rightmost successor with the pivot to increase the lexicographical
28-
// order of the suffix.
29-
letters[pivot] = letters[rightmostSuccessor].also { letters[rightmostSuccessor] = letters[pivot] }
25+
// Swap the rightmost successor with the pivot to increase the lexicographical
26+
// order of the suffix.
27+
letters[pivot] =
28+
letters[rightmostSuccessor].also { letters[rightmostSuccessor] = letters[pivot] }
3029

31-
// Reverse the suffix after the pivot to minimize its permutation.
32-
val suffix = letters.sliceArray(pivot + 1 until letters.size)
33-
suffix.reverse()
30+
// Reverse the suffix after the pivot to minimize its permutation.
31+
val suffix = letters.sliceArray(pivot + 1 until letters.size)
32+
suffix.reverse()
3433

35-
return (letters.sliceArray(0..pivot) + suffix).joinToString("")
36-
}
37-
}
34+
return (letters.sliceArray(0..pivot) + suffix).joinToString("")
35+
}

kotlin/Two Pointers/PairSumSorted.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
class PairSumSorted {
2-
3-
fun pairSumSorted(nums: IntArray, target: Int): List<Int> {
4-
var left = 0
5-
var right = nums.size - 1
6-
while (left < right) {
7-
val sum = nums[left] + nums[right]
8-
// If the sum is smaller, increment the left pointer, aiming
9-
// to increase the sum toward the target value.
10-
if (sum < target) {
11-
left++
1+
fun pairSumSorted(nums: IntArray, target: Int): List<Int> {
2+
var left = 0
3+
var right = nums.size - 1
4+
while (left < right) {
5+
val sum = nums[left] + nums[right]
6+
// If the sum is smaller, increment the left pointer, aiming
7+
// to increase the sum toward the target value.
8+
if (sum < target) {
9+
left++
1210
// If the sum is larger, decrement the right pointer, aiming
1311
// to decrease the sum toward the target value.
14-
} else if (sum > target) {
15-
right--
12+
} else if (sum > target) {
13+
right--
1614
// If the target pair is found, return its indexes.
17-
} else {
18-
return listOf(left, right)
19-
}
15+
} else {
16+
return listOf(left, right)
2017
}
21-
return emptyList()
2218
}
23-
}
19+
return emptyList()
20+
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
class PairSumSortedBruteForce {
2-
3-
fun pairSumSortedBruteForce(nums: IntArray, target: Int): List<Int> {
4-
val n = nums.size
5-
for (i in 0 until n) {
6-
for (j in i + 1 until nums.size) {
7-
if (nums[i] + nums[j] == target) {
8-
return listOf(i, j)
9-
}
1+
fun pairSumSortedBruteForce(nums: IntArray, target: Int): List<Int> {
2+
val n = nums.size
3+
for (i in 0 until n) {
4+
for (j in i + 1 until nums.size) {
5+
if (nums[i] + nums[j] == target) {
6+
return listOf(i, j)
107
}
118
}
12-
return emptyList()
139
}
14-
}
10+
return emptyList()
11+
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
class ShiftZeroToTheEnd {
2-
3-
fun shiftZerosToTheEnd(nums: IntArray) {
4-
// The 'left' pointer is used to position non-zero elements.
5-
var left = 0
6-
// Iterate through the array using a 'right' pointer to locate non-zero
7-
// elements.
8-
for (right in nums.indices) {
9-
if (nums[right] != 0) {
10-
// Swap element
11-
nums[left] = nums[right].also { nums[right] = nums[left] }
12-
// Increment 'left' since it now points to a position already occupied
13-
// by a non-zero element.
14-
left++
15-
}
1+
fun shiftZerosToTheEnd(nums: IntArray) {
2+
// The 'left' pointer is used to position non-zero elements.
3+
var left = 0
4+
// Iterate through the array using a 'right' pointer to locate non-zero
5+
// elements.
6+
for (right in nums.indices) {
7+
if (nums[right] != 0) {
8+
// Swap element
9+
nums[left] = nums[right].also { nums[right] = nums[left] }
10+
// Increment 'left' since it now points to a position already occupied
11+
// by a non-zero element.
12+
left++
1613
}
1714
}
18-
}
15+
}
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
class ShiftZeroToTheEndNaive {
1+
fun shiftZerosToTheEndNaive(nums: IntArray) {
2+
val temp = IntArray(nums.size)
3+
var i = 0
24

3-
fun shiftZerosToTheEndNaive(nums: IntArray) {
4-
val temp = IntArray(nums.size)
5-
var i = 0
6-
7-
// Add all non-zero elements to the left of 'temp'.
8-
for (num in nums) {
9-
if (num != 0) {
10-
// Mark non-zero on tmp
11-
// Otherwise, skip this num
12-
temp[i] = num
13-
i++
14-
}
5+
// Add all non-zero elements to the left of 'temp'.
6+
for (num in nums) {
7+
if (num != 0) {
8+
// Mark non-zero on tmp
9+
// Otherwise, skip this num
10+
temp[i] = num
11+
i++
1512
}
13+
}
1614

17-
// Set 'nums' to 'temp'.
18-
for (j in temp.indices) {
19-
nums[j] = temp[j]
20-
}
15+
// Set 'nums' to 'temp'.
16+
for (j in temp.indices) {
17+
nums[j] = temp[j]
2118
}
22-
}
19+
}

0 commit comments

Comments
 (0)