
난이도: Easy
문제 설명
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers nums
. Return true
if nums
is a special array, otherwise, return false
.
문제 예제
Example 1:
Input: nums = [1]
Output: true
Explanation:
There is only one element. So the answer istrue
.
Example 2:
Input: nums = [2,1,4]
Output: true
Explanation:
There is only two pairs:(2,1)
and(1,4)
, and both of them contain numbers with different parity. So the answer istrue
.
Example 3:
Input: nums = [4,3,1,6]
Output: false
Explanation:nums[1]
andnums[2]
are both odd. So the answer isfalse
.
제한 사항
1 <= nums.length <= 100
1 <= nums[i] <= 100
✏️ Solution(솔루션)
class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
for i in range(1, len(nums)):
if nums[i-1] % 2 == nums[i] % 2:
return False
return True
오랜만에 Easy문제가 나온 것 같다.
각 nums의 요소를 탐색하며 인접한 요소와 함께 짝수이거나 홀수이면 조건을 만족하지 않기 때문에 False를 return하였고, 모든 요소를 탐색 완료하면 조건을 만족한다고 판단하여 True를 return하고 정답을 맞출 수 있었다.
깃허브: github
algorithmPractice/LeetCode/3429-special-array-i at main · laewonJeong/algorithmPractice
하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.
github.com
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 3105. Longest Strictly Increasing or Strictly Decreasing Subarray (Python) (0) | 2025.02.03 |
---|---|
[LeetCode] 1752. Check if Array Is Sorted and Rotated (Python) (0) | 2025.02.02 |
[LeetCode] 827. Making A Large Island (Python) (0) | 2025.01.31 |
[LeetCode] 785. Is Graph Bipartite? (Python) (0) | 2025.01.30 |
[LeetCode] 2658. Maximum Number of Fish in a Grid (Python) (0) | 2025.01.28 |