C++ solution return random value but fail to pass test case six?


  • 0
    F

    I submit my solution and guarantee that it's totally random return. Why I still can't pass test case six? Do I miss anything?

    class RandomerType {
        private :
            int count_;
            int temp_;
            int model_;
        public :
            RandomerType(int count, int t, int n) : count_(count), temp_(t), model_(n){};
            
            int operator()(int index) {
                srand(index * count_);
                temp_ = temp_ + (rand() % 1000);
                return temp_ % model_;
            };
    };
    
    class Solution {
    private:
        vector<int> cards_;    
        int count_ = 0;
    public:
        Solution(vector<int> nums) {
            cards_ = nums;
        }
        
        /** Resets the array to its original configuration and return it. */
        vector<int> reset() {
            return cards_;
        }
        
        /** Returns a random shuffling of the array. */
        vector<int> shuffle() {
            size_t n = cards_.size();
            ++ count_;
            srand(count_ + time(0));
            int t = rand();
            vector<int> result = cards_;
            auto random_er = RandomerType(count_, t, n);
            random_shuffle(result.begin(), result.end(), random_er);
            return result;
        }
    };
    

  • 0
    F

    Because, in this solution, it was not probabilistically equal to return a uniform possible result. Check this : https://github.com/gzc/CLRS/blob/master/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md


Log in to reply
 

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