Skip to content

Commit 31b7ecf

Browse files
add 485
1 parent c066806 commit 31b7ecf

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 485. Max Consecutive Ones
5+
*
6+
* Given a binary array, find the maximum number of consecutive 1s in this array.
7+
*
8+
* Example 1:
9+
*
10+
* Input: [1,1,0,1,1,1]
11+
* Output: 3
12+
* Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
13+
*
14+
* Note:
15+
* The input array will only contain 0 and 1.
16+
* The length of input array is a positive integer and will not exceed 10,000
17+
* */
18+
public class _485 {
19+
public static class Solution1 {
20+
public int findMaxConsecutiveOnes(int[] nums) {
21+
int maxLen = 0;
22+
for (int i = 0; i < nums.length; i++) {
23+
int currentLen = 0;
24+
while (i < nums.length && nums[i] == 1) {
25+
i++;
26+
currentLen++;
27+
}
28+
maxLen = Math.max(maxLen, currentLen);
29+
}
30+
return maxLen;
31+
}
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._485;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _485Test {
10+
private static _485.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _485.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.findMaxConsecutiveOnes(new int[]{1, 1, 0, 1, 1, 1}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy