Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What would be time complexity of below problem? class Solution { public: bool isValid ( vector& weights, int days , int mid ) { int

What would be time complexity of below problem?
class Solution {
public:
bool isValid(vector& weights, int days ,int mid ){
int d =1, sum =0 ;
for ( int i =0 ; i < weights.size() ; i++){
if ( sum + weights[i]<= mid ){
sum += weights[i] ;
}
else {
d++;
sum = weights[i];
if( d > days || weights[i]> mid ) return false ;
}
}
return true ;
}
int shipWithinDays(vector& weights, int days){
int start =0, end =0, mid , answer =0 ;
for ( int i =0 ; i < weights.size() ; i++){
end += weights[i] ;
}
while ( start <= end ){
mid = start +( end - start )/2 ;
if(isValid( weights , days ,mid )){
answer = mid ;
end = mid -1 ;
}
else {
start = mid +1 ;
}
}
return answer ;
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

b. Determine the number of dependent variables, p.

Answered: 1 week ago