当前位置:网站首页>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)
边栏推荐
- 20220609 other: most elements
- 安装yolov3(Anaconda)
- Jetson TX2 brush machine
- Preliminary knowledge of Neural Network Introduction (pytorch)
- Automatic derivation of introduction to deep learning (pytoch)
- Leetcode刷题---217
- Ind FHL first week
- 侯捷——STL源码剖析 笔记
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- GAOFAN Weibo app
猜你喜欢

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)

Jetson TX2 刷机

Hands on deep learning pytorch version exercise solution - 2.3 linear algebra

6、 Data definition language of MySQL (1)

Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning

侯捷——STL源码剖析 笔记

神经网络入门之预备知识(PyTorch)

Linear regression of introduction to deep learning (pytorch)

Install yolov3 (Anaconda)

Jetson TX2 brush machine
随机推荐
Ind FHL first week
20220531 Mathematics: Happy numbers
Tensorflow—Image segmentation
Notes - regular expressions
CSDN, I'm coming!
20220608 other: evaluation of inverse Polish expression
Hands on deep learning pytorch version exercise solution - 2.6 probability
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Julia1.0
Leetcode刷题---202
Leetcode skimming ---10
Leetcode刷题---283
High imitation Netease cloud music
Ut2015 learning notes
Tensorflow—Neural Style Transfer
Simple real-time gesture recognition based on OpenCV (including code)
Neural Network Fundamentals (1)
Judging the connectivity of undirected graphs by the method of similar Union and set search
Ut2012 learning notes
R language classification