File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 174
174
1486|[ XOR Operation in an Array] ( ./1486-xor-operation-in-an-array.js ) |Easy|
175
175
1491|[ Average Salary Excluding the Minimum and Maximum Salary] ( ./1491-average-salary-excluding-the-minimum-and-maximum-salary.js ) |Easy|
176
176
1492|[ The kth Factor of n] ( ./1492-the-kth-factor-of-n.js ) |Medium|
177
+ 1493|[ Longest Subarray of 1's After Deleting One Element] ( ./1493-longest-subarray-of-1s-after-deleting-one-element.js ) |Medium|
177
178
1496|[ Path Crossing] ( ./1496-path-crossing.js ) |Easy|
178
179
1502|[ Can Make Arithmetic Progression From Sequence] ( ./1502-can-make-arithmetic-progression-from-sequence.js ) |Easy|
179
180
1598|[ Crawler Log Folder] ( ./1598-crawler-log-folder.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1493. Longest Subarray of 1's After Deleting One Element
3
+ * https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a binary array nums, you should delete one element from it.
7
+ *
8
+ * Return the size of the longest non-empty subarray containing only 1's in the
9
+ * resulting array. Return 0 if there is no such subarray.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums
14
+ * @return {number }
15
+ */
16
+ var longestSubarray = function ( nums ) {
17
+ const grouped = nums . join ( '' ) . split ( '0' ) . map ( s => s . length ) ;
18
+ let max = 0 ;
19
+
20
+ for ( let i = 0 ; i < grouped . length ; i ++ ) {
21
+ max = Math . max (
22
+ max ,
23
+ ( grouped [ i - 1 ] ?? 0 ) + grouped [ i ] ,
24
+ grouped [ i ] + ( grouped [ i + 1 ] ?? 0 )
25
+ ) ;
26
+ }
27
+
28
+ return max - ( nums . includes ( 0 ) ? 0 : 1 ) ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments