1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

go: add «2058. Find the Minimum and Maximum Number of Nodes Between Critical Points»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-05 13:20:26 +02:00
parent a837889497
commit d34c3e5b3d
Signed by: mfocko
SSH key fingerprint: SHA256:5YXD7WbPuK60gxnG6DjAwJiS9+swoWj33/HFu8g8JVo

View file

@ -0,0 +1,36 @@
package main
func nodesBetweenCriticalPoints(head *ListNode) []int {
OUT_OF_BOUNDS_DISTANCE := 1000000
isCritical := func(x, y, z int) bool {
return (y < x && y < z) || (y > x && y > z)
}
minDistance := OUT_OF_BOUNDS_DISTANCE
previous, node := head, head.Next
previousIdx, firstIdx := 0, 0
index := 1
for node.Next != nil {
if isCritical(previous.Val, node.Val, node.Next.Val) {
if previousIdx == 0 {
firstIdx = index
} else {
minDistance = min(minDistance, index-previousIdx)
}
previousIdx = index
}
index++
previous, node = node, node.Next
}
if minDistance != OUT_OF_BOUNDS_DISTANCE {
return []int{minDistance, previousIdx - firstIdx}
}
return []int{-1, -1}
}