当前位置:网站首页>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)
边栏推荐
- Advantageous distinctive domain adaptation reading notes (detailed)
- Step 1: teach you to trace the IP address of [phishing email]
- LeetCode - 900. RLE iterator
- The imitation of jd.com e-commerce project is coming
- Seata分布式事务失效,不生效(事务不回滚)的常见场景
- Data classification: support vector machine
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Leetcode - 1172 plate stack (Design - list + small top pile + stack))
- Several problems encountered in installing MySQL under MAC system
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
猜你喜欢

Ind wks first week

High imitation wechat

Softmax regression (pytorch)

Jetson TX2 刷机

2018 Lenovo y7000 black apple external display scheme

Leetcode - 1172 plate stack (Design - list + small top pile + stack))

Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation

Judging the connectivity of undirected graphs by the method of similar Union and set search

Leetcode刷题---367

Implementation of "quick start electronic" window dragging
随机推荐
Leetcode - 705 design hash set (Design)
Secure in mysql8.0 under Windows_ file_ Priv is null solution
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
20220610 other: Task Scheduler
Jetson TX2 brush machine
Hands on deep learning pytorch version exercise solution - 2.6 probability
Leetcode刷题---189
Leetcode skimming ---217
2018 y7000 upgrade hard disk + migrate and upgrade black apple
QT creator uses OpenCV Pro add
Leetcode刷题---1385
What can I do to exit the current operation and confirm it twice?
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Preliminary knowledge of Neural Network Introduction (pytorch)
八、MySQL之事务控制语言
Tensorflow - tensorflow Foundation
Drop out (pytoch)
conda9.0+py2.7+tensorflow1.8.0
Ut2014 learning notes
波士顿房价预测(TensorFlow2.9实践)