Time space both O(k) solution c++


  • 0
    F

    Here is my solution in O(k) space and O(k) time
    but I failed in precision problems . (especially for the case larger than 30)
    can anyone help?

    public:
        vector<int> getRow(int rowIndex) {
            
            double f=1,n=1;
            vector<int> res;
            res.push_back(1);
            for(int i = 0; i < (rowIndex+1)/2; i++)
            {
                f *= rowIndex - i;
                n *= i + 1;
                res.push_back(int(f/n));
                cout << f/n << endl;
            }
           
            for(int i = (rowIndex/2) -1 ; i >= 0; i--)
            {
                res.push_back(res.at(i));
            }
            
            
            
            return res;
        }
    };

  • 0
    F

    my idea is that
    C(0,0)
    C(1,0) C(1,1)
    C(2,0) C(2,1) C(2,2)
    C(3,0) C(3,1) C(3,2) C(3,3)
    ....

    So i only need to compute one line in one for loop
    actually I don' need another k space


Log in to reply
 

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