Created
July 2, 2020 06:31
-
-
Save NAVNEETOJHA/d582cac1e3c9cbabd0a6292cf2670d0f to your computer and use it in GitHub Desktop.
This file contains 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
// You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. | |
// Given n, find the total number of full staircase rows that can be formed. | |
// n is a non-negative integer and fits within the range of a 32-bit signed integer. | |
public class Solution { | |
public int arrangeCoins(int n) { | |
int level = 1; | |
for (long sum = 0; sum <= n; level++) { | |
sum += level; | |
} | |
return Math.max(level - 2, 0); | |
} | |
public static void main(String args[]) { | |
Solution s = new Solution(); | |
System.out.println(s.arrangeCoins(8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment