Leetcode 441. Arranging Coins

Source: Internet
Author: User

You had a total of n coins so want to form in a staircase shape, where every K-th Row must had E xactly 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.

Example 1:

n = 5The coins can form the following rows:¤¤¤¤¤because the 3rd row is incomplete, we return 2.

Example 2:

n = 8The coins can form the following rows:¤¤¤¤¤¤¤¤because the 4th row is incomplete, we return 3.

"Problem Analysis"

The topic is very simple, actually is a arithmetic progression. The summation formula of arithmetic progression is: K (k+1)/2

Ideas

1. Direct traversal, starting from 1, if the remaining number can not constitute a row is returned. Be careful to determine whether the remaining number is satisfied, rather than accumulating it later, which can cause overflow.

2. Newton's Method

Combined with the previous topic, it is found that this topic can also be solved by Newton's method, the actual demand is x* (x+1)/2-n = 0 o'clock x value.

Derivation of recursive relations using Newton's method: Xi+1 = (xi*xi+2*n)/(2*xi+1)

Newton's method can be consulted: http://www.cnblogs.com/liujinhong/p/6014973.html

In this self-sqrt () function of the topic also used in the Newton method, but the results of Newton's law will have some pits, everyone in the use of the time to pay attention. In some cases the Newton method can only converge to a certain precision, the convergence value may be in the last few values of the cyclic change. Therefore, it is best to determine the convergence accuracy, and then return the value after judging the final result. In this topic I use Newton's method is just a kind of attempt, do not recommend everyone to use.

3. Direct derivation of results

Just look at the formula and you can.
/* Mathematical Deduction
Suppose complete the K-layer, altogether n, by the arithmetic progression summation formula has:
(1+k) *K/2 = n
Step-by-step deduction:
K+k*k = 2*n
K*k + k + 0.25 = 2*n + 0.25
(k + 0.5) ^ 2 = 2*n +0.25
K + 0.5 = sqrt (2*n + 0.25)
K = sqrt (2*n + 0.25)-0.5
Here k is a floating-point number, which is taken as the largest integer less than K can be
*/

"Java Code 1"

1  Public classSolution {2      Public intArrangecoins (intN) {3         if(N < 1)return0;4         intCount = 0, I;5          for(I=1; n-count>=i; i++){6Count + =i;7         }8         returnI-1;9     }Ten}

"Newton Method of Java code"

1  Public classSolution {2      Public intArrangecoins (intN) {3         if(N < 1)return0;4         DoubleLast = 0, res = 1,num =N;5         6          while(Math.Abs (last-res) > 10e-3) {7Last =Res;8Res = (res*res + 2*num)/(2*res + 1);9         }Ten          One         intCount = (int) Res; A         intRest = count%2 = = 0? N (COUNT/2) * (count+1): n-count* ((count+1)/2); -         returnRest <= count? Count:count+1; -     } the}

"Java code Math derivation method"

1  Public class Solution {2      Public int arrangecoins (int  n) {3         return (int) (math.sqrt (long) n+0.25)-0.5); 4     }5 }

These methods run around 50ms, the difference is not very big, but the last method is very concise. There are several other ways to help you understand this topic.

Leetcode 441. Arranging Coins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.