LeetCode---Can Place Flowers、Shortest Unsorted Continuous Subarray、Maximum Product of Three Numbers

47 篇文章 0 订阅
38 篇文章 0 订阅

605. Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2
Output: False

Note:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. n is a non-negative integer which won't exceed the input array size.

假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。

给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。

思路:三个一组来讨论。正常思路都应该这样!遍历flowerbed,如果f[i]==0,i==0或者f[i-1]==0,i==f.size-1或者f[i+1]==0,即这三个条件同时满足,说明可以种花,则f[i]==1,为了下一个条件判断准备,然后n–。当遍历结束后,如果n<=0,即花种完了,则返回true,否则false。
 

class Solution:
    def canPlaceFlowers(self, flowerbed, n):
        for i in range(len(flowerbed)):
            if flowerbed[i]==0 and (i==0 or flowerbed[i-1]==0) and (i==len(flowerbed)-1 or flowerbed[i+1]==0):
                flowerbed[i]=1
                n-=1
        return n<=0

581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。

你找到的子数组应是最短的,请输出它的长度。

思路:先进行排序,然后对比找不同的数段

class Solution:
    def findUnsortedSubarray(self, nums):
        e=s=-1
        snums=sorted(nums)
        for i in range(len(nums)):
            if snums[i]!=nums[i]:
                if s==-1:
                    s=i
                e=i
        if e!=s:
            return e-s+1
        else:
            return 0
        

628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]
Output: 6

Example 2:

Input: [1,2,3,4]
Output: 24

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。

思路:可以先对原数组进行排序,对排序后的数,组成最大乘积的情况可能有两种,一种就是最后三位数的乘积,一种就是可能有负数存在,前两个数和最后一个数的乘积,所以最后结果可以在这两值之间取max值

class Solution:
    def maximumProduct(self, nums):
        s=sorted(nums)
        return max(s[0]*s[1]*s[-1],s[-1]*s[-2]*s[-3])

643. Maximum Average Subarray I

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.

Example 1:

Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75

Note:

  1. 1 <= k <= n <= 30,000.
  2. Elements of the given array will be in the range [-10,000, 10,000].

给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。

思路:先计算前k个数的和,设置个滑动窗,然后依次后移一个数,每次加上这个数并且减去k里的第一个数,找最大的,最后算平均值

class Solution:
    def findMaxAverage(self, nums, k):
        res=sum(nums[0:k])
        maxs=res
        for i in range(k,len(nums)):
            res+=nums[i]-nums[i-k]
            maxs=max(res,maxs)
        return maxs/k

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值