0과 1로 이루어진 nums 배열에서 연속된 1이 가장 많이 이어졌을때 수를 반환하시오
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
/*
이진 배열인 nums가 주어졌을때, 연속된 1의 최대수를 반환하시오.
1 <= nums길이 <= 10만
nums[i] -> 0 or 1
*/
int maximum = 0;
int temp = 0;
for(int number : nums) {
if(number == 1) {
temp++;
}else {
temp = 0;
}
if(temp >= maximum) maximum = temp;
}
return maximum;
}
}
Runtime: 1 ms, faster than 100.00% of Java online submissions for Max Consecutive Ones.
Memory Usage: 40.2 MB, less than 76.94% of Java online submissions for Max Consecutive Ones.
'Algorithm > LeetCode' 카테고리의 다른 글
[EASY] longest-palindrome (0) | 2023.01.11 |
---|---|
[LeetCode] - How Many Numbers Are Smaller Than the Current Number (0) | 2021.07.21 |
[LeetCode] Minimum Number of Operations to Move All Balls to Each Box (0) | 2021.06.30 |
[LeetCode] Jewels and Stones (0) | 2021.06.18 |
[LeetCode] Kids With the Greatest Number of Candies (0) | 2021.06.16 |