Leetcode Note: Go - Rectangle Overlap

Rectangle Overlap - LeetCode
https://leetcode.com/problems/rectangle-overlap/

  • Go 言語で取り組んだメモ

所感

  • int[] で rec1, rec2 が渡されるので 2つ のデータが Rectangle (長方形) として重なり合っているかを bool で return する

回答

Rectangle Overlap - LeetCode
https://leetcode.com/problems/rectangle-overlap/solution/

Golang solution faster than 100% with explanation and images - LeetCode Discuss
https://leetcode.com/problems/rectangle-overlap/discuss/1103101/Golang-solution-faster-than-100-with-explanation-and-images

func isRectangleOverlap(rec1 []int, rec2 []int) bool {

    left, right := max(rec1[0], rec2[0]), min(rec1[2], rec2[2])
    up, down := min(rec1[3], rec2[3]), max(rec1[1], rec2[1])

    if right > left && up > down {
        return true
    }

    return false

}

func max(a, b int) int {
    if a < b {
        return b
    }
    return a
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}