矩陣線性相關則矩陣行列式_搜索線性時間中的排序矩陣

矩陣線性相關則矩陣行列式

聲明 (Statement)

We have to search for a value x in a sorted matrix M. If x exists, then return its coordinates (i, j), else return (-1, -1).

我們必須在排序的矩陣M中搜索值x 。 如果x存在,則返回其坐標(i,j) ,否則返回(-1,-1)

Image for post

Let us consider the above matrix as an example. In this example, we are going to search for the value 12. Since 12 is present in the matrix, the algorithm should return its coordinates (2, 1)

讓我們以上述矩陣為例。 在此示例中,我們將搜索值12。由于矩陣中存在12,因此算法應返回其坐標(2,1)

簡單方法 (Simple Approach)

A simple approach is to traverse all the values in the matrix and check if it is equal to 12.

一種簡單的方法是遍歷矩陣中的所有值并檢查其是否等于12。

Image for post

The worst case time complexity of the above algorithm will be O(n x m) = O(n2) when n = m

n = m時,上述算法的最壞情況下時間復雜度將為O(nxm)= O(n2)

高效方法 (Efficient Approach)

The above algorithm behaves worse for large values of n and m. Let us look into the efficient algorithm now.

對于較大的n和m值,上述算法的性能較差。 現在讓我們研究高效算法。

算法 (Algorithm)

1. Start from Top Right position (0, m - 1) in the matrix M2. If the value is equal to x return (0, m - 1)3. Move one row down if the current value is less than x4. Move one column left if the current value is greater than x

Let us apply this algorithm into our matrix M. We are going to search for the value 12 in M

讓我們將此算法應用到矩陣M中。我們將在M中搜索值12

Image for post
Step 1 and 2
步驟1和2
  1. Start from the Top Right value 5 at M[0][4]. 5 is less than 12, so 12 should be somewhere in the bottom of the matrix since all row and column values are sorted in ascending order. So we move one row down.

    M [0] [4]的右上角值5開始 5小于12,因此12應該在矩陣底部的某個位置,因為所有行和列值都按升序排序。 因此,我們向下移動了一行。

  2. The value 10 at M[1][4] is also less than 12, so we move one row down.

    M [1] [4]上的值10也小于12,因此我們向下移動了一行。

Image for post
Step 3 and 4
步驟3和4

3. The value 15 at M[2][4] is greater than 12, so 12 should be somewhere in the left of the matrix, so we move one column left.

3. M [2] [4]上的值15大于12,因此12應該在矩陣的左側某處,因此我們向左移動一列。

4. The value 14 at M[2][3] is also greater than 12, so we move one column left

4. M [2] [3]上的值14也大于12,因此我們向左移動一列

Image for post
Step 5 and 6
步驟5和6

5. The value 13 at M[2][2] is greater than 12, so we move one column left

5. M [2] [2]的值13大于12,所以我們向左移動一列

6. The value at M[2][1] is equal to 12, so we return its index (2, 1)

6. M [2] [1]處的值等于12,因此我們返回其索引(2,1)

The worst case time complexity of the above algorithm will be O(n + m) = O(2n) = O(n) when n = m, because we will be iterating all rows and all columns once if x is at the bottom left position (n - 1, 0)

n = m時,上述算法的最壞情況下時間復雜度將為O(n + m)= O(2n)= O(n) 因為如果 x 位于左下角 我們將對所有行和所有列進行一次迭代 位置 (n-1,0)

You can find the Java solution for this algorithm in the Github repo.

您可以在Github存儲庫中找到該算法的Java解決方案。

謝謝🙏 (Thank you 🙏 👋🤘)

Linkedin | Github

Linkedin | Github

翻譯自: https://medium.com/swlh/search-sorted-matrix-in-linear-time-c999234d39a

矩陣線性相關則矩陣行列式

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388370.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388370.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388370.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算機英文版個人簡歷發文,計算機個人簡歷英文_英文簡歷.doc

計算機個人簡歷英文_英文簡歷I have the honor to present a brief introduction of myself to you in compliance with the requirements of your graduate admissionI was born in November 7th, 1966, at the town of Changing, Beijing. My parents are doing business and…

一地雞毛 OR 絕地反擊,2019年區塊鏈發展指南

如果盤點2018年IT技術領域誰是“爆款流量”,那一定有個席位是屬于區塊鏈的,不僅經歷了巨頭、小白紛紛入場的光輝歲月,也經歷了加密貨幣暴跌,爭先退場的一地雞毛。而當時間行進到2019年,區塊鏈又將如何發展呢? 近日,全球知名創投研究機構CBInsight發布了《What’s Next …

iphone UITableView及UIWebView的使用

1。新建一個基于Navigation-based Application的工程。 2。修改原來的RootViewController.h,RootViewController.m,RootViewController.xib為MyTableViewController.h,MyTableViewController.m,MyTableViewController.xib。 3。點擊MainVindow.xib,將R…

python 的datetime模塊使用

1.datetime模塊主要是5個類 date #日期類 年月日 datetime.date(year,month,day) time #時間類 時分秒 datetime.time(hour,minute,second,microsecond,tzoninfo),返回18:29:30 datetime #日期時間類 datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinf…

物聯網數據可視化_激發好奇心:數據可視化如何增強博物館體驗

物聯網數據可視化When I was living in Paris at the beginning of this year, I went to a minimum of three museums a week. While this luxury was made possible by the combination of an ICOM card and unemployment, it was founded on a passion for museums. Looking…

計算機公開課教學反思,語文公開課教學反思

語文公開課教學反思引導語: 在語文的公開課結束后,教師們在教學 有哪些需要反思的呢?接下來是yjbys小編為大家帶來的關于語文公開課教學反思,希望會給大家帶來幫助。篇一:語文公開課教學反思今天早上,我上了一節語文…

中國連續十年成馬來西亞最大貿易伙伴

中新社吉隆坡1月30日電 (記者 陳悅)馬來西亞國際貿易和工業部30日發布的2018年馬來西亞貿易報告顯示,2018年馬來西亞與中國的貿易額約為3138.1億林吉特(馬來西亞貨幣,約合774億美元),較上年同期增長8.1%,約占馬來西亞對外貿易總額…

Iphone NSMutableArray,NSMutableDictionary AND 動態添加按鈕

一.NSMutableDictionary NSMutableDictionary * tags; 1.NSMutableDictionary 添加內容: [tags setValue:xxx forKey :xxx]; 2.NSMutableDictionary 遍歷: for(NSString * title in tags){ //其中得到的title是key } 3.NSMutableD…

bzoj2938: [Poi2000]病毒

被Star_Feel大爺帶著做題 明顯大力AC機然后找環 本來我一開始想的是先去有另一個病毒為前綴的病毒,結果今天早上寫的時候偷懶沒寫 結果跳fail的時候會跳到中間。。。無語,Star_Feel大爺叫我son或一下now和fail就過了 還有神仙是直接把fail接到兒子的更流…

bigquery數據類型_將BigQuery與TB數據一起使用后的成本和性能課程

bigquery數據類型I’ve used BigQuery every day with small and big datasets querying tables, views, and materialized views. During this time I’ve learned some things, I would have liked to know since the beginning. The goal of this article is to give you so…

中國計算機學科建設,計算機學科建設戰略研討會暨“十四五”規劃務虛會召開...

4月15日下午,信息學院計算機系舉辦了計算機科學與技術學科建設戰略研討會暨“十四五”規劃務虛會。本次會議的主旨是借第五輪學科評估的契機,總結計算機學科發展的優劣勢,在強調保持優勢的同時,更著眼于短板和不足,在未…

iphone開發如何隱藏各種bar

轉載至:http://blog.csdn.net/riveram/article/details/7291142 狀態條StatusBar [cpp] view plaincopyprint?[UIApplication sharedApplication].statusBarHidden YES; [UIApplication sharedApplication].statusBarHidden YES; 導航條NavigationBar [cpp] v…

Swift5.1 語言指南(九) 閉包

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公眾號:山青詠芝(shanqingyongzhi)?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)?GitHub地址&a…

服務器被攻擊怎么修改,服務器一直被攻擊怎么辦?

原標題:服務器一直被攻擊怎么辦?有很多人問說,網站一直被攻擊,什么被掛馬,什么被黑,每天一早打開網站,總是會出現各種各樣的問題,這著實讓站長們揪心。從修改服務器管理賬號開始&…

腳本 api_從腳本到預測API

腳本 apiThis is the continuation of my previous article:這是我上一篇文章的延續: From Jupyter Notebook To Scripts從Jupyter Notebook到腳本 Last time we discussed how to convert Jupyter Notebook to scripts, together with all sorts of basic engine…

Iphone代碼創建視圖

要想以編程的方式創建視圖,需要使用視圖控制器中定義的viewDidLoad方法,只有在運行期間生成UI時才需要實現該方法。 在此只貼出viewDidLoad方法的代碼,因為只需要在這個方法里面編寫代碼: [cpp] view plaincopyprint?- (void)vi…

聊聊flink Table的OrderBy及Limit

序 本文主要研究一下flink Table的OrderBy及Limit 實例 Table in tableEnv.fromDataSet(ds, "a, b, c"); Table result in.orderBy("a.asc");Table in tableEnv.fromDataSet(ds, "a, b, c");// returns the first 5 records from the sorted …

binary masks_Python中的Masks概念

binary masksAll men are sculptors, constantly chipping away the unwanted parts of their lives, trying to create their idea of a masterpiece … Eddie Murphy所有的人都是雕塑家,不斷地消除生活中不必要的部分,試圖建立自己的杰作理念……埃迪墨…

css+沿正方形旋轉,CSS3+SVG+JS 正方形沿著正方本中軸移動翻轉的動畫

CSS語言:CSSSCSS確定* {margin: 0;padding: 0;fill: currentColor;vector-effect: non-scaling-stroke;}html {overflow: hidden;}body {display: flex;flex-direction: column;margin: 0;min-width: 10em;height: 100vh;}body > svg,body > input,body > …

Iphone在ScrollView下點擊TextField使文本筐不被鍵盤遮住

1.拖一個Scroll View視圖填充View窗口&#xff0c;將Scroll View視圖拖大一些&#xff0c;使其超出屏幕。 2.向Scroll View拖&#xff08;添加&#xff09;多個Label視圖和Text View視圖。 3.在.h頭文件中添加如下代碼&#xff1a; [cpp] view plaincopyprint?#import <U…