Sergio Perez looked to have sealed a P2 for Red Bull before Leclerc overtook him on the final lap.