JPMORGAN Coding Question – Solved

3 Live
Determine the highest value after executing n steps on an infinite 2D grid that initially contains all zeros. The grid is indexed from (1,1) at the bottom-left corner with coordinates increasing upwards and to the right. For each given coordinate pair (r, c), increment every cell in the rectangle from (1,1) to (r,c) inclusive by 1. After processing all coordinates, return the number of cells that contain the maximum value in the grid. Example: `upRight = ["1 4", "2 3", "4 1"]` Each string contains two space-separated integers representing the row (r) and column (c). The process: - Step 1: Increment cells in rectangle (1,1) to (1,4) - Step 2: Increment cells in rectangle (1,1) to (2,3) - Step 3: Increment cells in rectangle (1,1) to (4,1) The overlapping region across all three rectangles is the intersection of: - Rows: min(1, 2, 4) = 1 - Columns: min(4, 3, 1) = 1 Thus, cell (1,1) is the only cell included in all three updates. So the max value in the grid is 3, and it occurs once. Output: `1` Function Description: Complete the function `countMax` in the editor below. `countMax` has the following parameter: - `string upRight[n]`: an array of strings where each contains two space-separated integers representing the up-right corner of a rectangle. Return: - `long`: the number of occurrences of the final grid's maximum value. Constraints: - 1 ≀ n ≀ 100 - 1 ≀ number of rows, number of columns ≀ 10^6 Sample Input: `upRight = ["2 3", "3 7", "4 1"]` In this case: - Minimum of rows = min(2, 3, 4) = 2 - Minimum of columns = min(3, 7, 1) = 1 So the overlapping region is of size `2 Γ— 1 = 2` Output: `2`

Asked in: JPMORGAN

Image of the Question

Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one opera… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |