← Home

1399. Count Largest Group

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
139

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1399. Count Largest Group, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are digitSum, countLargestGroup, count.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 28 ms, faster than 12.50% of C++ online submissions for Count Largest Group.
02//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Count Largest Group.
03class Solution {
04public:
05    int digitSum(int n){
06        int sum = 0;
07        
08        while(n){
09            sum += n%10;
10            n/=10;
11        }
12        
13        return sum;
14    };
15    
16    int countLargestGroup(int n) {
17        map<int, int> count;
18        int maxCount = 0;
19        
20        for(int i = 1; i <= n; i++){
21            int dsum = digitSum(i);
22            count[dsum]++;
23            maxCount = max(maxCount, count[dsum]);
24        }
25        
26        int ans = 0;
27        
28        for(auto it = count.begin(); it != count.end(); it++){
29            if(it->second == maxCount){
30                ans++;
31            }
32        }
33        
34        return ans;
35    }
36};
37
38//Speed up with count_if
39//https://leetcode.com/problems/count-largest-group/discuss/563295/C%2B%2BJava-Count
40//Runtime: 4 ms, faster than 87.50% of C++ online submissions for Count Largest Group.
41//Memory Usage: 6.2 MB, less than 100.00% of C++ online submissions for Count Largest Group.
42class Solution {
43public:
44    int digitSum(int n){
45        int sum = 0;
46        
47        while(n){
48            sum += n%10;
49            n/=10;
50        }
51        
52        return sum;
53    };
54    
55    int countLargestGroup(int n) {
56        //n's range: [1...10000], 9999's digit sum is 36, which is largest
57        vector<int> count(37);
58        int maxCount = 0;
59        
60        for(int i = 1; i <= n; i++){
61            int dsum = digitSum(i);
62            count[dsum]++;
63            maxCount = max(maxCount, count[dsum]);
64        }
65        
66        return count_if(count.begin(), count.end(), 
67            [&maxCount](int n){return n == maxCount;});
68    }
69};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.