日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数论入门符号_大符号入门指南第2部分

發(fā)布時(shí)間:2023/12/31 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数论入门符号_大符号入门指南第2部分 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)論入門(mén)符號(hào)

Well hey there, Chef Remy! Welcome back to the kitchen!

好吧,雷米大廚! 歡迎回到廚房!

Today we’re going to complete our intro to Big O Notation with O(log n), O(n log n), O(2^n) and O(n!) via an impeccable film, Pixar’s Ratatouille. When we last left off, we were preparing our famous ratatouille dish for notorious restaurant critic Anton Ego who was waiting patiently for his meal.

今天,我們將通過(guò)無(wú)可挑剔的電影皮克斯的《料理鼠王》完成關(guān)于O大符號(hào)的介紹,其中包括O(log n),O(n log n),O(2 ^ n)和O(n!)。 當(dāng)我們最后一次離開(kāi)時(shí),我們正在為臭名昭著的餐廳評(píng)論家安東·埃戈(Anton Ego)準(zhǔn)備我們著名的料理鼠王料理,他耐心地等待著他的飯菜。

As a refresher, check out the time complexity comparison chart below:

復(fù)習(xí)一下下面的時(shí)間復(fù)雜度比較表:

O(log n)記錄時(shí)間 (O(log n) Log Time)

Let’s say our waiter received an order from a mysteriously cloaked man (Chef Skinner in disguise! 🕵🏻?♂?) who said “I’ll have what he’s having” in reference to Anton Ego.

假設(shè)我們的服務(wù)員收到了一個(gè)披著神秘衣服的男人的命令(偽裝成廚師斯金納!! ?),他說(shuō)“我將擁有他所擁有的”。

We know that Anton Ego is seated at table 29, so we will need to look up his food order through his table number. Lucky for us, the table numbers are always kept sorted in our array of orders. This is very helpful, because the restaurant is busy and we can’t keep the customers waiting! We need a quick way to figure out what Anton Ego ordered so that we can double it for the mysterious gentleman customer (coughSkinnercough):

我們知道Anton Ego坐在表29上,因此我們將需要通過(guò)他的表號(hào)查找他的食物訂單。 對(duì)我們來(lái)說(shuō)幸運(yùn)的是,表號(hào)始終按照我們的orders數(shù)組進(jìn)行排序。 這非常有幫助,因?yàn)椴蛷d很忙,我們不能讓顧客等著! 我們需要一種快速的方法來(lái)弄清楚Anton Ego訂購(gòu)了什么,以便為神秘的紳士客戶( coughSkinnercough )加倍購(gòu)買(mǎi):

Since the orders are sorted by table number, we can use binary search to cut our search time down. In this method, we take the middle order in the orders array and check for three things:

由于訂單是按table號(hào)排序的,因此我們可以使用二進(jìn)制搜索來(lái)減少搜索時(shí)間。 在這種方法中,我們將orders數(shù)組中的中間訂單作為對(duì)象并檢查三件事:

  • Does this table match the tableNum we’re looking for? If so, great! 🎉Return that order object. If not:

    該table是否與我們要查找的tableNum匹配? 如果是這樣,那就太好了! 🎉返回該訂單對(duì)象。 如果不:

  • Is this table less than the tableNum that we’re looking for? If so, eliminate the first half of the array. If not:

    該table是否小于我們要查找的tableNum ? 如果是這樣,則消除陣列的前半部分。 如果不:

  • Is this table greater than the tableNum that we’re looking for? If so, eliminate the second half of the array.

    該table是否大于我們要查找的tableNum ? 如果是這樣,請(qǐng)消除陣列的后半部分。

We repeat this process until we find the order object we’re looking for. Since we continuously eliminate half of the array with each recursive step, we say that the time complexity is O(log n) or log time.

我們重復(fù)此過(guò)程,直到找到所需的訂單對(duì)象。 由于我們?cè)诿總€(gè)遞歸步驟中都不斷消除數(shù)組的一半,因此我們說(shuō)時(shí)間復(fù)雜度為O(log n)或log time 。

This refers to a very math-y term, logarithm, which in Big O Notation looks at the question, “How many 2’s do we multiply together to get n?” If n was equal to 8, it would take 3 operations because 2 * 2 * 2 = 8. If n was 100, it would take 7 operations, and if n was 4,000,000,000 it would take only 32!(Grokking Algorithms)

這指的是一個(gè)非常數(shù)學(xué)的對(duì)數(shù)項(xiàng),以Big O表示法著眼于以下問(wèn)題:“我們將多少個(gè)2相乘以得到n ?” 如果n等于8,則將進(jìn)行3次運(yùn)算,因?yàn)? * 2 * 2 =8。如果n為100,則將進(jìn)行7次運(yùn)算;如果n為4,000,000,000,則將僅進(jìn)行32次運(yùn)算( Grokking算法)。

This time complexity is super optimal, and just above O(1) constant time! If your solution is in O(log n) time, you should be pretty pleased! 🙌

這個(gè)時(shí)間復(fù)雜度是超最佳的,剛好超過(guò)O(1)恒定時(shí)間! 如果您的解決方案是O(log n)時(shí)間,那么您應(yīng)該會(huì)很高興! 🙌

O(n log n)線性時(shí)間 (O(n log n) Linearithmic Time)

Do you remember how in Part 1 of this Big O series, we wanted to sort the chopped vegetable slices in order to grab the best ones for Anton Ego’s meal? Last time we used a nested for loop which resulted in O(n2) time- not very optimal!

您是否還記得在本Big O系列文章的第1部分中,我們?nèi)绾螌⑶兴榈氖卟饲衅诸?#xff0c;以便為Anton Ego的餐點(diǎn)挑選最好的蔬菜切片? 上次我們使用嵌套的for循環(huán)導(dǎo)致O(n2)時(shí)間-并不是非常理想!

Image credit: Pixar圖片來(lái)源:皮克斯

If we wanted to cut that time down (because Anton Ego is waiting and his review will make or break this restaurant!! ?), we could use the mergesort algorithm instead. Let’s pretend that each vegetable slice has a rating from 1–n, with 1 being the best slice and n being the worst:

如果我們想減少時(shí)間(因?yàn)锳nton Ego正在等待,并且他的評(píng)論將決定這家餐廳的成立與否! ? ),我們可以改用mergesort算法。 假設(shè)每個(gè)蔬菜切片的等級(jí)為1–n,其中1為最佳切片,n為最差切片:

let zucchiniSlices = [20, 11, 25, 3, 14, 5,...]const merge = (arr1, arr2) => {let sorted = [];while (arr1.length && arr2.length) {if (arr1[0] < arr2[0]) {sorted.push(arr1.shift());} else { sorted.push(arr2.shift());}};return sorted.concat(arr1.slice().concat(arr2.slice())); };const mergeSortByBestSlices = veggieSlices => {if (veggieSlices.length <= 1) return veggieSlices;let mid = Math.floor(veggieSlices.length / 2),left = mergeSortByBestSlices(veggieSlices.slice(0, mid)),right = mergeSortByBestSlices(veggieSlices.slice(mid));return merge(left, right); };mergeSortByBestSlices(zucchiniSlices) // [1, 2, 3, 4, 5, 6,...]

Mergesort uses a ‘divide and conquer’ approach (similar to binary search), taking an array and breaking it down continuously until each element becomes its own individual sub-array. Then it sorts those sub-arrays in pairs recursively until the entire array is sorted and joined back together again. This article does a great job of explaining the algorithm in further detail.

Mergesort使用“分而治之”的方法(類似于二進(jìn)制搜索),獲取一個(gè)數(shù)組并將其連續(xù)分解,直到每個(gè)元素成為自己的單獨(dú)子數(shù)組為止。 然后,它將這些子數(shù)組成對(duì)遞歸地排序,直到整個(gè)數(shù)組被排序并再次結(jié)合在一起。 本文在進(jìn)一步詳細(xì)解釋該算法方面做得很好。

With this approach, the time complexity is O(n log n) because of the 2 main steps involved. The breakdown of the original zucchiniSlices array happens in O(log n) time. Building it back together through sorting requires O(n) time since each element (zucchini slice) must be considered, as well as O(log n) time to merge each sub-array back together. It’s like the equivalent of saying n * log n and it’s also known as linearithmic time.

使用這種方法,由于涉及兩個(gè)主要步驟,因此時(shí)間復(fù)雜度為O(n log n) 。 原始zucchiniSlices數(shù)組的分解發(fā)生在O(log n)時(shí)間。 通過(guò)排序?qū)⑵渲匦聵?gòu)建在一起需要O(n)時(shí)間,因?yàn)楸仨毧紤]每個(gè)元素(西葫蘆切片),還需要O(log n)時(shí)間來(lái)將每個(gè)子數(shù)組合并在一起。 這就像說(shuō)n * log n ,也稱為線性時(shí)間。

Since this sort method and time complexity is more optimal than O(n2), we can get those vegetables into the oven and onto Anton Ego’s plate much faster! 👍

由于這種排序方法和時(shí)間復(fù)雜度比O(n2)最佳,因此我們可以更快地將那些蔬菜放入烤箱并放在Anton Ego的盤(pán)子上! 👍

O(2 ^ n)指數(shù)時(shí)間 (O(2^n) Exponential Time)

The ratatouille is almost ready to be served, but it feels like it’s missing… something. As Chef Remy, you look around and think of 3 herbs that might be a nice finishing touch to add on top: thyme, rosemary and/or marjoram 🌱.

料理鼠王幾乎已經(jīng)準(zhǔn)備好送達(dá)了,但感覺(jué)好像缺少了……某物。 作為廚師雷米(Remy),您環(huán)顧四周,想到3種草藥,它們可能是添加在頂部的一種不錯(cuò)的點(diǎn)睛之筆:百里香,迷迭香和/或馬郁蘭🌱。

Thinking through the different combinations of herbs would yield O(2^n) possibilities:

通過(guò)對(duì)草藥的不同組合進(jìn)行思考將產(chǎn)生O(2 ^ n)可能性:

// with just 1 herb there are 2 options:
[""] or ["thyme"]// with 2 herbs there are 4 options:
[""] or ["thyme"] or ["rosemary"] or ["thyme", "rosemary"]// with 3 herbs there are 8 options:
[""] or ["thyme"] or ["rosemary"] or ["marjoram"] or
["thyme", "rosemary"] or ["thyme", "marjoram"] or ["rosemary", "marjoram"] or ["thyme", "rosemary", "marjoram"]

As we can see, the number of options grow in proportion to 2^n:

如我們所見(jiàn),選項(xiàng)的數(shù)量與2 ^ n成正比:

  • When n = 1, we have 2^1 possibilities.

    當(dāng)n = 1時(shí),我們有2 ^ 1個(gè)可能性。
  • When n = 2, we have 2^2 possibilities.

    當(dāng)n = 2時(shí),我們有2 ^ 2的可能性。
  • When n = 3, we have 2^3 possibilities, and so on.

    當(dāng)n = 3時(shí),我們有2 ^ 3個(gè)可能性,依此類推。

This is called a power set, where the goal is to find all possible subsets of the options given, and the time complexity can be costly. Luckily for Anton Ego, you instinctively know exactly what should be added (a sprig of rosemary of course! 👌) and won’t need to think about every combination. The dish is finally complete!

這稱為功率集,其目標(biāo)是找到給定選項(xiàng)的所有可能子集,并且時(shí)間復(fù)雜度可能很高。 幸運(yùn)的是,對(duì)于Anton Ego,您本能地確切知道應(yīng)該添加什么(當(dāng)然是一小撮迷迭香!👌),并且無(wú)需考慮每種組合。 這道菜終于完成了!

Voila!瞧!

In general, you should do your best to avoid O(2^n) or exponential run times as they can blow up very quickly. This one is actually one of the worst time complexities, second only to O(n!).

通常,您應(yīng)盡力避免O(2 ^ n)或指數(shù)運(yùn)行時(shí)間,因?yàn)樗鼈儠?huì)很快爆炸。 實(shí)際上,這是最復(fù)雜的時(shí)間之一,僅次于O(n!)。

O(n!)階乘時(shí)間 (O(n!) Factorial Time)

Being that tonight is the grand re-opening of our restaurant, the tables are packed and the servers are very busy running around to attend to each one!

今晚正是我們餐廳的隆重開(kāi)幕,桌子已經(jīng)排滿,服務(wù)器非常忙碌,到處跑來(lái)跑去!

Image credit: Pixar圖片來(lái)源:皮克斯

Since time is of the essence and people are hungry, our waiter Linguini would like to calculate the best route between the tables so that he’ll walk (or skate) the minimum distance between each table. As it turns out, there are a LOT of possible routes between tables, even if there are only a few!

由于時(shí)間至關(guān)重要,而且人們很餓,因此我們的服務(wù)員Linguini希望計(jì)算桌子之間的最佳路線,以便他步行(或滑冰)每個(gè)桌子之間的最小距離。 事實(shí)證明,即使只有幾個(gè)表,表之間也有很多可能的路由!

Possible serving routes between tables表之間可能的服務(wù)路線

If Linguini is assigned to 5 tables, in order to examine each possible route you would need to do (5 * 4 * 3 * 2 * 1) permutations, which comes out to 120 in total. (Grokking Algorithms) If you were to add one more table to make 6, that would increase to 720 operations (6 * 120). If you add just one more to make 7 tables, that number goes up to 5,040, and if you somehow had 15 tables… that comes out to 1,307,674,368,000 operations! Yikes! 😱

如果將Linguini分配給5個(gè)表,則為了檢查每條可能的路線,您需要進(jìn)行(5 * 4 * 3 * 2 * 1)排列,總計(jì)為120。 ( Grokking算法)如果要再增加一張表來(lái)制作6,那將增加到720次運(yùn)算(6 * 120)。 如果僅增加一個(gè)就可以創(chuàng)建7個(gè)表,那么這個(gè)數(shù)字將達(dá)到5,040,如果您以某種方式擁有15個(gè)表,那么結(jié)果就是1,307,674,368,000個(gè)操作! kes! 😱

This is the absolute worst of the Big O Notation values, known as O(n!) or factorial time. This time complexity should be avoided at all costs, and luckily it’s pretty rare in our day-to-day algorithms.

這是Big O表示法值的絕對(duì)最差值,稱為O(n!)或階乘時(shí)間。 應(yīng)該不惜一切代價(jià)避免這種時(shí)間復(fù)雜性,幸運(yùn)的是,在我們的日常算法中,這種情況非常罕見(jiàn)。

And there you have it! The culmination of our 2-part beginner’s guide to Big O Notation. (Feel free to check out part 1 here, covering O(1), O(n) and O(n2)!)

在那里,您擁有了! 我們的兩部分“大O符號(hào)”初學(xué)者指南達(dá)到了頂峰。 (在此處隨意檢查第1部分,涵蓋O(1),O(n)和O(n2)!)

So how did it go with the ratatouille? Seems as if they liked it. 😊

那么料理鼠王又如何呢? 好像他們喜歡它。 😊

Image credit: Pixar圖片來(lái)源:皮克斯

翻譯自: https://medium.com/swlh/a-beginners-guide-to-big-o-notation-part-2-c4ede76cea36

數(shù)論入門(mén)符號(hào)

總結(jié)

以上是生活随笔為你收集整理的数论入门符号_大符号入门指南第2部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。