예제 #1
0
VariablesGrid VariablesGrid::getTimeSubGrid(	double startTime,
												double endTime
												) const
{
    uint startIdx = getCeilIndex( startTime );
	uint endIdx   = getFloorIndex( endTime );

	VariablesGrid newVariablesGrid;

	if ( ( isInInterval( startTime ) == BT_FALSE ) || ( isInInterval( endTime ) == BT_FALSE ) )
		return newVariablesGrid;
	
	if ( ( startIdx >= getNumPoints( ) ) || ( endIdx >= getNumPoints( ) ) )
		return newVariablesGrid;

// 	if ( startIdx > endIdx )
// 		return newVariablesGrid;
	
	// add all matrices in interval (constant interpolation)
	if ( ( hasTime( startTime ) == BT_FALSE ) && ( startIdx > 0 ) )
		newVariablesGrid.addMatrix( *(values[ startIdx-1 ]),startTime );
	
	for( uint i=startIdx; i<=endIdx; ++i )
		newVariablesGrid.addMatrix( *(values[i]),getTime( i ) );
	
	if ( hasTime( endTime ) == BT_FALSE )
		newVariablesGrid.addMatrix( *(values[ endIdx ]),endTime );

    return newVariablesGrid;
}
  int lengthOfLIS(vector<int>& nums) {
    // Method 1: Dynamic programming - O(n^2) time
    /*if (nums.empty())  return 0;
    vector<int> maxLISLengthSoFar(nums.size(), 0);
    int overallMax = INT_MIN;
    for (int i = 0; i < nums.size(); i++) {
      for (int j = i - 1; j >= 0; j--) {
        if (nums[j] < nums[i])
          maxLISLengthSoFar[i] = max(maxLISLengthSoFar[j], maxLISLengthSoFar[i]);
      }
      ++maxLISLengthSoFar[i];
      overallMax = max(overallMax, maxLISLengthSoFar[i]);
    }
    return overallMax;*/

    // Method 2: Binary search - O(n log n) time
    if (nums.empty())  return 0;
    vector<int> maxLISLengthSoFar(nums.size(), 0);
    maxLISLengthSoFar[0] = nums[0];
    int length = 1;
    for (int i = 1; i < nums.size(); i++) {
      if (nums[i] < maxLISLengthSoFar[0])
        maxLISLengthSoFar[0] = nums[i];
      else if (nums[i] > maxLISLengthSoFar[length - 1])
        maxLISLengthSoFar[length++] = nums[i];
      else
        maxLISLengthSoFar[getCeilIndex(maxLISLengthSoFar, 0, length - 1, nums[i])] = nums[i];
    }
    return length;
  }