Given two integers N and M. The task is to find the minimum number of points required to cover an N * M grid.
A point can cover two blocks in a 2-D grid when placed in any common line or sideline.
Input: N = 5, M = 7
Input: N = 3, M = 8
Approach: This problem can be solved using Greedy Approach. The main idea is to observe that a single point placed on the common line or sideline covers two blocks. So the total number of points needed to cover all the blocks(say B blocks) is B/2 when B is even else B/2 + 1 when B is odd.
For a grid having N*M blocks, The total number of blocks will be (N*M)/2 when either one of them is even. Otherwise, it will require ((N*M)/2) + 1 points to cover all the blocks and one extra for last untouched block.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum number of integers required to fill the NxM grid
- Minimum cost to cover the given positions in a N*M grid
- Minimum time required to cover a Binary Array
- Minimum queens required to cover all the squares of a chess board
- Number of squares of side length required to cover an N*M rectangle
- Steps required to visit M points in order on a circular ring of N points
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Collect maximum points in a grid using two traversals
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum number of swaps required to make a number divisible by 60
- Minimum number of given powers of 2 required to represent a number
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of changes required to make the given array an AP
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum number of primes required such that their sum is equal to N
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum number of given operation required to convert n to m
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.