leetcode--441--Arranging coins

Source: Internet
Author: User

Problem Description:

You have a total of n coins, you need to put them in a ladder shape, the K -line must have exactly the k coin.

Given a number n, find the total number of rows that can form a complete ladder row.

N is a non-negative integer and is within the range of a 32-bit signed integer.

Example 1:

n = 5 coins can be arranged in the following lines: ¤¤¤¤¤ returns 2 because the third line is incomplete.

Example 2:

n = 8 coins can be arranged in the following lines: ¤¤¤¤¤¤¤¤ returns 3 because the row is incomplete.

Method 1:

1 classsolution (object):2     defarrangecoins (self, n):3         """4 : Type N:int5 : Rtype:int6         """        7Low =08High =N9         Ten          whilelow<=High : Onemid = Int ((low+high)//2) A              -             ifmid* (mid+1)/2 <= N < (mid+1) * (mid+2)/2: -                 returnMid the             elifmid* (mid+1)/2 >N: -High = Mid-1 -             elif(mid+1) * (mid+2)/2 <=N: -Low = mid + 1

Amazing:

1 class solution (object): 2     def arrangecoins (self, n): 3          return Int (((8*n + 1) **0.5-1)/2)

Ditto:

class solution (Object):     def arrangecoins (self, N):         """         : Type n:int        : Rtype:int        "        " "= Int ((2*n+0.25) **0.5-0.5)         return K

2018-10-03 21:33:05

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.