넘치게 채우기

[LeetCode] 896. Monotonic Array 본문

PS/LeetCode

[LeetCode] 896. Monotonic Array

riveroverflow 2023. 9. 29. 18:47
728x90
반응형

https://leetcode.com/problems/monotonic-array/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제 유형 : 배열

문제 난이도 : Easy

 

문제

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

32비트 정수 배열 nums가 주어진다.

nums 배열이 일관된 오름차순이거나 내림차순이면 true를, 아니면 false를 반환하라.

 

 

풀이

C++ <algorithm>헤더의 is_sorted()를 이용하여 정렬되어있는지 아닌지를 판별할 수 있다.

 

코드

C++

class Solution {
public:
    bool isMonotonic(vector<int>& nums) {
        if(is_sorted(nums.begin(),nums.end()))
            return true;
        else if(is_sorted(nums.begin(),nums.end(),greater<int>()))
            return true;
        return false;
    }
};
728x90
반응형

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] 557. Reverse Words in a String III  (0) 2023.10.01
[LeetCode] 456. 132 Pattern  (0) 2023.09.30
[LeetCode] 905. Sort Array By Parity  (0) 2023.09.28
[LeetCode] 1048. Longest String Chain  (0) 2023.09.23
[LeetCode] 377. Combination Sum IV  (0) 2023.09.09