[LeetCode] Arrays 101 - Max Consecutive Ones

myminimin·2023년 7월 24일
0
post-thumbnail

모르는 단어랑 사용된 문장 적어두고 빠르게 넘어가자 !

capacity

  • We call the first one the capacity of the Array, and the second one the length of the Array. 첫 번째는 어레이의 용량, 두 번째는 어레이의 길이라고 합니다.

overwrite

  • This is something you'll need to keep track of yourself, and you won't get any errors if you overwrite an exsting DVD, or if you leave a gap in the Array. 이것은 자신을 추적하는 데 필요한 것이며 기존 DVD를 덮어쓰거나 어레이에 간격을 두어도 오류가 발생하지 않습니다.

provious

  • You might have noticed that we've been using a length variable in our previous examples, to keep track of the next empty index. 다음 빈 인덱스를 추적하기 위해 이전 예제에서 길이 변수를 사용하고 있음을 알아차렸을 것입니다.
// Create a new array with a capacity of 6.
int[] array = new int[6];

// Current length is 0, because it has 0 elements.
int length = 0;

// Add 3 items into it.
for (int i = 0; i < 3; i++) {
    array[i] = i * i;
    // Each time we add an element, the length goes up by one.
    length++;
}

System.out.println("The Array has a capacity of " + array.length);
System.out.println("The Array has a length of " + length);
	Running this code will give the following output:
    The Array has a capacity of 6
	The Array has a length of 3
   

Handling 자체는 '손질'과 같은 뜻이지만

  • Handling Array Parameters 배열 매개변수 처리가 된다.

binary '둘의'라는 뜻이지만

  • Given a binary array, find the maximum number of consecutive 1s in this array. 이진 배열이 주어지면 이 배열에서 연속되는 1의 최대 수를 찾으십시오. 이 된다.

  • luckily it's straightforward. 다행히 간단합니다.

iterate

  • Therefore, to iterate over all items in the Array, we can do the following. 따라서 배열의 모든 항목을 반복하려면 다음을 수행할 수 있습니다.
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        // Hint: Initialise and declare a variable here to 
        // keep track of how many 1's you've seen in a row.
        for (int i = 0; i < nums.length; i++) {
            // Do something with element nums[i].
        }
    }
}

fundamental

  • And that is the basics of Arrays that you'll need to get started! In the next chapter, we'll look at some of the fundamental techniques we use to work with Arrays. 이것이 시작하는 데 필요한 배열의 기본 사항입니다! 다음 장에서는 배열 작업에 사용하는 몇 가지 기본 기술을 살펴보겠습니다.

Consecutive

  • Max Consecutive Ones 최대 연속 것 - 연이은? 연결된?

1. Max Consecutive Ones

Given a binary array nums, return the maximum number of consecutive 1's in the array.

  • Example 1:
    - Input: nums = [1,1,0,1,1,1]
    - Output: 3
    - Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

  • Example 2:
    - Input: nums = [1,0,1,1,0,1]
    - Output: 2

  • Constraints:
    - 1 <= nums.length <= 105
    - nums[i] is either 0 or 1.

- my answer...

public class MaxConsecutiveOnes {
    public static int findMaxConsecutiveOnes(int[] nums) {
        int maxCount = 0;
        int currentCount = 0;

        for (int num : nums) {
            if (num == 1) {
                currentCount++;
            } else {
                currentCount = 0;
            }

            maxCount = Math.max(maxCount, currentCount);
        }

        return maxCount;
    }

해당 코드의 알고리즘은

  1. Initialize Variables: Initialize variables max_count to store the maximum number of consecutive 1's and current_count to count the consecutive 1's, both set to 0.
    변수 초기화: 최대 연속 1의 개수를 저장할 변수 max_count와 현재 연속된 1의 개수를 세는 변수 current_count를 0으로 초기화합니다.

  2. Traverse the Array: Traverse the given nums array and do the following for each element:

  • Check if the current element is 1.
    - If it is 1, increment the current_count by 1.
    - If it is 0, reset current_count to 0.
  • Update max_count with the maximum value between max_count and current_count.

배열 순회: 주어진 nums 배열을 순회하면서 다음을 수행합니다.

  • 현재 원소가 1인지 확인합니다.
    - 1이라면, current_count를 1 증가시킵니다.
    - 0이라면, current_count를 0으로 초기화합니다.
  • max_count를 current_count와 비교하여 더 큰 값으로 업데이트합니다.
  1. Return the Result: After traversing the array, return the value of max_count.
    결과 반환: 순회가 끝나면 max_count 값을 반환합니다.

1개의 댓글

comment-user-thumbnail
2023년 7월 24일

7.25 등원길 복습

답글 달기