Skip to content

Instantly share code, notes, and snippets.

@LJH960101
Last active May 29, 2019 09:13
Show Gist options
  • Select an option

  • Save LJH960101/93a51dbf57e4e8155f49c8ae1d523837 to your computer and use it in GitHub Desktop.

Select an option

Save LJH960101/93a51dbf57e4e8155f49c8ae1d523837 to your computer and use it in GitHub Desktop.
maxStep
inline int getSum(int n) {
return (1 + n) * n/2;
}
int maxStep(int n, int obstacle) {
bool onObstacle = false;
for (int i = 1; i <= n; ++i) {
if (getSum(i) == obstacle) {
onObstacle = true;
break;
}
if (getSum(i) > obstacle) {
break;
}
}
if (onObstacle) return getSum(n) - 1;
else return getSum(n);
}
TEST(TestCaseName, TestName) {
EXPECT_EQ(maxStep(4, 6), 9);
EXPECT_EQ(maxStep(10, 55), 54);
EXPECT_TRUE(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment