A vector editor offers a "smooth fill" that only works on convex shapes. A shape is given by its corners vertices, in order around the outline (clockwise or counterclockwise). The outline never crosses itself.
Write isConvex(vertices) that returns true if the shape is convex, meaning every turn along the outline goes the same way, and false if it has a dent. Three corners in a straight line don't count as a turn either way.
Input: vertices = [[0,0],[4,0],[4,4],[0,4]] Output: true
A square turns left at every corner.
Input: vertices = [[0,0],[4,0],[2,1],[4,4],[0,4]] Output: false
At [2, 1] the outline turns right while every other corner turns left: that's a dent.
3 <= vertices.length <= 10^4-10^4 <= x, y <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #666 Is the Shape Convex. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.