Go/Leet Code

Arrays101 Valid Mountain Array in GO

Gopythor 2022. 3. 4. 02:03
728x90
반응형

Accepted Solutions Runtime Distribution

Given an array of integers arr, return true if and only if it is a valid mountain array.

Recall that arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 104

My code

func validMountainArray(arr []int) bool {
    if len(arr) <= 2 {
        return false
    }
    l := 0
    r := len(arr) - 1

    for i := 0; i < len(arr)-1; i++ {
        if arr[i] < arr[i+1] {
            l++
        } else if arr[i] == arr[i+1] {
            return false
        } else {
            break
        }
    }
    for j := len(arr) - 1; j > 0; j-- {
        if arr[j] < arr[j-1] {
            r--
        } else if arr[j] == arr[j-1] {
            return false
        } else {
            break
        }
    }

        if l == r && r != len(arr)-1 && r != 0 {
        return true
    }

    return false
}
  • I tried to use switch which checks max is updated or not.
  • After that I added conditions, but there are many cases to solve.
  • So I tried to use two pointers(I don't know this also could be called like that)
  • First, Left starts from 0 to max value. If arr[i] == arr[i+1], this will be false.
  • Also If value does not increase, return max value.
  • Right also works same way.
  • First, It starts from end of array.
  • If values are same with j == j-1 array, it will be false.
  • Also If values does not increased, return max value.
  • when left, and right process ended, If condition will check it will be same or not.
    https://go.dev/play/p/0RUKlE8m8Bc

Sample 12 ms submission

func validMountainArray(arr []int) bool {
    l := len(arr)
    if l < 3 {
        return false
    }
    var top int = l
    for i := 1; i < l; i++ {
        if arr[i] > arr[i-1] {
            continue
        } else if arr[i] == arr[i-1] {
            return false
        } else {
            top = i-1
            break
        }
    }
    for i := top+1; i < l; i++ {
        if arr[i] < arr[i-1] {
            continue
        } else {
            return false
        }
    }
        return top > 0 && top < l-1
}
  • This is simple code
  • First if conditional Statement checks array has more than 3 arrays.
  • For mountain array, at least they must have up, peak, down values

Ascending power(0, 1, 2)

  • When there is a ascending power(0, 1, 2) array, first for loop didn't return anything including top value change(top == 2).
  • Second for loop will not work because i value is bigger than condition (top == 2, i = top +1 == 3, 3 < 3(false)).
  • last return condition checks top is bigger than 0 and less than array length.
  • But top has length value so it returns false(no down value).
  • Last return does not need to be top < l-1

Decending power(2, 1, 0)

  • When there is a Decending power(2, 1, 0) array, first for loop return top = i-1(0) because peak is the first one.
  • Second for loop continued and finished.
  • last return condition checks but top is 0 so result will be false.
  • There is no up value.

sample 16 ms submission

 func validMountainArray(A []int) bool {
    if len(A) < 3 {
        return false
    }

    i := 0
    for i < len(A)-2 {
        if A[i] == A[i+1] {
            return false
        } else if A[i] > A[i+1] {
            break
        }
        i++
    }

    if i == 0 || (i == len(A)-2 && A[i] < A[i+1]) {
        return false
    }

    for i < len(A)-1 {
        if A[i] == A[i+1] || A[i] < A[i+1] {
            return false
        }
        i++
    }

    return true
}
  • This is also simple code.
  • First if conditional Statement checks array has more than 3 arrays.
  • For mountain array, at least they must have up, peak, down values.
  • First for loop checks up values.

    Ascending power(0, 1, 2)

  • When there is a ascending power(0, 1, 2) array, first for loop checks i, i+1 array, but there is no breaks.
  • I will have 1(len(a-2).
  • If condition checks whether i == len(A)-2 && A[i]< A[i+1]. If so, It doesn't have down value.
  • Return false.

Decending power(2, 1, 0)

  • When there is a Decending power(2, 1, 0) array, first for loop checks i, i+1 array. Else if will make break.
  • Bacause i value is not smaller than i+1.
  • So it goes to If condition. but i value is 0 so it returns false.

Second for loop

  • This used after ivalue got peak and going until end array.
  • So after peak, value is same or bigger than expected peak, than return false.

Sample 6500 KB submission

 func validMountainArray(arr []int) bool {

    if len(arr) < 3 {
        return false
    }

    up := 0
    down := 0

    goingUp := true

    for i := 1; i < len(arr); i++ {

        if arr[i-1] == arr[i] {
            return false
        }

        if goingUp {

            if arr[i-1] > arr[i] {
                goingUp = false
                down++
            } else {
                up++
            }

        } else {
            if arr[i-1] < arr[i] {
                return false
            } else {
                down++
            }


        }

    }

    if (up > 0 && down > 0) {
        return true
    }
    return false
}
  • This Code concept is what i thought at first.
  • Use up switch when array values are ascending power.
  • When starts down value based on last value, switch for
  • Final if condition should have up and down values which more than 0.
  • If not, return false.

Ascending power(0, 1, 2)

  • In frist for loop, It will increase until end of array. Because going up is true.
  • Last If condition checks if up and down are not 0.
  • But down is 0 so it returns false.

Decending power(2, 1, 0)

  • In frist for loop, goingUP will be changed to false and increasing down until end of array.
  • Last If condition checks if up and down are not 0.
  • But up is 0 so it returns false.

sample 6600 KB submission

func validMountainArray(a []int) bool {
    if len(a) < 3 {
        return false
    }
    var mountainStart,slopeStart bool
    for i:= 0 ; i < len(a ) - 1 ;i++ {
        if a[i] == a[i+1]{
            return false
        }
        if a[i] < a[i+1] {
            mountainStart=true
            if slopeStart {
                return false
            }
            continue
        }else if a[i] > a[i+1] {
            if !mountainStart {
                return false
            }
            slopeStart = true
            continue
        }
    }
    if mountainStart && slopeStart {
        return true
    }
    return false
}
  • This also works as switch.
  • Check every if conditions mountainStart and slopeStart
  • Final if condition checks every start has true.

Ascending power(0, 1, 2)

  • From first For loop, only i increases with mountainStart true.
  • So later it will return false

Decending power(2, 1, 0)

  • From first For loop, only i increases with slopeStart true.
  • So later it will return false
728x90
반응형