829. Consecutive Numbers Sum
Given a positive integer
N, how many ways can we write it as a sum of consecutive positive integers?
Example 1:
Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3Example 2:
Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4Example 3:
Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Note:
1 <= N <= 10 ^ 9.
Approach #1: Math. [Java]
class Solution {
public int consecutiveNumbersSum(int N) {
int count = 1;
for (int k = 2; k < Math.sqrt(2*N); ++k) {
if ((N - (k * (k - 1) / 2)) % k == 0)
count++;
}
return count;
}
}
Analysis:
The though process goes like this - Given a number N, we can possibly write it as a sum of 2 numbers, 3 numbers, 4 numbers and so on. Let's assum the first number in this serise be x. Hence, we should have:
x + (x + 1) + (x + 2) + ... + (x + k) = N
kx + k * (k - 1) / 2 = N
kx = N - k * (k - 1) / 2
So, we can calculate the RHS for every value of k and if it is a multiple of k then we can construct a sum of N using k terms string from x.
Now, the question arises, till what value of k should we loop for?
That's easy. In the worst case, RHS should be greater than 0, That is
N - k * (k - 1) / 2 > 0
k * (k - 1) < 2 * N
~ k * k < 2N ==> k < sqrt(2N)
Hence the overall complexity of the algorithm is O(sqrt(N)).
Reference:
829. Consecutive Numbers Sum的更多相关文章
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...
- [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...
- [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers
SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...
随机推荐
- CentOS7系统重置root密码
https://blog.csdn.net/qq_42969074/article/details/88080821
- 不使用map和set实现LRU——那用List?
遇到一道面试题,不使用map和set实现LRU,要求get的时间复杂度为O(logn),put的时间复杂度不超过O(n).想到了用ArrayList来实现,保存有序的key.然而牵涉add节点,在保证 ...
- MYSQL的replace into
replace into t(id, update_time) values(1, now()); 或 replace into t(id, update_time) select 1, now(); ...
- 危害api收集
以下每一条代码,无论其通过什么方式被调用,在哪个类里被调用,传入什么参数,都具有唯一不变性(在逆向出来的的smali文件里),故可以作为匹配的凭证. 网络操作相关: Ljava/net/URL ...
- gtk编译之makefile的写法(之一)
在学习c语言GUI编程时想必大家都会遇见这样一个问题买就是每次编译都要敲`pkg-config --cflags --libs gtk+-2.0`这个烦恼吧 这是我们可以编写一个makefile文件这 ...
- Apache Pulsar 在能源互联网领域的落地实践
关于 Apache Pulsar Apache Pulsar 是 Apache 软件基金会顶级项目,是下一代云原生分布式消息流平台,集消息.存储.轻量化函数式计算为一体,采用计算与存储分离架构设计,支 ...
- 2018ICPC南京Problem G. Pyramid
题意: 询问类似于这样的三角形中:里面正三角形的个数是多少. 思路:打表找了个规律发现就是C4n+3 1 //#include<bits/stdc++.h> 2 #include& ...
- P1996_约瑟夫问题(JAVA语言)_可能是最简单的解法了!
思路:使用队列模拟. 判断是否为出圈的数.如果不是,把数加入队列尾部:如果是,输出并删除. 题目背景 约瑟夫是一个无聊的人!!! 题目描述 n个人(n<=100)围成一圈,从第一个人开始报数,数 ...
- greenplum6.14、GPCC6.4安装详解
最近在做gp的升级和整改,所以把做的内容整理下,这篇文章主要是基于gp6.14的安装,主要分为gp,gpcc,pxf的一些安装和初始化.本文为博客园作者所写: 一寸HUI,个人博客地址:https:/ ...
- SqlServer触发器的创建与使用
前言 上期我们介绍了SqlServer的视图和存储过程创建与使用,这期我们介绍一下触发器. 有需要回顾的可以电梯直达看一下: SqlServer视图的创建与使用 SqlServer存储过程的创建与使用 ...