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 ...
随机推荐
- Prism -- 简介
Prism是一个开源框架,用于在WPF.Xamarin Forms.Uno/Win UI等应用中创建松耦合.可维护.可测试的XAML应用程序.Prism提供了一组设计模式的实现,这些设计模式有助于编写 ...
- ALGO基础(一)—— 排序
ALGO基础(一)-- 排序 冒选插希快归堆,以下均为从小到大排 1 冒泡排序 描述: 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一 ...
- Ubuntu的点滴-Vim、VM Tools、Samba、换源
<div id="article_content" class="article_content clearfix"> <link rel=& ...
- Docker 下Elasticsearch 的安装 和ik分词器
(1)docker镜像下载 docker pull elasticsearch:5.6.8 (2)安装es容器 docker run -di --name=changgou_elasticsearch ...
- Magicodes.IE Excel合并行数据导入教程
说明 Magicodes.IE.Excel目前已支持合并行单元格导入,如本篇教程所示. 安装包Magicodes.IE.Excel Install-Package Magicodes.IE.Excel ...
- Nebula Storage 2.0 存储格式
随着 2.0 各版本的陆续发布,Nebula Graph 迎来了一系列的改动,在存储方面,影响最大的改动就是底层编码格式进行了修改.Nebula Graph 的底层存储是基于 KV 保存在 Rocks ...
- android分析之mutex
Android的锁是对Linux锁的一种包装: // ------------------------------------------------------------------------- ...
- Codeforces 976C Nested Segments
题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...
- springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署
知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...
- io流(对象流总结)
对象流 对象流就是对引用数据类型进行操作 序列化:将对象的状态信息转换为可以存储或传输的形式的过程,因此类需要序列化后才可以存储到文件中 对象输出流: 很简单,就三句话,将把一个对象导入指定文件中,要 ...