- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Minimum Time Visiting All Points in C++

Suppose there are some points given as an array. We have to find the minimum time in seconds to visit all points. There are some conditions.

- In one second, it can move vertically, horizontally and diagonally
- We have to visit the points in the same order as they appear in the array.

So if the points are [(1, 1), (3, 4), (-1, 0)], then output will be 7. If we check the sequence for the shortest route, the sequence will be (1, 1), (2, 2), (3, 3), (3, 4), (2, 3), (1, 2), (0, 1), (-1, 0)

To solve this we will just find the maximum of x coordinate difference of two consecutive points, and y coordinate difference of two consecutive points. The max values will be added together.

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: int minTimeToVisitAllPoints(vector<vector<int>>& p) { int ans = 0; int n = p.size(); for(int i = 1; i < n; i++){ ans += max(abs(p[i][0] - p[i-1][0]), abs(p[i][1] - p[i-1] [1])); } return ans; } }; main(){ Solution ob; vector<vector<int>> c = {{1,1},{3,4},{-1,0}}; cout << ob.minTimeToVisitAllPoints(c); }

## Input

[[1,1],[3,4],[-1,0]]

## Output

7

- Related Questions & Answers
- Shortest Path Visiting All Nodes in C++
- Program to find minimum cost to connect all points in Python
- Minimum Time to Collect All Apples in a Tree in C++
- Program to find minimum time to complete all tasks in python
- Program to find minimum time to finish all jobs in Python
- Minimum Initial Points to Reach Destination
- Find minimum time to finish all jobs with given constraints in Python
- Find minimum time to finish all jobs with given constraints in C++
- Minimum Time Difference in C++
- C++ code to find minimum time needed to do all tasks
- Minimum Time to Build Blocks in C++
- Finding minimum time difference in an array in JavaScript
- Pick points from array such that minimum distance is maximized in C++
- Top Ten Novels of All Time
- Minimum number of points to be removed to get remaining points on one side of axis using C++.

Advertisements