账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    一个数组先升序再降序,用最优时间复杂度,求最大值?例如[1,2,2,2,2,3,1]
    37
    0

    一个数组先升序再降序,求最大值?例如[1,2,2,2,2,3,1],用最优时间复杂度,算法实现获取最大值3

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 这个问题可以通过一个双重循环来解决。外层循环控制升序数组,内层循环控制降序数组。

      以下是一个Python的解决方案:

      ```python def max_increasing_decreasing(arr): max_increasing = arr[0] max_decreasing = arr[0]

      for num in arr:
          if num > max_increasing:
              max_increasing = num
          elif num < max_decreasing:
              max_decreasing = num
      
      return max_increasing, max_decreasing
      

      ```

      这个函数的时间复杂度是O(n),其中n是数组的长度。这是因为我们只需要遍历一次数组。

      这个解决方案的空间复杂度也是O(n),因为我们需要存储两个变量max_increasing和max_decreasing,它们都是数组的一个元素。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部