File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 226422642496|[ Maximum Value of a String in an Array] ( ./solutions/2496-maximum-value-of-a-string-in-an-array.js ) |Easy|
226522652498|[ Frog Jump II] ( ./solutions/2498-frog-jump-ii.js ) |Medium|
226622662499|[ Minimum Total Cost to Make Arrays Unequal] ( ./solutions/2499-minimum-total-cost-to-make-arrays-unequal.js ) |Hard|
2267+ 2500|[ Delete Greatest Value in Each Row] ( ./solutions/2500-delete-greatest-value-in-each-row.js ) |Easy|
226722682501|[ Longest Square Streak in an Array] ( ./solutions/2501-longest-square-streak-in-an-array.js ) |Medium|
226822692502|[ Design Memory Allocator] ( ./solutions/2502-design-memory-allocator.js ) |Medium|
226922702503|[ Maximum Number of Points From Grid Queries] ( ./solutions/2503-maximum-number-of-points-from-grid-queries.js ) |Hard|
Original file line number Diff line number Diff line change 1+ /**
2+ * 2500. Delete Greatest Value in Each Row
3+ * https://leetcode.com/problems/delete-greatest-value-in-each-row/
4+ * Difficulty: Easy
5+ *
6+ * You are given an m x n matrix grid consisting of positive integers.
7+ *
8+ * Perform the following operation until grid becomes empty:
9+ * - Delete the element with the greatest value from each row. If multiple such elements exist,
10+ * delete any of them.
11+ * - Add the maximum of deleted elements to the answer.
12+ *
13+ * Note that the number of columns decreases by one after each operation.
14+ *
15+ * Return the answer after performing the operations described above.
16+ */
17+
18+ /**
19+ * @param {number[][] } grid
20+ * @return {number }
21+ */
22+ var deleteGreatestValue = function ( grid ) {
23+ for ( const row of grid ) {
24+ row . sort ( ( a , b ) => a - b ) ;
25+ }
26+
27+ let result = 0 ;
28+ const cols = grid [ 0 ] . length ;
29+ for ( let col = cols - 1 ; col >= 0 ; col -- ) {
30+ let maxInColumn = 0 ;
31+ for ( let row = 0 ; row < grid . length ; row ++ ) {
32+ maxInColumn = Math . max ( maxInColumn , grid [ row ] [ col ] ) ;
33+ }
34+ result += maxInColumn ;
35+ }
36+
37+ return result ;
38+ } ;
You can’t perform that action at this time.
0 commit comments