当前位置:网站首页>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)
边栏推荐
- 20220610 other: Task Scheduler
- Numpy Foundation
- Inverse code of string (Jilin University postgraduate entrance examination question)
- Julia1.0
- 深度学习入门之自动求导(Pytorch)
- Leetcode - 705 design hash set (Design)
- Tensorflow—Neural Style Transfer
- Leetcode刷题---704
- Model evaluation and selection
- 8、 Transaction control language of MySQL
猜你喜欢

波士顿房价预测(TensorFlow2.9实践)

一步教你溯源【钓鱼邮件】的IP地址

Multilayer perceptron (pytorch)

Ut2015 learning notes

八、MySQL之事务控制语言
![[LZY learning notes -dive into deep learning] math preparation 2.1-2.4](/img/92/955df4a810adff69a1c07208cb624e.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4

Policy gradient Method of Deep Reinforcement learning (Part One)

Training effects of different data sets (yolov5)

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

权重衰退(PyTorch)
随机推荐
20220531 Mathematics: Happy numbers
Leetcode skimming ---263
重写波士顿房价预测任务(使用飞桨paddlepaddle)
Leetcode-404: sum of left leaves
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
七、MySQL之数据定义语言(二)
An open source OA office automation system
QT creator uses OpenCV Pro add
Inverse code of string (Jilin University postgraduate entrance examination question)
[LZY learning notes dive into deep learning] 3.5 image classification dataset fashion MNIST
Configure opencv in QT Creator
深度学习入门之自动求导(Pytorch)
8、 Transaction control language of MySQL
A super cool background permission management system
Model selection for neural network introduction (pytorch)
Knowledge map enhancement recommendation based on joint non sampling learning
Install yolov3 (Anaconda)
Julia1.0
Weight decay (pytorch)
High imitation Netease cloud music