-
Notifications
You must be signed in to change notification settings - Fork 89
Added Golang solutions of 'Two Pointers' tasks #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
func isAlnum(r rune) bool { | ||
return unicode.IsLetter(r) || unicode.IsDigit(r) | ||
} | ||
|
||
func isPalindromeValid(input string) bool { | ||
s := []rune(input) | ||
|
||
left, right := 0, len(s)-1 | ||
|
||
for left < right { | ||
// Skip non-alphanumeric characters from the left. | ||
for left < right && !isAlnum(s[left]) { | ||
left++ | ||
} | ||
// Skip non-alphanumeric characters from the right. | ||
for left < right && !isAlnum(s[right]) { | ||
right-- | ||
} | ||
// If the characters at the left and right pointers don't | ||
// match, the string is not a palindrome. | ||
if s[left] != s[right] { | ||
return false | ||
} | ||
|
||
left++ | ||
right-- | ||
} | ||
|
||
return true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
func largestContainer(heights []int) int { | ||
maxWater := 0 | ||
left, right := 0, len(heights)-1 | ||
|
||
for left < right { | ||
// Calculate the water contained between the current pair of | ||
// lines. | ||
water := min(heights[left], heights[right]) * (right - left) | ||
maxWater = max(maxWater, water) | ||
// Move the pointers inward, always moving the pointer at the | ||
// shorter line. If both lines have the same height, move both | ||
// pointers inward. | ||
if heights[left] < heights[right] { | ||
left++ | ||
} else if heights[left] > heights[right] { | ||
right-- | ||
} else { | ||
left++ | ||
right-- | ||
} | ||
} | ||
|
||
return maxWater | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
func reverse(s []rune, start int) { | ||
end := len(s) - 1 | ||
for start < end { | ||
s[start], s[end] = s[end], s[start] | ||
start++ | ||
end-- | ||
} | ||
} | ||
|
||
func nextLexicographicalSequence(s string) string { | ||
letters := []rune(s) | ||
// Locate the pivot, which is the first character from the right that breaks | ||
// non-increasing order. Start searching from the second-to-last position. | ||
pivot := len(letters) - 2 | ||
for pivot >= 0 && letters[pivot] >= letters[pivot+1] { | ||
pivot-- | ||
} | ||
// If pivot is not found, the string is already in its largest permutation. In | ||
// this case, reverse the string to obtain the smallest permutation. | ||
if pivot == -1 { | ||
reverse(letters, 0) | ||
return string(letters) | ||
} | ||
// Find the rightmost successor to the pivot. | ||
rightmostSuccessor := len(letters) - 1 | ||
for letters[rightmostSuccessor] <= letters[pivot] { | ||
rightmostSuccessor-- | ||
} | ||
// Swap the rightmost successor with the pivot to increase the lexicographical | ||
// order of the suffix. | ||
letters[pivot], letters[rightmostSuccessor] = letters[rightmostSuccessor], letters[pivot] | ||
// Reverse the suffix after the pivot to minimize its permutation. | ||
reverse(letters, pivot+1) | ||
return string(letters) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
func pairSumSorted(nums []int, target int) []int { | ||
left, right := 0, len(nums)-1 | ||
for left < right { | ||
sum := nums[left] + nums[right] | ||
// If the sum is smaller, increment the left pointer, aiming | ||
// to increase the sum toward the target value. | ||
if sum < target { | ||
left++ | ||
} else if sum > target { | ||
// If the sum is larger, decrement the right pointer, aiming | ||
// to decrease the sum toward the target value. | ||
right-- | ||
} else { | ||
// If the target pair is found, return its indexes. | ||
return []int{left, right} | ||
} | ||
} | ||
return []int{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have decided to use |
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
func shiftZerosToTheEnd(nums []int) { | ||
// The 'left' pointer is used to position non-zero elements. | ||
left := 0 | ||
// Iterate through the array using a 'right' pointer to locate non-zero | ||
// elements. | ||
for right := 0; right < len(nums); right++ { | ||
if nums[right] != 0 { | ||
nums[left], nums[right] = nums[right], nums[left] | ||
// Increment 'left' since it now points to a position already occupied | ||
// by a non-zero element. | ||
left++ | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
func tripletSum(nums []int) [][]int { | ||
triplets := [][]int{} | ||
sort.Ints(nums) | ||
|
||
for i := 0; i < len(nums); i++ { | ||
// Optimization: triplets consisting of only positive numbers | ||
// will never sum to 0. | ||
|
||
if nums[i] > 0 { | ||
break | ||
} | ||
|
||
// To avoid duplicate triplets, skip 'a' if it's the same as | ||
// the previous number. | ||
if i > 0 && nums[i] == nums[i-1] { | ||
continue | ||
} | ||
|
||
// Find all pairs that sum to a target of '-a' (-nums[i]). | ||
pairs := pairSumSortedAllPairs(nums, i+1, -nums[i]) | ||
for _, pair := range pairs { | ||
triplets = append(triplets, []int{nums[i], pair[0], pair[1]}) | ||
} | ||
} | ||
|
||
return triplets | ||
} | ||
|
||
func pairSumSortedAllPairs(nums []int, start int, target int) [][]int { | ||
pairs := [][]int{} | ||
left, right := start, len(nums)-1 | ||
for left < right { | ||
sum := nums[left] + nums[right] | ||
if sum == target { | ||
pairs = append(pairs, []int{nums[left], nums[right]}) | ||
left++ | ||
// To avoid duplicate '[b, c]' pairs, skip 'b' if it's the | ||
// same as the previous number. | ||
for left < right && nums[left] == nums[left-1] { | ||
left++ | ||
} | ||
} else if sum < target { | ||
left++ | ||
} else { | ||
right-- | ||
} | ||
} | ||
|
||
return pairs | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this function be moved below the
isPalindromeValid
function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for
next_lexicographical_sequence.go