
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Shortest Path Visiting All Nodes in C++
- Minimum Time to Collect All Apples in a Tree in C++
- 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
- Program to find minimum cost to connect all points in Python
- Minimum Time to Build Blocks in C++
- Program to find minimum time to complete all tasks in python
- Program to find minimum time to finish all jobs in Python
- Find minimum time to finish all jobs with given constraints in Python
- Find minimum cost to buy all books in C++
- Find minimum speed to finish all Jobs in C++
- Pick points from array such that minimum distance is maximized in C++
- Time Needed to Inform All Employees in C++
- Minimum operation to make all elements equal in array in C++

Advertisements