extremely concise code, don't know why MLE


  • 0
    K

    code is here

    class Solution {
    public:
        Solution(vector<int> nums) {
            for (int i = 0; i < nums.size(); ++i)
                m[nums[i]].push_back(i);
        }
        
        int pick(int target) {
            return m[target][rand() % m[target].size()];
        }
    private:
        unordered_map<int, vector<int>> m;
    };
    

    passed all test case actually 13/13, but the last one the test case was [1,2,3,3,3] and just picked a whole bunch of 3s. i don't see how this case can cause MLE issue. at least it should be a huge one. just don't get it.. maybe leetcode just want you to use reservoir sampling. but i mean if you have lots of picks than building the object, then this is a better solution.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.