簡體   English   中英

給定n個數字的排列數組,計算編號。 元組 (i,j,k,l,m) 使得 i <j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l]< div><div id="text_translate"><p> 對於軟件開發人員面試的硬算法回合,我最近被問到這個問題。 我能夠使用蠻力/遞歸來做到這一點,不確定預期的方法,對此有什么幫助嗎?</p><blockquote><p> 給定n數字的排列數組,計算元組數(i,j,k,l,m)使得i&lt;j&lt;k&lt;l&lt;m和a[i]&lt;a[k]&lt;a[j]&lt;a[m]&lt;a[l] 。</p></blockquote><p> 我的方法-&gt; 使用遞歸從給定數組中嘗試 (i,j,k,l,m) 的所有元組,並僅考慮滿足條件的元組,例如 N 選擇 5,因為這具有指數時間復雜性,我想讓它成為三次方或二次方</p></div></j<k<l<m>

[英]Given an array of permutation of n numbers calculate the no. of tuples (i,j,k,l,m) such that i<j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l]

對於軟件開發人員面試的硬算法回合,我最近被問到這個問題。 我能夠使用蠻力/遞歸來做到這一點,不確定預期的方法,對此有什么幫助嗎?

給定n數字的排列數組,計算元組數(i,j,k,l,m)使得i<j<k<l<ma[i]<a[k]<a[j]<a[m]<a[l]

我的方法-> 使用遞歸從給定數組中嘗試 (i,j,k,l,m) 的所有元組,並僅考慮滿足條件的元組,例如 N 選擇 5,因為這具有指數時間復雜性,我想讓它成為三次方或二次方

我會大量使用Quadtrees 之類的東西。 四叉樹允許您將一堆點放在 2d 中,然后有效地搜索區域。 要么找到點,要么只計算它們。 (存儲每個節點中的點數,然后計數會變得更快,因為當您的正方形完全位於所需區域時,您可以停止。)

一個有用的四叉樹是將排列視為一系列 n 點(i, a[i]) 我們可以使用它來查找i在一個范圍內而a[i]在另一個范圍內的所有情況。 叫那棵樹perm

第二個有用的四叉樹是 map 所有反轉i < ja[j] < a[i]到點(i, a[j]) 調用那棵樹invert

現在您的搜索看起來像這樣:

search invert to find all inversions (j, k):
    x = count i < j with a[i] < a[k] (search perm)
    y = count inversions (l, m) with k < l and a[j] < a[m] (search invert)
    add x*y to total

我相信填充perm將花費時間O(n log(n)) ,填充invert將花費O(n^2 log(n)) ,並且每次搜索以獲取計數應該類似於O((log(n))^2) (不確定那個。)由於存在O(n^2)反轉,因此總性能應該類似於O((n log(n))^2)

檢查三元組數量的最佳方法(小於 O(n^2) )其中 a[k] <a[i]<a[j] for all i<j<k in an array< div><div id="text_translate"><p> 我正在解決一個問題,我必須在數組中找到 Ai、Aj 和 Ak 三元組的數量,使得 Ak &lt; Ai &lt; Aj 和 i &lt; j &lt; k。 我知道來自<a href="https://www.geeksforgeeks.org/count-of-triplets-in-an-array-i-j-k-such-that-i-j-k-and-ak-ai-aj/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/count-of-triplets-in-an-array-ijk-such-that-ijk-</a>的時間復雜度 O(n^2) 和 O(n^3) 代碼<a href="https://www.geeksforgeeks.org/count-of-triplets-in-an-array-i-j-k-such-that-i-j-k-and-ak-ai-aj/" rel="nofollow noreferrer">and-ak-ai-aj/</a>來做到這一點... O(n^3) 時間復雜度代碼:</p><pre> def CountTriplets(arr, n): cnt = 0; for i in range(0, n): for j in range(i + 1, n): for k in range(j + 1, n): # If it satisfy the # given conditions if (arr[k] &lt; arr[i] and arr[i] &lt; arr[j]): cnt += 1; # Return the final count return cnt; # Driver Code # Given array arr[] arr = [ 2, 5, 1, 3, 0 ]; n = len(arr); # Function Call print(CountTriplets(arr, n))</pre><p> 和 O(n^2) 時間復雜度代碼:</p><pre> # Function to count triplets def CountTriplets(a, n): # To store count # of total triplets ans = 0 for i in range (n): # Initialize count to zero cnt = 0 for j in range (i + 1, n): # If a[j] &gt; a[i] then, # increment cnt if (a[j] &gt; a[i]): cnt += 1 # If a[j] &lt; a[i], then # it mean we have found a[k] # such that a[k] &lt; a[i] &lt; a[j] else: ans += cnt # Return the final count return ans # Driver code if __name__ == "__main__": arr = [2, 5, 1, 3, 0] n = len(arr) print (CountTriplets(arr, n))</pre><p> 我認為這個問題可以用小於 O(n^2) 的時間復雜度來解決。</p><p> 我看到這個具有挑戰性的問題由 50 人在本地(非英語)網站上以小於 O(n^2) 的時間復雜度解決,但我們看不到解決方案。 </p></div></a[i]<a[j]>

[英]Optimal way ( less than O(n^2) ) to check for number of triplets where a[k]<a[i]<a[j] for all i<j<k in an array

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 大小為 M 的 N 個字符串與大小為 L 的 K 個子字符串的比較 檢查整數數組是否包含整數a [i],以便a [i] = a [j] + a [k]其中j,k 找到所有這些不同元組的計數,(i,j,k)其中(i * j)%k == 0 檢查三元組數量的最佳方法(小於 O(n^2) )其中 a[k] <a[i]<a[j] for all i<j<k in an array< div><div id="text_translate"><p> 我正在解決一個問題,我必須在數組中找到 Ai、Aj 和 Ak 三元組的數量,使得 Ak &lt; Ai &lt; Aj 和 i &lt; j &lt; k。 我知道來自<a href="https://www.geeksforgeeks.org/count-of-triplets-in-an-array-i-j-k-such-that-i-j-k-and-ak-ai-aj/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/count-of-triplets-in-an-array-ijk-such-that-ijk-</a>的時間復雜度 O(n^2) 和 O(n^3) 代碼<a href="https://www.geeksforgeeks.org/count-of-triplets-in-an-array-i-j-k-such-that-i-j-k-and-ak-ai-aj/" rel="nofollow noreferrer">and-ak-ai-aj/</a>來做到這一點... O(n^3) 時間復雜度代碼:</p><pre> def CountTriplets(arr, n): cnt = 0; for i in range(0, n): for j in range(i + 1, n): for k in range(j + 1, n): # If it satisfy the # given conditions if (arr[k] &lt; arr[i] and arr[i] &lt; arr[j]): cnt += 1; # Return the final count return cnt; # Driver Code # Given array arr[] arr = [ 2, 5, 1, 3, 0 ]; n = len(arr); # Function Call print(CountTriplets(arr, n))</pre><p> 和 O(n^2) 時間復雜度代碼:</p><pre> # Function to count triplets def CountTriplets(a, n): # To store count # of total triplets ans = 0 for i in range (n): # Initialize count to zero cnt = 0 for j in range (i + 1, n): # If a[j] &gt; a[i] then, # increment cnt if (a[j] &gt; a[i]): cnt += 1 # If a[j] &lt; a[i], then # it mean we have found a[k] # such that a[k] &lt; a[i] &lt; a[j] else: ans += cnt # Return the final count return ans # Driver code if __name__ == "__main__": arr = [2, 5, 1, 3, 0] n = len(arr) print (CountTriplets(arr, n))</pre><p> 我認為這個問題可以用小於 O(n^2) 的時間復雜度來解決。</p><p> 我看到這個具有挑戰性的問題由 50 人在本地(非英語)網站上以小於 O(n^2) 的時間復雜度解決,但我們看不到解決方案。 </p></div></a[i]<a[j]> 給定兩個 32 位數字 N 和 M,以及兩個位位置 i 和 j。 編寫一個方法,將 N 中 i 和 j 之間的所有位設置為 M 找出數組中三元組i,j,k的數量,以使索引為i到j-1的元素的異或等於索引為j到k的元素的異或 嵌套循環的時間復雜度,其中k &lt;j &lt;i &lt;n 從循環索引k,獲得對i,j,其中i <j? 查找將所有K數轉換為[L,R]范圍(即L≤x≤R)所需的操作次數的最佳方法 給定3個排序的數組A,B,C和數字S,找到i,j,k,使得A [i] + B [j] + C [k] = S
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM