Ctrl K

No Inward Corners

Medium
A plot of land is fenced in. The fence posts stand at corners, where corners[i] = [x, y], and the fence runs from post to post in order, then from the last post back to the first. The fence is guaranteed never to cross or touch itself: exactly two neighbouring sections meet at each post, and sections share no other points. Return True if the resulting polygon is convex, meaning none of its corners is pushed inward, and False otherwise. A post in the middle of a straight stretch of fence (three consecutive posts on one line) does not break convexity.
Example 1
corners=[[0, 0], [4, 0], [6, 3], [2, 5], [-1, 2]] → True

Walking along the outline we always turn the same way (left), so there are no dents.

Example 2
corners=[[0, 0], [6, 0], [6, 6], [3, 2], [0, 6]] → False

At [3, 2] the outline turns the other way: that corner points inward.

Example 3
corners=[[0, 0], [2, 0], [4, 0], [4, 3], [0, 3]] → True

The corner [2, 0] lies right on a straight side; that is not a dent, so the shape is convex.

Constraints
  • 3 ≤ len(corners) ≤ 10⁴
  • corners[i].length == 2
  • -10⁴ ≤ x, y ≤ 10⁴
  • All points are distinct
  • The polygon is simple: its sides never cross or overlap
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmallest Paddock MediumSmallest Rectangle of Pins MediumNearest Points to the Base MediumDevices in Router Range MediumLongest Firework Chain Medium
loading Python…
Input
corners=[[0, 0], [4, 0], [6, 3], [2, 5], [-1, 2]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.