Ejemplo n.º 1
0
		int trap(int A[], int n) {
			vector<int> height(n, 0), presum(n, 0), right_pair_pos(n, 0);
			arrayToVector(A, n, height);
			calcRightPairPos(height, right_pair_pos);
			calcHeightPresum(height, presum);
			return getWaterTrapped(height, presum, right_pair_pos);
		}
 vector<int> brute_force(vector<int>& A) {
     // O(N^2)
     vector<int> presum(A.size() + 1, 0);
     presum[0] = 0;
     for (int i = 1; i <= A.size(); i++) {
         if (i == 1) {
             presum[i] = A[i - 1];
         } else {
             presum[i] = presum[i - 1] + A[i - 1];
         }
     }
     
     vector<int> ans;
     int biggest = INT_MIN;
     for (int i = 0; i < A.size(); i++) { // [0, n - 1]
         for (int j = i + 1; j <= A.size(); j++) { // [1, n]
             if (biggest < presum[j] - presum[i]) {
                 biggest = presum[j] - presum[i];
                 ans.clear();
                 ans.push_back(i);
                 ans.push_back(j - 1);
             }
         }
     }
     return ans;
 }
 /**
  * @param A an integer array
  * @return  A list of integers includes the index of 
  *          the first number and the index of the last number
  */
 vector<int> continuousSubarraySum(vector<int>& A) {
     // Write your code here
     vector<int> presum(A.size() + 1, 0);
     presum[0] = 0;
     for (int i = 1; i <= A.size(); i++) {
         if (i == 1) {
             presum[i] = A[i - 1];
         } else {
             presum[i] = presum[i - 1] + A[i - 1];
         }
     }
     
     vector<int> ans;
     int smallest_index = 0;
     int biggest = INT_MIN;
     for (int i = 1; i < presum.size(); i++) {
         if (presum[i] - presum[smallest_index] > biggest) {
             biggest = presum[i] - presum[smallest_index];
             ans.clear();
             ans.push_back(smallest_index);
             ans.push_back(i - 1);
         }
         if (presum[smallest_index] > presum[i]) {
             smallest_index = i;
         }
     }
     return ans;
 }
long long int Difsum(long long int x,long long int y,int d1,int d2)
 {
   long long int s=0;
   int i;
   s=s+Ordsum(x,(pow(10,d1)-1));
   s=s+Ordsum(y,pow(10,d2-1));
   for(i=d1+1;i<d2;i++)
    s=s+presum(i);
   return s;
 }   
 /**
  * @param A an integer array
  * @return  A list of integers includes the index of 
  *          the first number and the index of the last number
  */
 vector<int> continuousSubarraySumII(vector<int>& A) {
     // Write your code here
     
     int sum = 0;
     for (auto& a : A) {
         sum += a;
     }
     vector<int> presum(A.size() + 1, 0);
     presum[0] = 0;
     for (int i = 1; i <= A.size(); i++) {
         if (i == 1) {
             presum[i] = A[i - 1];
         } else {
             presum[i] = presum[i - 1] + A[i - 1];
         }
     }
     
     // get largest subarray
     vector<int> ans;
     int smallest_index = 0;
     int biggest = INT_MIN;
     for (int i = 1; i < presum.size(); i++) {
         if (presum[i] - presum[smallest_index] > biggest) {
             biggest = presum[i] - presum[smallest_index];
             ans.clear();
             ans.push_back(smallest_index);
             ans.push_back(i - 1);
         }
         if (presum[smallest_index] > presum[i]) {
             smallest_index = i;
         }
     }
     
     
     // get smallest subarray
     vector<int> ans2;
     int largest_index = 0;
     int smallest = INT_MAX;
     for (int i = 1; i < presum.size(); i++) {
         if (presum[i] - presum[largest_index] < smallest) {
             smallest = presum[i] - presum[largest_index];
             ans2 = computeRange(largest_index, i - 1, A.size());
         }
         if (presum[largest_index] < presum[i]) {
             largest_index = i;
         }
     }
     
     if (sum == smallest) { // needs better explain
         return ans;
     } else {
         return sum - smallest > biggest ? ans2 : ans;
     }
 }
 int kInversePairs(int n, int k) {
     vector<int> presum(1 + k);
     int MOD = 1000000007;
     for (int i = 1; i <= n; ++i) {
         vector<int> temp(1 + k);
         temp[0] = 1;
         for (int j = 1; j <= k; ++j) {
             int val = (presum[j] - ((j - i >= 0) ? presum[j - i] : 0) + MOD) % MOD;                
             temp[j] = (temp[j - 1] + val) % MOD;
         }
         presum = temp;
     }
     
     return (presum[k] - ((k > 0) ? presum[k - 1] : 0) + MOD) % MOD;
 }
Ejemplo n.º 7
0
 /**
  * @param matrix an integer matrix
  * @return the coordinate of the left-up and right-down number
  */
 vector<vector<int>> submatrixSum(vector<vector<int>>& matrix) {
     // Write your code here
     int rows = matrix.size();
     int cols = matrix[0].size();
     
     vector<vector<int> > ans;
     vector<vector<int> > presum(rows + 1, vector<int>(cols, 0));
     
     // init presum matrix
     for (int j = 0; j < cols; j++) {
         for (int i = 1; i < rows + 1; i++) {
             presum[i][j] = presum[i - 1][j] + matrix[i - 1][j];
         }
     }
 
     for (int top = 0; top < rows + 1; top++) {
         for (int down = top + 1; down < rows + 1; down++) {
             
             vector<int> linepresum = computeLinePresum(presum, top, down);
             map<int, int> exist;
             int i = 0;
             while (i < linepresum.size() && exist.find(linepresum[i]) == exist.end()) {
                 exist[linepresum[i]] = i;
                 i += 1;
             }
             if (i < linepresum.size()) {
                 // find it!
                 vector<int> p{top, exist[linepresum[i]]};
                 vector<int> p2{down - 1, i - 1};
                 ans.push_back(p);
                 ans.push_back(p2);
                 return ans;
             }
         }
     }
     return ans;
 }