Lintcode: Majority Number II 解题报告

Majority Number II

原题链接: http://lintcode.com/en/problem/majority-number-ii/#

Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

Find it.

Note

There is only one majority number in the array

Example

For [1, 2, 1, 2, 1, 3, 3] return 1

Challenge

O(n) time and O(1) space

SOLUTION 1:

与Majority Number 1相似。但我们要保存2个number.

1. 遇到第一个不同的值,先记录number 2.

2. 新值与n1,n2都不同,则cnt1,cnt2都减少

3. 当n1,n2任意一个为0时,从新值中挑出一个记录下来。

4. 最后再对2个候选值进行查验,得出最终的解。

主页君其实也想不太明白这个题目为什么这样解。

还是举个例子吧

7 1 7 7 61 61 61 10 10 10 61

n1      7 7 7 7  7   7   7   7   7 10 10

cnt1    1 1 2 3  2   2   2   1   0  1   1

n2       0 1 1 1  1  61  61 61 61 61 61

cnt2    0 1 1 1  0   1   2    1  0   0   1

证明:要对cnt1--的条件是:遇到2个与n1不同的值

1. 当n2为空时,第一个会填补n2, 第二个才会减cnt1.

2. 或者,n2已经被填补,这时会一直减cnt1,但是cnt2也会被减,当cnt2减到0,再遇到新值也不会再减了。也就是说,连续减n的情形,一定是前面连续

加过n2  n次(那n次连加n2时,不会减少cnt1)。

以上:2个与n1不同的值,才会对cnt1减。如果n1是majority值,它一定最后会留下 。

以上理论对于n2也适用。

 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @return: The majority number that occurs more than 1/3
 5      */
 6     public int majorityNumber(ArrayList<Integer> nums) {
 7         // write your code
 8         // When there are only 1 or 2 elements in the array,
 9         // there is no solution.
10         if (nums == null || nums.size() <= 2) {
11             return -1;
12         }
13
14         int n1 = 0;
15         int n2 = 0;
16
17         int cnt1 = 0;
18         int cnt2 = 0;
19
20         int size = nums.size();
21         for (int i = 0; i < size; i++) {
22             int num = nums.get(i);
23             if (cnt1 != 0 && num == n1) {
24                 cnt1++;
25             } else if (cnt2 != 0 && num == n2) {
26                 cnt2++;
27             } else if (cnt1 == 0) {
28                 cnt1 = 1;
29                 n1 = num;
30             } else if (cnt2 == 0) {
31                 cnt2 = 1;
32                 n2 = num;
33             } else {
34                 cnt1--;
35                 cnt2--;
36             }
37         }
38
39         // count the two candiates.
40         cnt1 = 0;
41         cnt2 = 0;
42         for (int num: nums) {
43             if (num == n1) {
44                 cnt1++;
45             } else if (num == n2) {
46                 cnt2++;
47             }
48         }
49
50         if (cnt1 < cnt2) {
51             return n2;
52         }
53
54         return n1;
55     }
56 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/math/MajorityNumber2.java

时间: 2024-10-22 04:28:34

Lintcode: Majority Number II 解题报告的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

Pascal&#39;s Triangle &amp; II 解题报告

杨辉三角,分别求前n行和第n行. [求杨辉三角前n行] Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 基础题,直接看代码,注意边界. public class Solution { public List<List<Integer>&g

hdu 1711 Number Sequence 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目意思:给出一条有n个数的序列a[1],a[2],......,a[n],和一条有m 个数的序列b[1],b[2],......,b[m],求出b[1],b[2],...,b[m]在序列a中完全匹配时,在序列a中的位置,如果找不到输出-1. 这几天一直在学kmp,该题算是kmp的入门题吧.有个地方要稍稍注意,代码中,主串和模式串的比较初始值为-1,-1,否则如果从0开始,会默认第一个字符是相

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

Majority Number II

题目描述 链接地址 解法 算法解释 题目描述 Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Example Given [1, 2, 1, 2, 1, 3, 3], return 1. Note There is only one majority number in the array. Challenge O(n

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an