当前位置:网站首页>[greed] leetcode991 Broken Calculator
[greed] leetcode991 Broken Calculator
2022-06-23 03:40:00 【Twilight_ years】
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
multiply the number on display by 2, or
subtract 1 from the number on display.
Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
The question :x You can multiply by 2 Operation and reduction 1 operation , seek x Turn into y The minimum number of operations
Ideas : greedy
Can be reversed :y You can add 1 operation , Or divide by 2 The operation of (y If it's even )
Can prove that : When y If it's even , Divide 2 The number of operations obtained must be greater than 1 The number of operations is small

therefore , When y In the case of odd numbers ,y Can only add 1; When y If it's even ,y Can only be divided by 2
When y Less than x when , Add only 1 operation .
Time complexity O(logY)
class Solution {
public int brokenCalc(int x, int y) {
int res=0;
while(y>x){
if(y%2==1){
y++;
}else{
y/=2;
}
res++;
}
return res+x-y;
}
}边栏推荐
- Static code block, code block, constructor execution order
- How to solve the problem that easynvr cannot be cascaded to the easynvs platform? View ports first
- 软件项目管理 8.4.软件项目质量计划
- Simply use the pagoda to build WordPress
- What is the difference between ArrayList and array?
- TRTC zero foundation -- Video subscription on the code
- What are the advantages and difficulties of introducing AI into ISP Technology
- Minecraft server technology explanation 𞓜 teach you how to get to the ashes from Xiaobai -- server technology explanation
- innodb_ruby 视角下 MySQL 记录增删改
- How to save the model obtained from sklearn training? Just read this one
猜你喜欢

直接插入排序

聊聊内存模型和内存序

R tree of search tree

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现

Using jhipster to build microservice architecture

Encryption related to returnee of national market supervision public service platform

Static code block, code block, constructor execution order

JS Part 4

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
随机推荐
Goframe framework (RK boot): realize distributed log tracing
Brief introduction to arm architecture
[leetcode] flip linked list II
Generate PDF417 code in batch through TXT file
The power of code refactoring: how to measure the success of refactoring
Form submit onclick and onsubmit
冒泡排序法
[advanced Android] entrusted by kotlin
Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e
Frequent actions to expand the scale of new energy industry Guangzhou plans to invest 1.4 billion in photovoltaic power generation projects
Golang resource embedding scheme
[machine learning] wuenda's machine learning assignment ex2 logistic regression matlab implementation
Banknext microservice: a case study
WordPress modifying fixed links and pseudo statics
New configuration of Alipay
How to store easydss version 3.0 video files in other free disks?
Using jhipster to build microservice architecture
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation
Even if you don't learn gradle, these common development operations are worth mastering
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]