Last active
May 29, 2019 09:13
-
-
Save LJH960101/93a51dbf57e4e8155f49c8ae1d523837 to your computer and use it in GitHub Desktop.
maxStep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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