簡體   English   中英

用n表示具有整體A i,j的nxn數組:= sin(zi,j)其中zi,j是(0,2pi]中均勻分布的隨機數

[英]Denote by n the nxn array having entires A i,j := sin(z i, j) where z i, j are uniformly distributed random numbers in (0,2pi]

在創建具有特定數字值的數組時,我發現了很多幫助,但似乎找不到任何幫助我在第一個或第二個問題中設置數組的東西。

我並不是要問這個作業的答案,這只是我的第一個Python作業,所以我是一個初學者,由於沒有數字,所以無法弄清楚如何設置所需的數組。

到目前為止,我發現這可以創建一個空數組:

import itertools
import numpy as np
my_array = np.empty([n, n])

然后將坐標i,j的值設置為f(i,j)。

for i, j in itertools.product(range(n), range(n)):
     my_array[i, j] = f(i, j)

我只是似乎無法弄清楚如何將此代碼實際應用於我的問題。 sin(z)是我的f(i,j)嗎?

是的,sin(z i,j )將是您的f(i,j)。 但是,不使用循環可能更有效:

np.sin((2 * np.pi) * (1 - np.random.random_sample((n, n))))

檢查三元組數量的最佳方法(小於 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.

相關問題 Python3:將 2d [i,j] (1000x1000) 數組重塑為 ijz 數組 (1000x1000,3) [i,j,z] 在i和j python之間生成n個隨機數 以數字形式查找數組的索引(i,j 到 ij) 對於用戶輸入 n,並且 1&lt;=i 檢查三元組數量的最佳方法(小於 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]> 打印 x 的最大值,其中 x = |(A[i] – A[j]) + (i – j)| 神經網絡可以學習| sin(x)| 為[0,pi]而不是[0,2pi]或[0,4pi] 是否有更短的方法來找到答案,其中m [i,j] = m [i-1,j] + m [i,j-1] + 1的m [i,j]值? 查找對 (i,j) 的數量,使得 i &lt; j 且 A[i] &gt;= 2A[j] 一種算法,用於找到 n 個整數的排列,使得對於任意兩個數字 a[i] 和 a[j] (i &lt; j),它們的平均值不在它們之間
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM