File tree Expand file tree Collapse file tree 4 files changed +105
-0
lines changed
Max Consecutive Ones III - Leetcode 1004 Expand file tree Collapse file tree 4 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int longestOnes (vector<int >& nums, int k) {
4
+ int n = nums.size ();
5
+ int maxLen = 0 ;
6
+
7
+ // Iterate over all starting points of subarrays
8
+ for (int i = 0 ; i < n; ++i) {
9
+
10
+ // Check each subarray starting from `i`
11
+ for (int j = i; j < n; ++j) {
12
+ int zeroCount = 0 ;
13
+ for (int c = i; c <= j; ++c) {
14
+ if (nums[c] == 0 )
15
+ zeroCount++;
16
+ }
17
+
18
+ // If the number of zeroes exceeds k,
19
+ // update the maximum length
20
+ if (zeroCount <= k) {
21
+ maxLen = max (maxLen, j - i + 1 );
22
+ }
23
+
24
+ }
25
+ }
26
+
27
+ return maxLen;
28
+ }
29
+ };
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int longestOnes (int [] nums , int k ) {
3
+ int maxLength = 0 ;
4
+
5
+ // Iterate over all starting points
6
+ for (int i = 0 ; i < nums .length ; i ++) {
7
+
8
+ // Iterate over all possible subarrays starting from i
9
+ for (int j = i ; j < nums .length ; j ++) {
10
+
11
+ int zeroCount = 0 ;
12
+ for (int c = i ; c <= j ; ++c ) {
13
+ if (nums [c ] == 0 )
14
+ ++zeroCount ;
15
+ }
16
+
17
+ // Update maxLength if this subarray is valid
18
+ if (zeroCount <= k ) {
19
+ maxLength = Math .max (maxLength , j - i + 1 );
20
+ }
21
+
22
+ }
23
+ }
24
+
25
+ return maxLength ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @param {number } k
4
+ * @return {number }
5
+ */
6
+ var longestOnes = function ( nums , k ) {
7
+ let maxLength = 0 ;
8
+
9
+ // Iterate over all starting points
10
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
11
+
12
+ // Iterate over all possible subarrays starting from i
13
+ for ( let j = i ; j < nums . length ; j ++ ) {
14
+ let zeroCount = 0 ;
15
+ for ( let c = i ; c <= j ; ++ c ) {
16
+ if ( nums [ c ] == 0 )
17
+ ++ zeroCount ;
18
+ }
19
+
20
+ // If the number of zeroes exceeds k,
21
+ // update the maximum length
22
+ if ( zeroCount <= k ) {
23
+ maxLength = Math . max ( maxLength , j - i + 1 ) ;
24
+ }
25
+
26
+ }
27
+ }
28
+
29
+ return maxLength ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def longestOnes (self , nums , k ):
3
+ """
4
+ :type nums: List[int]
5
+ :type k: int
6
+ :rtype: int
7
+ """
8
+ longest_ones = 0
9
+
10
+ # Iterate over all starting points
11
+ for i in range (len (nums )):
12
+
13
+ # Iterate over all possible subarrays starting from i
14
+ for j in range (i + 1 , len (nums ) + 1 ):
15
+ window_len = j - i
16
+ num_zeros = nums [i :j ].count (0 ) # O(k) operation
17
+ if num_zeros <= k :
18
+ longest_ones = max (longest_ones , window_len )
19
+ return longest_ones
You can’t perform that action at this time.
0 commit comments