Krononberg

1742. Maximum Number of Balls in a Box.cpp 본문

개발 로그/알고리즘

1742. Maximum Number of Balls in a Box.cpp

k._. 2021. 10. 18. 11:10

처음으로 map을 자연스럽게 써보았다.

 

🔑 주어진 번호를 쪼개서, key값을 만든다음, 그 key의 value를 1씩 늘려준다.

그리고 map에서 최대값을 찾는다.

 

class Solution {
public:
    int countBalls(int lowLimit, int highLimit) {
        map<int,int>m;
        for(int i=lowLimit; i<=highLimit;i++){
            string tmp = to_string(i);
            int sum =0;
            for(int j =0; j<tmp.size(); j++){
                sum+= tmp[j]-'0';
            }
            m[sum]++;
        }
        int max=-1;
        for(auto &it : m){
            if(it.second > max){
                max = it.second;
            }
        }
        return max;
    }
};