当前位置:网站首页>【贪心】leetcode991. Broken Calculator
【贪心】leetcode991. Broken Calculator
2022-06-23 03:32:00 【暮色_年华】
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.
题意:x可以进行乘以2操作和减1操作,求x变为y的最少操作次数
思路:贪心
可以逆向考虑:y可以进行加1操作,或者除以 2的操作(y是偶数的情况下)
可以证明:当y是偶数的情况下,除以2所得的操作次数一定比加1 的操作次数小

所以,当y是奇数的情况下,y只能加1;当y是偶数的情况下,y只能除以2
当y小于x时,只能进行加1操作。
时间复杂度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;
}
}边栏推荐
- Concept and function of ES6 symbol
- Easynvr is displayed online after cascading the upper platform, but what is the reason for the video playback timeout?
- Composition and simple classification of IP addresses
- Using promise to process asynchronous operations
- Easygbs solution to video playback failure caused by tcp/udp configuration problems
- CentOS install redis
- Evolution of cloud firewall products
- If there is a smart bus visualization platform, can "beginning" restart indefinitely?
- Why can only a small number of condition type prices be maintained in me12 of SAP mm?
- Enterprise official website applet building tutorial - solution
猜你喜欢

Encryption related to returnee of national market supervision public service platform

Fetch request details
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]

Gakataka student end to bundle Version (made by likewendy)
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
随机推荐
How to batch generate jan13 barcode
JS event bubble and event capture
Fetch request details
JS how to delete an item specified in an array
JS to determine whether the page is opened for the first time today
CFS After the CHM file is opened, the hyperlink content cannot be loaded and blank is displayed
Use ES6 new Target to simulate abstract classes
The difference between the use of return, break and continue in the if statement in JS
How to generate IATA barcode in batch through TXT file
Simple analysis of easygbs compatible with old version HLS streaming address method [with code]
Exploration on the framework of stream batch integration technology and its practice in kangaroo cloud number stack
Form submit onclick and onsubmit
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
Wwdc21 - App store server API practice summary
Mybatties plus batch warehousing
Goframe framework (RK boot): enable tls/ssl
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]
CFs of cifs/smb protocol is mounted on win10/2019. Error 1272 is reported. The security policy prevents unauthenticated guest access
Use micro build to realize search function
Network security memorabilia - Summary of vulnerability exploitation events in 2021