Baozi Leetcode solution 169: Major Element

Problem Statement

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

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

Example 2:

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

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

Simple but great question that can be solved in many ways, the voting one approves to be the most time and space efficient solution.

You can refer to Leetcode official solution for a detailed explanation.

Solutions

 1 public int majorityElement(int[] num) {
 2     int vote = 0;
 3     int res = 0;
 4
 5     for (int i = 0; i < num.length; i++) {
 6         if (vote == 0) {
 7             res = num[i];
 8             vote++;
 9         } else {
10             if (num[i] == res) {
11                 vote++;
12             } else {
13                 vote--;
14             }
15         }
16     }
17
18     return res;
19 }
20
21 public int majorityElementOptimized(int[] num) {
22     int vote = 0;
23     int majorityElement = 0;
24
25     for (int i = 0; i < num.length; i++) {
26         if (vote == 0) {
27             majorityElement = num[i];
28         }
29         vote += num[i] == majorityElement ? 1 : -1;
30     }
31
32     return majorityElement;
33 }

Voting implementation

Time Complexity: O(N) where N is the array size

Space Complexity: O(1) Consant space

References

原文地址:https://www.cnblogs.com/baozitraining/p/12053768.html

时间: 2024-11-12 23:25:53

Baozi Leetcode solution 169: Major Element的相关文章

Baozi Leetcode solution 229: Major Element II

Problem Statement Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Outp

Baozi Leetcode solution 54: Sprial Matrix

Problem Statement  Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5,

Baozi Leetcode solution 1292.&#160;Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Problem Statement Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square. Example 1: Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2

【LeetCode】169. Majority Element

题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 题解: Solution 1 () class

【LeetCode】169. Majority Element 解题小结

题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: 自己的思路大概是建立一个map,

LeetCode【169. Majority Element】

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 思路.1 排序,选择第n/2个数,调用STL的sort,

Baozi Leetcode solution 201: Bitwise AND of Numbers Range

Problem Statement Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. Example 1: Input: [5,7] Output: 4 Example 2: Input: [0,1] Output: 0 Problem link Video Tutorial You can find t

Baozi Leetcode solution 135: Candy

Problem Statement There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating

Baozi Leetcode solution 72. Edit Distance

Problem Statement Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: