Skip to content

Instantly share code, notes, and snippets.

@NAVNEETOJHA
Created July 2, 2020 06:31
Show Gist options
  • Save NAVNEETOJHA/d582cac1e3c9cbabd0a6292cf2670d0f to your computer and use it in GitHub Desktop.
Save NAVNEETOJHA/d582cac1e3c9cbabd0a6292cf2670d0f to your computer and use it in GitHub Desktop.
// 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