前往
大廳
主題

(Easy)Leetcode 2529. Maximum Count of Positive Integer and Negative Integer

光陰LD | 2025-03-12 13:43:43 | 巴幣 2 | 人氣 23

給定一個non-decreasing array,回傳max(正數長度, 負數長度)

思考:
化簡為繁了
for num in nums:
負數則負rec++
正數則正rec++
我考慮了0,但其實不用考慮。起因是想省掉"如果第一個是正數就全正"的判斷
=====
狀態爛透了,半夜吃藥還是失眠,壓力太大了,久違的這種狀態
我大概,需要放個假,一兩天的那種就行了

class Solution:
    def maximumCount(self, nums: List[int]) -> int:
        if nums[0] > 0:
            return len(nums)
        elif nums[0] == 0:
            rec = 0
            while rec < len(nums) and nums[rec] == 0:
                rec += 1
            return len(nums) - rec
        else:
            rec_negative = 0
            while rec_negative < len(nums) and nums[rec_negative] < 0:
                rec_negative += 1
            rec_zeros = rec_negative
            while rec_zeros < len(nums) and nums[rec_zeros] == 0:
                rec_zeros += 1
            return max(rec_negative, len(nums) - rec_zeros)

送禮物贊助創作者 !
0
留言
追蹤 創作集

作者相關創作

更多創作