英文:
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<Integer> ArrayL1 = new ArrayList<Integer>();
ArrayList<Integer> ArrayL2 = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
int n, i;
// size of ArrayL1 = size of 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[i] + ArrayL2[j]);
int count = 0;
while (count < 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<Integer> ArrayL1 = new ArrayList<Integer>();
ArrayList<Integer> ArrayL2 = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
int n, i;
// size of ArrayL1 = size of 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++;
}
}
}
After changing the above syntax, program is giving
Output:
3
1 2 3
4 5 6
9
8
8
Are you expecting anything else?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论