如何检查一个列表中是否存在元素小于等于另一个列表中的元素。

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

How to check if there exists an element in a list less than equal to element in another list

问题

static ArrayList<Integer> counting(ArrayList<Integer> A, ArrayList<Integer> B)   
{
     
    ArrayList<Integer> finallist = new ArrayList<>();

    Collections.sort(A); // Sort the array A to improve efficiency

    for (int i : B)
    { 
        int count = 0;
        for (int j : A)
        {
            if (j <= i)
                count++;
            else
                break; // Since the array A is sorted, we can exit the loop early
        }
         finallist.add(count);
    }

    return finallist;
}
英文:

Consider two Array list

a = [1,4,2,4] 

b = [3,5] 

I wanted to return the count of numbers for each number in B less than equal to number in A.

So the answer would be [2,4] since 3 in B has 2 numbers in A that are <= 3 i.e. [1,2]

And 5 in B has 4 numbers in A that are <= 5 . i.e [1,4,2,4]

Note: i tried using 2 loops but testcases time out.

this is the code

static ArrayList&lt;Integer&gt; counting(ArrayList&lt;Integer&gt;A, ArrayList&lt;Integer&gt;B)   
{
     
    ArrayList&lt;Integer&gt; finallist = new ArrayList&lt;&gt;();

    for(int i : B)
    { 
        int count = 0;
        for(int j: A)
        {
            if(j&lt;=i)
                count++;

           
        }
         finallist.add(count);
    }

    return finallist;
}

we were supposed to edit the function and return arraylist

答案1

得分: 1

我理解你的问题是你找不到答案。
希望这可以帮助你。

你的代码在计算函数参数方面存在问题,你应该声明你期望的数据类型。
static ArrayList counting(List A, List B) 这不是一种方式,
相反,你应该写成 static ArrayList counting(ArrayList<Integer> A, ArrayList<Integer> B)

这将解决你的问题。

以下是我的解决方案:

计数函数。

private static ArrayList<Integer> counting(ArrayList<Integer> A, ArrayList<Integer> B) {
    ArrayList<Integer> finallist = new ArrayList<>();

    for (int i : B) {
        int count = 0;
        for (int j : A) {
            if (j <= i) {
                count++;
            }
        }
        finallist.add(count);
    }

    return finallist;
}

从中调用计数的主函数:

ArrayList<Integer> ans = counting(A, B);
for (int i : ans) {
    System.out.print(i + " ");
}

输出:

2 4

这可以解决你遇到的问题。

英文:

What I understood from Your question was You weren't able to find the answer.
Hope this can help you.

Your code had a problem in counting function parameter, you should declare what is the data type that you are expecting.
static ArrayList counting(List A, List B) this isn't a way
instead you should have wrote static ArrayList counting(ArrayList&lt;Integer&gt; A, ArrayList&lt;Integer&gt; B)

and that will fix your problem.

here's my solution:

Counting function.

private static ArrayList counting(ArrayList&lt;Integer&gt;A, ArrayList&lt;Integer&gt;B ){
	ArrayList&lt;Integer&gt; finallist = new ArrayList&lt;&gt;();
	
	for(int i : B) {
		int count = 0;
		for(int j : A) {
			if(j &lt;= i) {
				count++;
			}
		}
		finallist.add(count);
	}
	
	return finallist;
}

Main from where am calling counting:

ArrayList&lt;Integer&gt; ans = counting(A,B);
		for(int i : ans) {
			System.out.print(i+&quot; &quot;);
		}

Output:

2 4 

This could resolve the issue you are facing.

答案2

得分: 1

以下是翻译好的部分:

当我看到 一个数组 并且我需要在其中 查找 某物时,所以这是使用 二分查找 的一个 神奇钥匙。这将给出你 O(n log n) 的时间复杂度。

public static int[] findLessNumbers(int[] a, int[] b) {
    TreeMap<Integer, Integer> numCount = new TreeMap<>();

    for (int aa : a)
        numCount.put(aa, numCount.getOrDefault(aa, 0) + aa);

    int total = 0;

    for (Map.Entry<Integer, Integer> entry : numCount.entrySet())
        numCount.put(entry.getKey(), total += entry.getValue());

    int[] res = new int[b.length];

    for (int i = 0; i < res.length; i++)
        res[i] = Optional.ofNullable(numCount.floorKey(b[i])).orElse(0);

    return res;
}
英文:

When I see an array and I need find smth. in it, so this is a magic key to use binary search. This gives you O(n log n) time complexity.

public static int[] findLessNumbers(int[] a, int[] b) {
    TreeMap&lt;Integer, Integer&gt; numCount = new TreeMap&lt;&gt;();

    for (int aa : a)
        numCount.put(aa, numCount.getOrDefault(aa, 0) + aa);

    int total = 0;

    for (Map.Entry&lt;Integer, Integer&gt; entry : numCount.entrySet())
        numCount.put(entry.getKey(), total += entry.getValue());

    int[] res = new int[b.length];

    for (int i = 0; i &lt; res.length; i++)
        res[i] = Optional.ofNullable(numCount.floorKey(b[i])).orElse(0);

    return res;
}

huangapple
  • 本文由 发表于 2020年9月23日 14:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64021998.html
匿名

发表评论

匿名网友

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

确定