[Classic Interview Questions] [Baidu] There are n points from left to right on the number axis a [0], a [1],..., A [n, number axis
Question
N points a [0], a [1],…, A [n-1], given a rope with a length of L, the rope can cover up to several points.
Train of Thought 1
Traverse all intervals and compare with the rope L.
I. traverse the start point of the interval and j. traverse the end point of the interval.
The time complexity is O (n ^ 2)
Code 1
/* ----------------------------------- * Date: 2015-02-08 * Author: SJF0115 * Subject: Rope coverage * Source: Baidu 2014 * blog: ---------------------------------- */# include <iostream> # include <vector> # include <cstring> # include <algorithm> using namespace std; class Solution {public: // points the length of the given point L rope int RopeCover (vector <int> points, int L) {int size = points. size (); if (size <= 0) {return 0 ;}// if // The maximum number of points that can be overwritten int max = 0; int start = 0, end = 0; // I start point j end point traverses all intervals; for (int I = 0; I <size-1; ++ I) {for (int j = I + 1; j <size; ++ j) {if (points [j]-points [I] <= L & j-I + 1> max) {max = j-I + 1; start = I; end = j ;} // if }}// for cout <"start Point->" <start <"end Point->" <end <endl; return max ;}}; int main () {Solution s; vector <int> points = {-,}; int L = 15; int result = s. ropeCover (points, L); // output cout <result <endl; return 0 ;}
Train of Thought 2
Two pointers: start and end.
If points [front]-points [rear] <= L, the start header moves one step forward.
If points [front]-points [rear]> L, the tail end moves one step forward.
Each number can be traversed up to two times, so the time complexity is O (n ).
A netizen gave an image metaphor for this algorithm:
It is like a snake with a length of L. If you cannot stretch your head, you only need to take one step at most to cover several points.
Code 2
/* ----------------------------------- * Date: 2015-02-08 * Author: SJF0115 * Subject: Rope coverage * Source: Baidu 2014 * blog: ---------------------------------- */# include <iostream> # include <vector> # include <cstring> # include <algorithm> using namespace std; class Solution {public: // points the length of the given point L rope int RopeCover (vector <int> points, int L) {int size = points. size (); if (size <= 0) {return 0 ;}// if // The maximum number of points that can be overwritten int max = 0; int start = 1, end = 0; int maxS = 0, maxE = 0; while (end <start) {if (points [start]-points [end] <= L) {if (start-end + 1> max) {max = start-end + 1; maxS = end; maxE = start ;} // if // move the head forward to a grid + + start;} // if else {// move the tail forward to a grid + + end ;}} // while cout <"Start Point->" <maxS <"End Point->" <maxE <endl; return max ;}; int main () {Solution s; vector <int> points = {-1, 3, 4, 9, 11, 25}; int L = 8; int result = s. ropeCover (points, L); // output cout <result <endl; return 0 ;}
If you have any questions about this method, please correct me. If you have a better method, welcome to the Guide.