Skip to content

Commit 44c30df

Browse files
committed
Fixed PR notes
Signed-off-by: Slava Lysunkin <lysunkin@gmail.com>
1 parent 6312b1a commit 44c30df

6 files changed

+83
-117
lines changed
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
"strings"
6-
"unicode"
7-
)
8-
91
func isAlnum(r rune) bool {
102
return unicode.IsLetter(r) || unicode.IsDigit(r)
113
}
124

135
func isPalindromeValid(input string) bool {
14-
s := []rune(strings.ToLower(input))
6+
s := []rune(input)
7+
158
left, right := 0, len(s)-1
169

1710
for left < right {
18-
for !isAlnum(s[left]) && left < right {
11+
// Skip non-alphanumeric characters from the left.
12+
for left < right && !isAlnum(s[left]) {
1913
left++
2014
}
21-
for !isAlnum(s[right]) && left < right {
15+
// Skip non-alphanumeric characters from the right.
16+
for left < right && !isAlnum(s[right]) {
2217
right--
2318
}
24-
19+
// If the characters at the left and right pointers don't
20+
// match, the string is not a palindrome.
2521
if s[left] != s[right] {
2622
return false
2723
}
@@ -32,8 +28,3 @@ func isPalindromeValid(input string) bool {
3228

3329
return true
3430
}
35-
36-
func main() {
37-
fmt.Println(isPalindromeValid("A dog! A panic in a pagoda.")) // -> true
38-
fmt.Println(isPalindromeValid("abc123")) // -> false
39-
}

go/Two Pointers/largest_container.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
)
6-
71
func largestContainer(heights []int) int {
2+
maxWater := 0
83
left, right := 0, len(heights)-1
9-
maxVol := 0
104

115
for left < right {
12-
width := right - left
13-
height := heights[left]
14-
if heights[right] < height {
15-
height = heights[right]
16-
}
17-
currentVol := width * height
18-
if currentVol > maxVol {
19-
maxVol = currentVol
20-
}
21-
6+
// Calculate the water contained between the current pair of
7+
// lines.
8+
water := min(heights[left], heights[right]) * (right - left)
9+
maxWater = max(maxWater, water)
10+
// Move the pointers inward, always moving the pointer at the
11+
// shorter line. If both lines have the same height, move both
12+
// pointers inward.
2213
if heights[left] < heights[right] {
2314
left++
15+
} else if heights[left] > heights[right] {
16+
right--
2417
} else {
18+
left++
2519
right--
2620
}
2721
}
2822

29-
return maxVol
30-
}
31-
32-
func main() {
33-
fmt.Println(largestContainer([]int{2, 7, 8, 3, 7, 6})) // -> 24
23+
return maxWater
3424
}
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
)
6-
71
func reverse(s []rune, start int) {
82
end := len(s) - 1
93
for start < end {
@@ -15,31 +9,27 @@ func reverse(s []rune, start int) {
159

1610
func nextLexicographicalSequence(s string) string {
1711
letters := []rune(s)
18-
n := len(letters)
19-
pivot := n - 2
20-
12+
// Locate the pivot, which is the first character from the right that breaks
13+
// non-increasing order. Start searching from the second-to-last position.
14+
pivot := len(letters) - 2
2115
for pivot >= 0 && letters[pivot] >= letters[pivot+1] {
2216
pivot--
2317
}
24-
18+
// If pivot is not found, the string is already in its largest permutation. In
19+
// this case, reverse the string to obtain the smallest permutation.
2520
if pivot == -1 {
2621
reverse(letters, 0)
2722
return string(letters)
2823
}
29-
30-
successor := n - 1
31-
for letters[successor] <= letters[pivot] {
32-
successor--
24+
// Find the rightmost successor to the pivot.
25+
rightmostSuccessor := len(letters) - 1
26+
for letters[rightmostSuccessor] <= letters[pivot] {
27+
rightmostSuccessor--
3328
}
34-
35-
letters[pivot], letters[successor] = letters[successor], letters[pivot]
36-
29+
// Swap the rightmost successor with the pivot to increase the lexicographical
30+
// order of the suffix.
31+
letters[pivot], letters[rightmostSuccessor] = letters[rightmostSuccessor], letters[pivot]
32+
// Reverse the suffix after the pivot to minimize its permutation.
3733
reverse(letters, pivot+1)
38-
3934
return string(letters)
4035
}
41-
42-
func main() {
43-
fmt.Println(nextLexicographicalSequence("abcd")) // -> abdc
44-
fmt.Println(nextLexicographicalSequence("dcba")) // -> abcd
45-
}

go/Two Pointers/pair_sum_sorted.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
package main
2-
3-
import "fmt"
4-
51
func pairSumSorted(nums []int, target int) []int {
62
left, right := 0, len(nums)-1
73
for left < right {
84
sum := nums[left] + nums[right]
9-
if sum == target {
10-
return []int{left, right}
11-
}
5+
// If the sum is smaller, increment the left pointer, aiming
6+
// to increase the sum toward the target value.
127
if sum < target {
138
left++
14-
} else {
9+
} else if sum > target {
10+
// If the sum is larger, decrement the right pointer, aiming
11+
// to decrease the sum toward the target value.
1512
right--
13+
} else {
14+
// If the target pair is found, return its indexes.
15+
return []int{left, right}
1616
}
1717
}
1818
return []int{}
1919
}
20-
21-
func main() {
22-
fmt.Println(pairSumSorted([]int{-5, -2, 3, 4, 6}, 7)) // -> [2 3]
23-
fmt.Println(pairSumSorted([]int{1, 1, 1}, 2)) // -> [0 2]
24-
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
package main
2-
3-
import "fmt"
4-
51
func shiftZerosToTheEnd(nums []int) {
2+
// The 'left' pointer is used to position non-zero elements.
63
left := 0
4+
// Iterate through the array using a 'right' pointer to locate non-zero
5+
// elements.
76
for right := 0; right < len(nums); right++ {
87
if nums[right] != 0 {
98
nums[left], nums[right] = nums[right], nums[left]
9+
// Increment 'left' since it now points to a position already occupied
10+
// by a non-zero element.
1011
left++
1112
}
1213
}
1314
}
14-
15-
func main() {
16-
nums := []int{0, 1, 0, 3, 2}
17-
shiftZerosToTheEnd(nums)
18-
fmt.Println(nums) // -> [1 3 2 0 0]
19-
}

go/Two Pointers/triplet_sum.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
"sort"
6-
)
7-
81
func tripletSum(nums []int) [][]int {
9-
result := [][]int{}
10-
2+
triplets := [][]int{}
113
sort.Ints(nums)
12-
n := len(nums)
134

14-
for i := 0; i < n-2; i++ {
5+
for i := 0; i < len(nums); i++ {
6+
// Optimization: triplets consisting of only positive numbers
7+
// will never sum to 0.
8+
9+
if nums[i] > 0 {
10+
break
11+
}
12+
13+
// To avoid duplicate triplets, skip 'a' if it's the same as
14+
// the previous number.
1515
if i > 0 && nums[i] == nums[i-1] {
1616
continue
1717
}
1818

19-
left, right := i+1, n-1
20-
for left < right {
21-
sum := nums[i] + nums[left] + nums[right]
22-
if sum == 0 {
23-
result = append(result, []int{nums[i], nums[left], nums[right]})
24-
left++
25-
right--
26-
for left < right && nums[left] == nums[left-1] {
27-
left++
28-
}
29-
for left < right && nums[right] == nums[right+1] {
30-
right--
31-
}
32-
} else if sum < 0 {
33-
left++
34-
} else {
35-
right--
36-
}
19+
// Find all pairs that sum to a target of '-a' (-nums[i]).
20+
pairs := pairSumSortedAllPairs(nums, i+1, -nums[i])
21+
for _, pair := range pairs {
22+
triplets = append(triplets, []int{nums[i], pair[0], pair[1]})
3723
}
3824
}
3925

40-
return result
26+
return triplets
4127
}
4228

43-
func main() {
44-
fmt.Println(tripletSum([]int{0, -1, 2, -3, 1})) // -> [[-3 1 2] [-1 0 1]]
29+
func pairSumSortedAllPairs(nums []int, start int, target int) [][]int {
30+
pairs := [][]int{}
31+
left, right := start, len(nums)-1
32+
for left < right {
33+
sum := nums[left] + nums[right]
34+
if sum == target {
35+
pairs = append(pairs, []int{nums[left], nums[right]})
36+
left++
37+
// To avoid duplicate '[b, c]' pairs, skip 'b' if it's the
38+
// same as the previous number.
39+
for left < right && nums[left] == nums[left-1] {
40+
left++
41+
}
42+
} else if sum < target {
43+
left++
44+
} else {
45+
right--
46+
}
47+
}
48+
49+
return pairs
4550
}

0 commit comments

Comments
 (0)