Greedy interval selection
Count the maximum compatible half-open talks [start, finish), with positive duration and endpoints representable as 32-bit integers.
| Maximize count | Sort by finish | Accept when start ≥ previous finish |
| Merge ranges | Sort by start | Carry the farthest end of the current group |
| Different rewards | Weighted scheduling | Earliest finish alone can lose total reward |
Python
def max_talks(talks):
end, count = float('-inf'), 0
for start, finish in sorted(talks, key=lambda t: t[1]):
if start >= end:
end, count = finish, count+1
return countC++
int maxTalks(std::vector<std::pair<int,int>> talks) {
std::sort(talks.begin(), talks.end(), [](auto a, auto b) {return a.second < b.second;});
long long end = LLONG_MIN; int count = 0;
for (auto [s,f] : talks) if (s >= end) { end = f; ++count; }
return count;
}Java
static int maxTalks(int[][] talks) {
java.util.Arrays.sort(talks, (a,b) -> Integer.compare(a[1],b[1]));
long end = Long.MIN_VALUE; int count = 0;
for (int[] t : talks) if (t[0] >= end) { end = t[1]; count++; }
return count;
}