最大和配对数组列表使用堆

huangapple go评论76阅读模式
英文:

Maximum Sum Pairs Array List using heap

问题

import java.io.*;
import java.util.*;

public class Source {

    public static void main(String args[]) {
        // 用于存储给定输入的两个 ArrayList
        ArrayList<Integer> ArrayL1 = new ArrayList<Integer>();
        ArrayList<Integer> ArrayL2 = new ArrayList<Integer>();

        Scanner in = new Scanner(System.in);
        int n, i;

        // ArrayL1 和 ArrayL2 的大小均为 n
        n = in.nextInt();

        for (i = 0; i < n; i++) {
            ArrayL1.add(in.nextInt());
        }
        for (i = 0; i < n; i++) {
            ArrayL2.add(in.nextInt());
        }
        KMaxCombinations(ArrayL1, ArrayL2);
    }

    static void KMaxCombinations(ArrayList<Integer> ArrayL1, ArrayList<Integer> ArrayL2) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
        int N = ArrayL1.size();

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                pq.add(ArrayL1.get(i) + ArrayL2.get(j));

        int count = 0;

        while (count < N) {
            System.out.println(pq.peek());
            pq.remove();
            count++;
        }

    }
}
英文:

I am trying to print the max numbers (of array size) which are formed from the sum of ArrayL1.get(i), ArrayL2.get(j) in the descending order. Somehow my code below is not working.I need to get the the time and space complexity of O(n^2 log n) and O(n) respectively.
For instance:
input
3
1 2 3
4 5 6
output
9
8
8

    import java.io.*; 
import java.util.*; 
public class Source {
public static void main(String args[]) {
//below two ArrayList are used to store the given input
ArrayList&lt;Integer&gt; ArrayL1 = new ArrayList&lt;Integer&gt;();
ArrayList&lt;Integer&gt; ArrayL2 = new ArrayList&lt;Integer&gt;();
Scanner in = new Scanner(System.in);
int n, i;
// size of ArrayL1 = size of ArrayL2 = n
n = in.nextInt();
for (i = 0; i &lt; n; i++) {
ArrayL1.add(in.nextInt());
}
for (i = 0; i &lt; n; i++) {
ArrayL2.add(in.nextInt());
}
KMaxCombinations(ArrayL1,ArrayL2);
}
static void KMaxCombinations(ArrayList&lt;Integer&gt; ArrayL1,ArrayList&lt;Integer&gt; ArrayL2)  {
PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;Integer&gt;(Collections.reverseOrder());
int N = ArrayL1.size();
for (int i = 0; i &lt; N; i++) 
for (int j = 0; j &lt; N; j++) 
pq.add(ArrayL1[i] + ArrayL2[j]); 
int count = 0; 
while (count &lt; N) 
{ 
System.out.println(pq.peek()); 
pq.remove(); 
count++; 
} 
}
}

答案1

得分: 1

你的代码中,你尝试使用错误的语法从 ArrayList 中访问元素。意思是你使用了以下语法:

ArrayL1.[i] + ArrayL2.[j]

理想情况下应该是:

ArrayL1.get(i) + ArrayL2.get(j)

我已经更新了 Source.java

import java.io.*;
import java.util.*;

public class Source {

    public static void main(String args[]) {
        // 下面的两个 ArrayList 用于存储给定的输入
        ArrayList<Integer> ArrayL1 = new ArrayList<Integer>();
        ArrayList<Integer> ArrayL2 = new ArrayList<Integer>();

        Scanner in = new Scanner(System.in);
        int n, i;

        // ArrayL1 和 ArrayL2 的大小都为 n
        n = in.nextInt();

        for (i = 0; i < n; i++) {
            ArrayL1.add(in.nextInt());
        }
        for (i = 0; i < n; i++) {
            ArrayL2.add(in.nextInt());
        }
        KMaxCombinations(ArrayL1, ArrayL2);
    }

    static void KMaxCombinations(ArrayList<Integer> ArrayL1,
            ArrayList<Integer> ArrayL2) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(
                Collections.reverseOrder());
        int N = ArrayL1.size();

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                pq.add(ArrayL1.get(i) + ArrayL2.get(j));

        int count = 0;

        while (count < N) {
            System.out.println(pq.peek());
            pq.remove();
            count++;
        }

    }
}

更改了上述语法后,程序输出如下:

3
1 2 3
4 5 6
9
8
8

你还有其他需要吗?

英文:

In your code you are trying to access element from ArrayList using incorrect syntax. Mean you are using below syntax

ArrayL1.[i] + ArrayL2.[j]

Ideally it should be

ArrayL1.get(i) + ArrayL2.get(j)

I have updated Source.java

import java.io.*;
import java.util.*;
public class Source {
public static void main(String args[]) {
// below two ArrayList are used to store the given input
ArrayList&lt;Integer&gt; ArrayL1 = new ArrayList&lt;Integer&gt;();
ArrayList&lt;Integer&gt; ArrayL2 = new ArrayList&lt;Integer&gt;();
Scanner in = new Scanner(System.in);
int n, i;
// size of ArrayL1 = size of ArrayL2 = n
n = in.nextInt();
for (i = 0; i &lt; n; i++) {
ArrayL1.add(in.nextInt());
}
for (i = 0; i &lt; n; i++) {
ArrayL2.add(in.nextInt());
}
KMaxCombinations(ArrayL1, ArrayL2);
}
static void KMaxCombinations(ArrayList&lt;Integer&gt; ArrayL1,
ArrayList&lt;Integer&gt; ArrayL2) {
PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;Integer&gt;(
Collections.reverseOrder());
int N = ArrayL1.size();
for (int i = 0; i &lt; N; i++)
for (int j = 0; j &lt; N; j++)
pq.add(ArrayL1.get(i) + ArrayL2.get(j));
int count = 0;
while (count &lt; N) {
System.out.println(pq.peek());
pq.remove();
count++;
}
}
}

After changing the above syntax, program is giving

Output:

3
1 2 3
4 5 6
9
8
8

Are you expecting anything else?

huangapple
  • 本文由 发表于 2020年9月26日 01:39:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定