[leetcode]数组类题目总结与回顾(287,1014)

287. Find the Duplicate Number

 

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Example 1:

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

Example 2:

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

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

解题思路:

这道题要求要求更为严格,不能修改数组,同样也是常数的空间复杂度,时间复杂度小于O(n2)。我自己想的方法都不符合这些要求,实在想不出,最后去搜索了别人的解法,感觉实在是很NB!!,这里只记录二分法的解题思路,另一个很NB,时间复杂度更低的解可以参考:https://blog.csdn.net/wr339988/article/details/53617914中的第二种解法。

因为数组中的数都是在1和n之间,假如数组中的数没有重复,那么数组中的数小于或等于1-n的中间数的个数肯定是小于或等于这个中间数的。举个栗子就是:假如这里n为6,在[1,6]的数组中[1,2,3,4,5,6],小于或等于[1,6]的中间数mid (3)的个数是小于或等于3的。

有了这个结论后,那么来看有重复元素的数组中,计算数组中的元素值小于或等于3的元素个数cnt。

如果cnt > mid(3),说明重复元素肯定在值[1,3]中,那么在次区间再同样的进行搜索即可。

如果cnt <= 3,那么说明重复的数肯定是在后半段[4,6]中。

如此不断的缩小搜索范围,那么最终找到的数就是那个重复元素

代码如下:

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        if not nums:
            return -1
        start = 1
        end = len(nums) -1
        while start < end:
            mid = (start + end)//2
            cnt = self.countLtMid(nums,mid)
            if cnt > mid:
                end = mid
            else:
                start = mid +1
        return start
        
    def countLtMid(self,nums,mid):
        cnt = 0
        for i in nums:
            if i<= mid:
                cnt += 1
        return cnt

1014. Best Sightseeing Pair

题目描述:

Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.

The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.

 

Example 1:

Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

Note:

  1. 2 <= A.length <= 50000
  2. 1 <= A[i] <= 1000

解题思路:

求A[i] + A[j] + i - j的最大值,就是求A[i]+i +A[j]-[j]的最大值。

max(A[i]+A[j] +i-j) = max(A[i]+i +A[j]-[j]) = max(max(A[i]+i) +A[j]-[j])

因为要求i < j,因此需要记录每次j位置时,它之前i的A[i]+i的最大值。

代码如下:

class Solution:
    def maxScoreSightseeingPair(self, A: List[int]) -> int:
        if not A:
            return 0
        res = 0
        pre_i = 0
        # A[i]+A[j] +i-j = A[i]+i +A[j]-[j]
        # 对于当前j,保存其前面最大的A[i]+i,然后在求最大的结果
        # 即max(A[i]+A[j] +i-j) = max(A[i]+i +A[j]-[j]) = max(max(A[i]+i) +A[j]-[j])
        for j in range(1,len(A)):

            res = max(A[j]- j + A[pre_i] + pre_i,res)
            if A[j] + j > A[pre_i]+pre_i:
                pre_i = j
        return res

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值