본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 671. Second Minimum Node In a Binary Tree

반응형

https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/

 

Longest Continuous Increasing Subsequence - LeetCode

Can you solve this real interview question? Longest Continuous Increasing Subsequence - Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasin

leetcode.com

간단 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

연속하는 증가 수열의 길이를 저장할 length와 중간 결과를 저장할 cnt를 선언 후 1로 초기화해줍니다.

📔 풀이과정

1. nums의 원소를 순회하며 인접 원소만 확인해서 증가한다면 cnt++, 아니라면 length에 cnt와 비교해 더 큰 값으로 갱신해줍니다.

2. 계속 증가할 수 있으니 마지막에 length와 cnt를 비교해 갱신해줍니다.

📔 정답 출력 | 반환

length를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int length = 1;
        int cnt = 1;
        for(int i = 1; i < nums.size(); i++) {
            if(nums[i-1] < nums[i]) {
                cnt++;
            }
            else {
                length = max(length, cnt);
                cnt = 1;
            }
        }
        length = max(length, cnt);
        return length;
    }
};

📕 Test Case

[1,3,5,7]

답: 4


*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.