Skip to content

Commit b8373e8

Browse files
committed
MaxConsecutiveOnes485
1 parent ee43f44 commit b8373e8

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 295
47+
# Total: 296
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 78 | 162 | 52 | 3 |
51+
| 79 | 162 | 52 | 3 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -296,6 +296,7 @@
296296
| [477. Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TotalHammingDistance477.java) | Medium |
297297
| [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SlidingWindowMedian480.java) | Hard |
298298
| [482. License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LicenseKeyFormatting482.java) | Easy |
299+
| [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaxConsecutiveOnes485.java) | Easy |
299300
| [490. The Maze](https://leetcode.com/problems/the-maze/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TheMaze490.java) | Medium |
300301
| [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NextGreaterElementII503.java) | Medium |
301302
| [508. Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MostFrequentSubtreeSum508.java) | Medium |

src/MaxConsecutiveOnes485.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Given a binary array, find the maximum number of consecutive 1s in this array.
3+
*
4+
* Example 1:
5+
* Input: [1,1,0,1,1,1]
6+
* Output: 3
7+
*
8+
* Explanation: The first two digits or the last three digits are consecutive 1s.
9+
* The maximum number of consecutive 1s is 3.
10+
*
11+
* Note:
12+
* The input array will only contain 0 and 1.
13+
* The length of input array is a positive integer and will not exceed 10,000
14+
*/
15+
16+
public class MaxConsecutiveOnes485 {
17+
public int findMaxConsecutiveOnes(int[] nums) {
18+
if (nums == null || nums.length == 0) return 0;
19+
int left = 0;
20+
int right = 0;
21+
int max = 0;
22+
while (right < nums.length) {
23+
if (nums[right] == 1) {
24+
right++;
25+
continue;
26+
}
27+
max = Math.max(max, right-left);
28+
left = right;
29+
while (left < nums.length && nums[left] == 0) left++;
30+
right = left;
31+
}
32+
max = Math.max(max, right-left);
33+
return max;
34+
}
35+
36+
37+
/**
38+
* https://leetcode.com/problems/max-consecutive-ones/discuss/96693/Java-4-lines-concise-solution-with-explanation
39+
*/
40+
public int findMaxConsecutiveOnes2(int[] nums) {
41+
int maxHere = 0, max = 0;
42+
for (int n : nums)
43+
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1);
44+
return max;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)