Dp does not works but Greedy does , Why?

I am solving a question

on spoj which is kind of similar to activity selection problem .
First tried with dp but dp gave me the wrong ans but when i did it greedily and it worked .

But both the approaches are quite similar till dp not worked , Why is it so ?

DP Solution .

``````bool cmp ( pair <int,int> &a , pair<int,int> &b ) {
if (a.second == b.second)
return a.first<b.first;
return a.second<b.second;
}

void Go() {
int n = 0 ;
cin >> n ;

vpi v(n) ;
for ( auto &i : v ) {
cin >> i.ff >> i.ss ;
}

sort(all(v),cmp) ;
vi dp(n,1) ;

for ( int i = 1 ; i < n ; i++ ) {
int s = 0 , e = i-1 ;
int ans = 0 ;
while ( s <= e ) {
int mid = s+(e-s)/2 ;
if ( v[mid].ss <= v[i].ff ) {
ans = max ( ans , dp[mid] ) ;
s = mid+1 ;
} else {
e = mid-1 ;
}
}
dp[i] += ans ;
}
cout << *max_element(all(dp)) << Endl ;
}
``````

Greedy Solution :

``````bool cmp ( pair <int,int> &a , pair<int,int> &b ) {
if (a.second == b.second)
return a.first<b.first;
return a.second<b.second;
}

void Go() {
int n = 0 ;
cin >> n ;
vpi v(n) ;
for ( auto &i : v ) {
cin >> i.ff >> i.ss ;
}

sort(all(v),cmp) ;
int ans = 0 ;
int end = -1 ;

for ( int i = 0 ; i < n ; i++ ) {
if ( v[i].ff >= end ) {
ans++ ;
end = v[i].ss ;
}
}
cout << ans << Endl ;
}
``````

My question is that if we are doing exactly same thing in both apporaches why dp solution does not works ?