21 NİSAN 2010, ÇARŞAMBA
Sayı dizisi verilmiş, diğer tüm sayılar ürünler (bölüm)dizisi döndürür
İş görüşmesinde bu soru soruldu, ve diğerleri çözmek için nasıl bilmek istiyorum. Java ile en rahat değilim, ama diğer dillerde çözümler bekliyoruz.
Sayı dizisi verildiğinde,
nums
, sayı dizisiproducts[i]
nums[j], j != i
tüm ürün neredeproducts
, dönüş.Input : [1, 2, 3, 4, 5] Output: [(2
4*5), (1int a[N] // This is the input int products[N]; // Get the products below the current index p=1; for(int i=0;i<N; i) { products[i]=p; p*=a[i]; } // Get the products above the curent index p=1; for(int i=N-1;i>=0;--i) { products[i]*=p; p*=a[i]; }
4*5), (1int a[N] // This is the input int products[N]; // Get the products below the current index p=1; for(int i=0;i<N; i) { products[i]=p; p*=a[i]; } // Get the products above the curent index p=1; for(int i=N-1;i>=0;--i) { products[i]*=p; p*=a[i]; }
4*5), (1int a[N] // This is the input int products_below[N]; p=1; for(int i=0;i<N; i) { products_below[i]=p; p*=a[i]; } int products_above[N]; p=1; for(int i=N-1;i>=0;--i) { products_above[i]=p; p*=a[i]; } int products[N]; // This is the result for(int i=0;i<N; i) { products[i]=products_below[i]*products_above[i]; }
3*5), (1int a[N] // This is the input int products_below[N]; p=1; for(int i=0;i<N; i) { products_below[i]=p; p*=a[i]; } int products_above[N]; p=1; for(int i=N-1;i>=0;--i) { products_above[i]=p; p*=a[i]; } int products[N]; // This is the result for(int i=0;i<N; i) { products[i]=products_below[i]*products_above[i]; }
3*4)] = [120, 60, 40, 30, 24]int a[N] // This is the input int products_below[N]; p=1; for(int i=0;i<N; i) { products_below[i]=p; p*=a[i]; } int products_above[N]; p=1; for(int i=N-1;i>=0;--i) { products_above[i]=p; p*=a[i]; } int products[N]; // This is the result for(int i=0;i<N; i) { products[i]=products_below[i]*products_above[i]; }
Bölümü kullanmadan
O(N)
bunu yapmak gerekir.
CEVAP
21 NİSAN 2010, ÇARŞAMBA
polygenelubricants yöntem açıklaması: Hile diziler (4 element için) oluşturmaktır
{ 1, a[0], a[0]*a[1], a[0]*a[1]*a[2], }
{ a[1]*a[2]*a[3], a[2]*a[3], a[3], 1, }
Sol ve sağ kenarlarına sırasıyla başlatarak O(n) yapılabilir.
Sonra iki dizi öğe öğesi çarparak istenen sonucu verir
Benim kod şöyle görünecektir
int a[N] // This is the input
int products_below[N];
p=1;
for(int i=0;i<N; i) {
products_below[i]=p;
p*=a[i];
}
int products_above[N];
p=1;
for(int i=N-1;i>=0;--i) {
products_above[i]=p;
p*=a[i];
}
int products[N]; // This is the result
for(int i=0;i<N; i) {
products[i]=products_below[i]*products_above[i];
}
Eğer O(1) uzayda olmak gerekirse bu kadar net IMHO) yapabilirsiniz
int a[N] // This is the input
int products[N];
// Get the products below the current index
p=1;
for(int i=0;i<N; i) {
products[i]=p;
p*=a[i];
}
// Get the products above the curent index
p=1;
for(int i=N-1;i>=0;--i) {
products[i]*=p;
p*=a[i];
}
Bunu Paylaş:
Nasıl sayı dizisi toplamını bulmak içi...
Nasıl Ruby sayı dizisi toplanacak?...
Nasıl üretmek için bash adım n? (artış...
1 milyar sayı dizisi 100 büyük sayılar...
En hızlı sayı dizisi eksik sayısını bu...