当前位置:网站首页>[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
2022-07-02 06:53:00 【tomly2020】
Day 35
1060 Missing elements in an ordered array
There is an existing one, press Ascending Array of arranged integers nums
, Each of these numbers Different from each other .
Give you an integer k
, Please find and return the first... From the leftmost side of the array k
Missing number .
Method
Because the array is in ascending order , So we can consider using dichotomy to solve this problem , It's easy to know , For subscripts i
The elements of , The number of empty elements between it and the first element is nums[i]-nums[0]-i
, And what we are looking for is k
Missing elements , therefore , We just need to find out The interval between and the first element should not exceed k
The maximum subscript of , Then add and k
The difference between the , It's the one we want to find k
Missing elements .
class Solution {
public int missingElement(int[] nums, int k) {
int l = 0, r = nums.length - 1;
// Binary search for the largest subscript
while (l <= r){
int mid = (l + r) >> 1;
if (nums[mid] - nums[0] - mid >= k){
r = mid - 1;
}
else{
l = mid + 1;
}
}
return nums[r] + k - (nums[r] - nums[0] - r);
}
}
1901 Find the pinnacle element Ⅱ
One 2D In the grid Peak element That means Strictly greater than Its adjacent grid ( On 、 Next 、 Left 、 Right ) The elements of .
To give you one from 0 Numbered starting Of m x n
matrix mat
, The values of any two adjacent lattices are inequality . find Any one Peak element mat[i][j]
and Return to its position [i,j]
.
You can assume that the entire matrix is surrounded by a circle with a value of -1
Lattice of .
It is required that the time complexity must be written O(m log(n))
or O(n log(m))
The algorithm of
Method
According to the characteristics of the elements in the array , Because every number in the array is different , Then there must be such a peak element , And find the peak element , We can use the maximum value of column or row to deal with ( It is equivalent to a dimensionality reduction operation for the element group , We have the maximum value of each row or column to represent that row or column ). In this way, the problem is transformed into a one-dimensional array , Let's look for a peak .
For in a one-dimensional array , Algorithm for finding peak , We can use the idea of class dichotomy . The specific algorithm is as follows :
Definition l
and r
, Make mid=(l+r)/2
, Let's compare mid
and mid-1
, That is to investigate the increase and decrease of this community , If we find that , In this neighborhood , It's incremental , So in mid
There must be a peak element on the right of , otherwise mid
There must be a peak element on the left . By constantly narrowing the scope of search , Finally find the peak element .
When returning the answer , We just need to be in the column where the answer is , Just find the position of the largest element in the column .
class Solution {
public static int[] dx = {
0, 0, 1, -1};
public static int[] dy = {
1, -1, 0, 0};
public static int xLength;
public static int yLength;
public int[] findPeakGrid(int[][] mat) {
xLength = mat.length;
yLength = mat[0].length;
int l = 0, r = yLength - 1;
while (l <= r){
int mid = (l + r) >> 1;
int curMax = -1;
int preMax = -1;
for (int i = 0; i < xLength; ++i){
curMax = Math.max(curMax, mat[i][mid]);
if (mid - 1 >= 0) preMax = Math.max(preMax, mat[i][mid - 1]);
}
if (mid - 1 < 0 || preMax < curMax) l = mid + 1;
else r = mid - 1;
}
int Max = -1, index = -1;
for (int i = 0; i < xLength; ++i){
if (mat[i][r] > Max) {
Max = mat[i][r];
index = i;
}
}
return new int[]{
index ,r};
}
}
1380 The lucky number in the matrix
To give you one m * n
Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order .
Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :
The smallest of all elements in the same row
The largest of all elements in the same column
Method
Because each element in the matrix is different , therefore , There is at most one lucky number in a matrix , Prove slightly .
So we just need to find out the only lucky number .
We maintain the minimum value of each row and the maximum value of each column , And then iterate through the entire array , If you find that the minimum value of the row in a certain position is the same as the maximum value of the column , Then the number in this position is the answer .
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int[] lMax = new int[matrix.length];
int[] cMax = new int[matrix[0].length];
for (int i = 0; i < matrix.length; ++i){
lMax[i] = matrix[i][0];
for (int j = 0; j < matrix[0].length; ++j){
lMax[i] = Math.min(lMax[i], matrix[i][j]);
}
}
for (int j = 0; j < matrix[0].length; ++j){
for (int i = 0; i < matrix.length; ++i){
cMax[j] = Math.max(cMax[j], matrix[i][j]);
}
}
for (int i = 0; i < matrix.length; ++i){
for (int j = 0; j < matrix[0].length; ++j){
if (lMax[i] == cMax[j]) {
ArrayList<Integer> res = new ArrayList<>();
res.add(matrix[i][j]);
return res;
}
}
}
return new ArrayList<Integer>();
}
}
边栏推荐
- 20201002 vs 2019 qt5.14 developed program packaging
- In depth study of JVM bottom layer (II): hotspot virtual machine object
- pytest(3)parametrize参数化
- web自动中利用win32上传附件
- Self study table Au
- Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
- How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
- SQL注入闭合判断
- After reading useful blogs
- Date time API details
猜你喜欢
解决微信小程序swiper组件bindchange事件抖动问题
unittest.TextTestRunner不生成txt测试报告
The table component specifies the concatenation parallel method
js中对于返回Promise对象的语句如何try catch
unittest. Texttestrunner does not generate TXT test reports
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
[Zhang San learns C language] - deeply understand data storage
Redis -- cache breakdown, penetration, avalanche
A preliminary study on ant group G6
Self study table Au
随机推荐
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
virtualenv和pipenv安装
PgSQL learning notes
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
Loops in tensorrt
Selenium+msedgedriver+edge browser installation driver pit
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
SQL注入闭合判断
UEditor .Net版本任意文件上传漏洞复现
JS divides an array into groups of three
Error "list" object is not callable in Web automatic switching window
Fe - eggjs combined with typeorm cannot connect to the database
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
FE - weex 开发 之 使用 weex-ui 组件与配置使用
Common function writing method and set get writing method for calculating attributes
JS to determine whether there is a value in the object in the array
How to try catch statements that return promise objects in JS
Win电脑截图黑屏解决办法
js中对于返回Promise对象的语句如何try catch