Given a n*m grid and the position of some poles to be painted in the grid, the task is to find the minimum cost to paint all the poles. There is no cost involved in moving from the one row to the other, whereas moving to an adjacent column has 1 rupee cost associated with it.
Examples:
Input: n = 2, m = 2, noOfPos = 2
pos[0] = 0, 0
pos[1] = 0, 1
Output: 1
The grid is of 2*2 size and there are two poles at {0, 0} and {0, 1}.
So we will start at {0, 0} and paint the pole and then go to
the next column to paint the pole at {0, 1} position which will
cost 1 rupee to move one column.
Input: n = 2, m = 2, noOfPos = 2
pos[0] = {0, 0}
pos[1] = {1, 0}
Output: 0
Both poles are in the same column. So, no need to move to another column.
Approach: As there is the only cost of moving in columns therefore if we go to any column we will paint all the poles in that column and then move forward. So basically the answer will be the difference between the two farthest columns.
Below is the required implementation:
C++
// C++ implementation of the above approach #include <iostream> #include <list> using namespace std; // Function to find the cost to paint all poles void find(int n,int m,int p,int q[2][2]) { // To store all the columns,create list list <int> z ; int i ; for(i = 0;i < p;i++) z.push_back(q[i][1]); // sort in ascending order z.sort(); // z.back() gives max value // z.front() gives min value cout << z.back() - z.front() <<endl ; } // Driver code int main() { int n = 2; int m = 2; int p = 2; int q[2][2] = {{0,0},{0,1}} ; find(n, m, p, q); return 0; // This code is contributed by ANKITRAI1 } |
Java
// Java implementation of the above approach import java.util.*; class solution { // Function to find the cost to paint all poles static void find(int n,int m,int p,int q[][]) { // To store all the columns,create list Vector <Integer> z= new Vector<Integer>() ; int i ; for(i = 0;i < p;i++) z.add(q[i][1]); // sort in ascending order Collections.sort(z); // z.back() gives max value // z.front() gives min value System.out.print(z.get(z.size()-1) - z.get(0) ) ; } // Driver code public static void main(String args[]) { int n = 2; int m = 2; int p = 2; int q[][] = {{0,0},{0,1}} ; find(n, m, p, q); } } //contributed by Arnab Kundu |
Python3
# Function to find the cost to paint all poles import math as ma def find(n, m, p, q): # To store all the columns z =[] for i in range(p): z.append(q[i][1]) print(max(z)-min(z)) n, m, p = 2, 2, 2q =[(0, 0), (0, 1)] find(n, m, p, q) |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to find the cost to paint all poles static void find(int n, int m, int p, int [,]q) { // To store all the columns,create list List <int> z = new List<int>(); int i; for(i = 0; i < p; i++) z.Add(q[i, 1]); // sort in ascending order z.Sort(); // z.back() gives max value // z.front() gives min value Console.Write(z[z.Count-1] - z[0]); } // Driver code public static void Main(String []args) { int n = 2; int m = 2; int p = 2; int [,]q = {{0, 0}, {0, 1}}; find(n, m, p, q); } } // This code is contributed by PrinciRaj1992 |
PHP
<?php // PHP implementation of the above approach // Function to find the cost to // paint all poles function find($n, $m, $p, $q) { // To store all the columns, // create an array $z = array(); for($i = 0; $i < $p; $i++) array_push($z, $q[$i][1]); // sort in ascending order sort($z); echo max($z) - min($z); } // Driver code $n = 2; $m = 2; $p = 2; $q = array(array(0, 0), array(0, 1)); find($n, $m, $p, $q); // This code is contributed by ita_c ?> |
1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum number of points required to cover all blocks of a 2-D grid
- Cost of painting n * m grid
- Minimum queens required to cover all the squares of a chess board
- Minimum squares to cover a rectangle
- Minimum time required to cover a Binary Array
- Count minimum number of fountains to be activated to cover the entire garden
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles
- Number of squares of side length required to cover an N*M rectangle
- Count number of ways to cover a distance | Set 2
- Python IMDbPY – Getting cover URL of the series
- How to rearrange columns of a 2D NumPy array using given index positions?
- Construct smallest N-digit number possible by placing digits at positions specified by given array
- Minimum product in a grid of adjacent elements
- Minimum number of integers required to fill the NxM grid
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Minimum sum falling path in a NxN grid
- Minimum distance to the end of a grid from source
- Minimum distance to the corner of a grid from source
- Minimum cost to partition the given binary string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

