classSolution { public: intdistributeCandies(int n, int limit){ int ans = 0; for (int i = 0; i <= limit; i++) { for (int j = 0; j <= limit; j++) { if (i + j > n) { break; } if (n - i - j <= limit) { ans++; } } } return ans; } };
classSolution { public: intdistributeCandies(int n, int limit){ int ans = 0; for (int i = 0; i <= min(limit, n); i++) { if (n - i > 2 * limit) { continue; } ans += min(n - i, limit) - max(0, n - i - limit) + 1; } return ans; } };