Arranging Coins

2023-12-27 04:59
文章标签 coins arranging

本文主要是介绍Arranging Coins,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目地址:https://leetcode.com/problems/arranging-coins/

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.

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.

题目理解起来还是比较容易的,就是看一下等差数列 1,2,3,4,......i 的和,等差数列前 i 项和公式:


Si=ia1+i(i1)2d

在本题目中,我们做的其实就是 a1=1 d=1 的情况,于是有:


Si=i(i+1)2

我们要确定的就是 Si 刚好小于硬币个数n时候的i,也就是等差数列的第i项,于是有:


Si<=n<Si+1


i(i+1)2<=n<(i+1)(i+2)2


i(i+1)<=2n<(i+1)(i+2)

于是我们代码可以这么写:

public class ArrangingCoins {public static int arrangeCoins(int n) {for (int i = 1; i < n; i++) {if ((i * (i + 1) <= 2 * n) && ((i + 1) * (i + 2) > 2 * n))return i;continue;}return 0;}public static void main(String[] args) {// 超时报错System.out.println(arrangeCoins(1804289383));}
}

可以看出,这个循环是从1开始的,如果n比较大的话,这个时间就玄乎了,有没有更快的方法呢?我们再看一下,现在知道了这个:


2n<(i+1)(i+2)


2n<i2+3i+2


2n+2<i2+4i+4=(i+2)2


2n+2<(i+2)2


i>2n+22

有了这个以后,那么我们前面的就不用算了,省的费那么多时间,于是代码实现为:

public class ArrangingCoins {public static int arrangeCoins(int n) {if (n <= 1)return n;int start = (int)Math.sqrt(n + 1) * (int)Math.sqrt(2) - 2;for (int i = start; i < n; i++) {if ((i * (i + 1) <= 2 * n) && ((i + 1) * (i + 2) > 2 * n))return i;continue;}return 0;}public static void main(String[] args) {System.out.println(arrangeCoins(1804289383));}
}

这回就快多了。

这篇关于Arranging Coins的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541941

相关文章

hdu 2844 Coins(多重背包 可达不可达)

http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意: 一位同学想要买手表,他有n种硬币,每种硬币已知有num[i]个。已知手表的价钱最多m元,问她用这些钱能够凑出多少种价格来买手表。 思路:多重背包。但不能直接转化为01背包求,因为数据太多,TLE无疑。。可以增加一个一维数组use[ i ],记录到达i元时j种钱用的次数。 #includ

UVA 562 Dividing coins (01背包基础)

【题目链接】:click here~~ 代码: /** Problem: UVA No.562* Running time: 0MS* Complier: C++* Author: ACM_herongwei* Create Time: 11:12 2015/9/9 星期三* zeroonebags * 将金币总价值的一半作为背包容量,然后zeronebags*/#in

uva 562Uva 562 Dividing coins

平衡问题,将n个硬币的总价值累加得到sum,再用sum/2作为背包容量对n个硬币做01背包处理,看能组成的最大容量是多少。 /********************** Author:fisty* Data:2014-10-02* uva562* ******************/#include <cstdio>#include <cstring>#include <alg

PAT 甲级 1048 Find Coins two pointer的写法

PAT 甲级 1048 Find Coins 这道题可以用二分、散列和two pointers三种方法实现,此前写过二分的方法:PAT 甲级 1048 Find Coins 二分的写法 这里是two pointer的写法 #include <bits/stdc++.h>using namespace std;int main(){#ifdef LOCALfreopen("input.t

PAT 甲级 1048 Find Coins 二分的写法

PAT 甲级 1048 Find Coins 二分的写法 散列的写法非常简单明了,LeetCode Problem List很前面就有类似的题。其实这道题用二分实现也是可以的,下面是二分的写法。之后准备做一个二分的专题总结。 #include <bits/stdc++.h>using namespace std;int main(){#ifdef LOCALfreopen("input

北大oj Coins

Problem: 北大oj Coins 文章目录 思路解题方法复杂度Code 思路 题目要求我们找出所有可能组成的金额总数,给定一系列硬币面值和每种硬币的数量。我们使用动态规划来解决这个问题。关键在于如何处理每种硬币数量大于1的情况,这需要对余数进行分组,以便于在遍历时能够有效地更新状态。 解题方法 我们首先初始化一个布尔数组dp,其长度为最大目标金额m加上1

POJ 1742 Coins 多重部分和问题

题目链接:POJ 1742 Coins E. Coins People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided

100 Stylized Resource Textures - Leather Fur Bones Coins More

100多种风格化的骨骼、毛皮、兽皮、皮革、硬币和其他资源相关纹理的集合,可用于各种不同的风格化/幻想/rpg风格的游戏环境。 在这个系列中,你会发现大量适合风格化/幻想/rpg风格游戏中各种不同环境的纹理——骨头、毛皮、兽皮、皮革、硬币、战利品、宝藏等等! 每个纹理都是可平铺/无缝的,并与各种不同的场景完全兼容——标准Unity地形、Unity标准着色器、URP、HDRP等等都是兼容的。 所有

hdu1398 Square Coins(生成函数)

/......................................................................................................................................................................................................

1048 Find Coins (25 分)并未AC

1048 Find Coins (25 分)并未AC Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of