当前位置:网站首页>Leetcode skimming ---278
Leetcode skimming ---278
2022-07-03 10:35:00 【Long time no see 0327】
subject : You are the product manager , Currently leading a team to develop new products . Unfortunately , The latest version of your product doesn't pass the quality test . Because each version is based on the previous version , So all versions after the wrong version are wrong .
Suppose you have n A version [1, 2, ..., n], You want to find out the first wrong version that caused all subsequent versions to go wrong .
You can call bool isBadVersion(version) Interface to determine version number version If there is an error in unit test . Implement a function to find the first wrong version . You should try to minimize calls to API The number of times .
Input :n = 5, bad = 4
Output :4
Method 1 : Two points search
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int left = 1, right = n;
while (left < right) { // Cycle until the left and right end points of the interval are the same
int mid = left + (right - left) / 2; // Prevent calculation overflow
if (isBadVersion(mid)) {
right = mid; // The answer in [left, mid] in
} else {
left = mid + 1; // The answer in [mid, right] in
}
}
// At this time there is left == right, The interval is reduced to a point , Is the answer
return left;
}
};
Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- Pytorch ADDA code learning notes
- [C question set] of Ⅵ
- Model selection for neural network introduction (pytorch)
- Leetcode刷题---832
- Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
- Leetcode刷题---1385
- GAOFAN Weibo app
- Are there any other high imitation projects
- High imitation wechat
- 神经网络入门之矩阵计算(Pytorch)
猜你喜欢
Are there any other high imitation projects
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
重写波士顿房价预测任务(使用飞桨paddlepaddle)
Jetson TX2 brush machine
Ut2015 learning notes
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
A complete answer sheet recognition system
Jetson TX2 刷机
Timo background management system
Model evaluation and selection
随机推荐
Tensorflow—Neural Style Transfer
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Weight decay (pytorch)
深度学习入门之线性代数(PyTorch)
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Implementation of "quick start electronic" window dragging
Data classification: support vector machine
Adaptive Propagation Graph Convolutional Network
Leetcode skimming ---217
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Jetson TX2 刷机
Ind wks first week
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
Numpy Foundation
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Matrix calculation of Neural Network Introduction (pytoch)
ThreadLocal原理及使用场景
Leetcode刷题---832
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation