多数元素II | N / 3重复号码

多数元素II | N / 3重复号码

 

在本教程中,我将讨论一个非常有趣的问题,即在数组中找到N / 3个重复数

给定大小为n的数组,编写代码以查找所有出现n / 3次以上的元素

注意:我们必须在线性时间和O(1)空间中解决此问题。

范例1:

输入:[3,2,3]

输出3

在此示例中,此数组的大小为3。我们必须找到出现次数大于n / 3的所有数字,其中n是数组的大小。数字3出现1次以上。

范例2:

输入:[1、1、1、3、3、2、2、2]

输出:[1、2]

 
  • 多数元素II |  N / 3重复号码

    多数元素II

这个问题是数组查找多数元素的变体。

在解决此问题之前,让我们讨论一些有助于我们有效解决此问题的要点。

对于长度为n的数组。

i)最多可以有一个大于n / 2倍的多数元素。

ii)最多可以有两个多数元素大于n / 3倍。

在本文中,我将讨论三种方法,它是解决此问题的Java代码。

编程视频教程

方法一–蛮力法

 

最简单的方法是使用两个for循环查找出现n / 3次以上的数字。

这种方法的时间复杂度为O(n ^ 2)。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Majority Element II - Brute Force Approach
public static List<Integer> majorityElementBruteForce(int[] nums) {
 
     int majorityCount = nums.length/3;
     Set<Integer> result = new HashSet<>();
 
    for(int i = 0; i < nums.length; i++) {
 
       int count = 0;
       for(int j = 0; j < nums.length; j++) {
 
            if(nums[i] == nums[j]) {
                count++;
            }
        }
 
        if(count > majorityCount) {
             result.add(nums[i]);
         }
     }
 
     return new ArrayList<>(result);
}

使用HashMap的多数元素II(> N / 3次)– Java代码

我们可以使用HashMap改善时间复杂度。这里的想法是遍历一个数组并计算每个数字的出现次数。将数字及其计数放入HashMap中。

然后遍历地图并检查计数大于n / 3的所有元素。

这种方法的时间复杂度为O(n),其空间复杂度也为O(n)。

关于数组的编程问题

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Find numbers which occurred greater than n/3 times
public static List<Integer> majorityElementUsingMap(int[] nums) {
 
    int majorityCount = nums.length/3;
    List<Integer> result = new ArrayList<>();
 
    Map<Integer, Integer> map = new HashMap<>();
 
    for(int i = 0; i < nums.length; i++) {
        map.put(nums[i], map.getOrDefault(nums[i],0)+1);
     }
 
    for(Map.Entry<Integer, Integer> record : map.entrySet()) {
 
         if(record.getValue() > majorityCount) {
            result.add(record.getKey());
         }
     }
 
     return result;
}

在O(1)空间中查找N / 3个重复数字– Java代码

在此示例中,让我们讨论如何找到在O(1)空间中出现N / 3次以上的数字。为了解决这个问题,我使用了博耶-摩尔多数表决算法。

在这里,我使用的观察结果是,最多可以有两个多数元素大于n / 3倍

Boyer-Moore多数表决算法将查找多数元素(如果存在)。在此算法中,首先我们找到多数元素的候选者。之后,我们验证该候选元素是否为多数元素。

这种方法的时间复杂度为O(n),其空间复杂度为O(1)。

插入删除GetRandom O(1)解决方案

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Boyer Moore Majority Vote Algorithm for Finding Number which Appears more than N/3 times.
*/
public static List<Integer> majorityElement(int[] nums) {
 
    int count1 = 0;
    int count2 = 0;
 
    Integer candidate1 = null;
    Integer candidate2 = null;
 
    //Traverse an array
    for(int n : nums) {
 
      if(candidate1 != null && candidate1 == n) {
           count1++;
 
      } else if (candidate2 != null && candidate2 == n) {
          count2++;
 
      } else if (count1 == 0) {
            candidate1 = n;
            count1++;
 
      } else if (count2 == 0) {
           candidate2 = n;
           count2++;
      
      } else {
          count1--;
          count2--;
      }
   }
  
   /*
     Once we found the candidate, let's verify it whether it's a majority element or not.
  */      
 
    List<Integer> result = new ArrayList<>();
    count1 = 0;
    count2 =0;
 
    for(int n : nums) {
       if(candidate1 != null && candidate1 == n) {
           count1++;
       }
 
       if (candidate2 != null && candidate2 == n) {
            count2++;
       }
    }
 
    int p = nums.length;
    if (count1 > p/3) result.add(candidate1);
    if (count2 > p/3) result.add(candidate2);
 
    return result;
}

使用递归对堆栈进行排序

从O(1)中的堆栈中获取最小元素

N / 3重复号码InterviewBit解决方案– Java

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Interview Bit Solution
public class Solution {
    public int repeatedNumber(final List<Integer> a) {
        int count1 = 0;
        int count2 = 0;
        
        Integer candidate1 = null;
        Integer candidate2 = null;
        
        for(int n : a) {
            
            if(candidate1 != null && candidate1 == n) {
                count1++;
            
            } else if (candidate2 != null && candidate2 == n) {
                count2++;
            
            } else if (count1 == 0) {
                candidate1 = n;
                count1++;
            
            } else if (count2 == 0) {
                candidate2 = n;
                count2++;
            
            } else {
                count1--;
                count2--;
            }
        }
        
        List<Integer> result = new ArrayList<>();
        count1 = 0;
        count2 =0;
        
        for(int n : a) {
             if(candidate1 != null && candidate1 == n) {
                count1++;
            
            }
            
            if (candidate2 != null && candidate2 == n) {
                count2++;
            
            }
        }
            
            int p = a.size();
            if (count1 > p/3) result.add(candidate1);
            if (count2 > p/3) result.add(candidate2);
 
            return result.size() > 0 ? result.get(0) : -1;
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值