简体   繁体   English

Leetcode:计算 O(n) 时间复杂度的元素

[英]Leetcode : Counting Elements with O(n) time Complexity

Given an integer array arr, count element x such that x + 1 is also in arr.If there're duplicates in arr, count them separately.给定一个 integer 数组 arr,计算元素 x 使得 x + 1 也在 arr 中。如果 arr 中有重复项,则分别计算。

Example 1: Input: arr = [1,2,3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.示例 1: 输入:arr = [1,2,3] Output: 2 解释:1 和 2 被计入原因 2 和 3 在 arr 中。

Example 2: Input: arr = [1,1,2] Output: 2 Explanation: 1 counted twice cause 2 is in arr.示例 2: 输入:arr = [1,1,2] Output: 2 解释:1 计数两次,因为 2 在 arr 中。

Example 3: Input: arr = [1,1,3,3,5,5,7,7] Output: 0 Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr.示例 3: 输入:arr = [1,1,3,3,5,5,7,7] Output: 0 解释:不计算数字,因为 arr 中没有 2、4、6 或 8。

Example 4: Input: arr = [1,1,2,2] Output: 2 Explanation: Two 1s are counted cause 2 is in arr.示例 4: 输入:arr = [1,1,2,2] Output: 2 解释:两个 1 被计数,因为 2 在 arr 中。

MyCode: (Does not work for Example 2 only(for now)) MyCode:(仅对示例 2 不起作用(目前))

int count = 0;
HashSet set = new HashSet();
for(int i=0;i<arr.length;i++) {
    if(set.contains(arr[i]-1) || set.contains(arr[i]+1)) {
        count++;
        set.add(arr[i]);
    }else {
        set.add(arr[i]);
    }
}
return count;

Why are you counting arr[i]-1 ?.你为什么要计算arr[i]-1 The condition set.contains(arr[i]+1) is enough to count as per the definition.条件set.contains(arr[i]+1)足以按照定义计算。

I guess you are trying to achieve this in a single pass.我猜你正试图一次性实现这一目标。 This does not work because, you have to count the duplicate occurences as well.这不起作用,因为您还必须计算重复出现的次数。

Example 2 would work if the input was in a different order.如果输入的顺序不同,示例 2 将起作用。 ex: [2,1,1] .例如: [2,1,1] your solution is more like counting the arr[i]+1 s than counting such numbers itself.您的解决方案更像是计算arr[i]+1 ,而不是计算这些数字本身。

Here's my code with 2 pass:这是我的 2 遍代码:

class Solution {
    public int countElements(int[] arr) {
        Set<Integer> lookup = new HashSet<>();
        for(Integer i: arr){
            lookup.add(i);
        }
        
        int count =0;
        
        for(Integer i:arr){
            if(lookup.contains(i+1)){
                count++;
            }
        }
        return count;
    }
}
  1. Add all elements to a set.将所有元素添加到集合中。 This has complexity of O(n) and helps with O(1) lookup.这具有 O(n) 的复杂性并有助于 O(1) 查找。
  2. Iterate through the array and see if that element is present in the set.遍历数组并查看该元素是否存在于集合中。 This has complexity of O(n) * O(1) viz O(n)这具有 O(n) * O(1) 即 O(n) 的复杂性

Although we are iterating the array twice, the time complexity will be O(n).尽管我们对数组进行了两次迭代,但时间复杂度为 O(n)。

Swift Solution O(n) Swift 解决方案 O(n)

class Solution {
    func countElements(_ arr: [Int]) -> Int {
        var arrSet = Set(arr)
        var result = 0

        for num in arr {
            if arrSet.contains(num+1) {
                result += 1
            }
        }
        return result
    }
}

simple solution简单的解决方案

class Solution:
    def countElements(self, arr: List[int]) -> int:
        c=0
        for i in arr:
         if(i+1 in arr):
                c+=1
        if(c>0):
            return c
        else:
            return 0```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM