Uber Round 1 DSA Problem
502
Aug 27, 2025
Apr 17, 2026
Find available parking space on a given day. Parking regions are consecutive from 0 to n.
Given an array where the ith element shows the parking region for the ith day.
Return an array of parking space for the ith day region which is unoccupied and can be used for parking.
park_region[] = {{3,5},{6,7},{8,9},{1,12}}
available_region[]={3,2,2,5}
set<pair<int,int>> st;
int intersection(pair<int,int> p,vector<int>& region)
{
if(p.second<region[0] || p.first>region[1])
{
return 0;
}
int start=max(p.first,region[0]);
int end = min(p.second,region[1]);
return (end-start+1);
}
int solve(vector<int> ®ion)
{
int total = (region[1]-region[0]+1);
auto it = st.lower_bound({region[0],INT_MAX});
if(it!=st.begin())
{
it--;
if((*it).second<region[0])
{
it++;
}
}
int start=region[0];
int end=region[1];
while(it!=st.end() && (end>=(*it).first))
{
start=min((*it).first,start);
end = max((*it).second,end);
total-=intersection(*it,region);
it=st.erase(it);
//cout<<start<<" "<<end<<endl;
}
auto left_it = st.lower_bound({start,INT_MAX});
if(left_it!=st.begin())
{
left_it--;
if((*left_it).second+1==start)
{
start=(*left_it).first;
st.erase(left_it);
}
}
auto right_it = st.lower_bound({end+1,INT_MAX});
if(right_it!=st.end() && (*right_it).first==(end+1))
{
end=(*right_it).second;
st.erase(right_it);
}
st.insert({start,end});
return total;
}
int main()
{
vector<vector<int>> park_region={{3,5},{6,7},{8,9},{1,12}};
for(int i=0;i<park_region.size();i++)
{
cout<<solve(park_region[i])<<" ";
}
}This is a merge interval problem.
Comments (3)


