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 ...
随机推荐
- RabbitMQ(三) SpringBoot2.x 集成 RabbitMQ
3-1 RabbitMQ 整合 SpringBoot2.x 生产者发送消息 创建 SpringBoot 项目application.properties 配置 spring.rabbitmq.host ...
- 后端程序员之路 50、Go语言开发环境
Downloads - The Go Programming Languagehttps://golang.org/dl/ - windows下的开发环境 - 下载go1.8.1.windows ...
- Java多态练习题
需求: 宠物饿了,需要铲屎官给宠物喂食. 不同宠物吃的东西不一样. 不同宠物恢复后体力值不一样. 铲屎官和狗狗玩接飞盘游戏,狗狗健康值减少10,与铲屎官亲密度增加5 铲屎官和 企鹅玩游泳游戏,企鹅健康 ...
- URL 地址解析
//虚拟目录的路径 Response.Write("<strong>Request.ApplicationPath:</strong>" + Request ...
- AJAX 加载效果(遮盖层)
//创建遮罩层函数体 function createMask(){ var node=document.createElement('div'); node.setAttribute('id','ba ...
- 测试平台系列(4) 使用Flask蓝图(blueprint)
使用Flask蓝图(blueprint) 回顾 先来看一下上一篇的作业吧,使用「logbook」的时候,遇到了时区不对的情况.那么我们怎么去解决这个问题呢? 实际上logbook默认采用的是世界标准时 ...
- CMU数据库(15-445)Lab3- QUERY EXECUTION
Lab3 - QUERY EXECUTION 实验三是添加对在数据库系统中执行查询的支持.您将实现负责获取查询计划节点并执行它们的executor.您将创建执行下列操作的executor Access ...
- SQLServer 2008快速导出架构和数据脚本
https://jingyan.baidu.com/article/454316ab715218f7a7c03a9d.html
- Redis之数据类型和持久化及高可用
数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) String是r ...
- 如何使用jQuery $.post() 方法实现前后台数据传递
基础方法为 $.post(URL,data,callback); 参数介绍: 1.URL 参数规定您希望请求的 URL. 2.data 参数规定连同请求发送的数据. 3.callback 参数是请求成 ...