代码之家  ›  专栏  ›  技术社区  ›  Shiva

数组中的配对号(a,b),使a*2>=b

  •  6
  • Shiva  · 技术社区  · 6 年前

    我试图用这样一种方法来求解数组中的配对数(A,B) a*2 >=b . 其中A和B是来自输入数组的数字。

    实例:

    输入: a[] = {1,2,3,4,5}

    产量:2

    说明:

    • 我们可以把1和3配对
    • 2加4或5

    输入: a[] = {4,3,2,1,5}

    产量:2

    说明:

    • 我们可以把1和3配对
    • 2加4或5

    输入: a[] = {4,3,2,1,5,6}

    产量:3

    说明:

    • 我们可以把5和1配对
    • 2加4
    • 3加6

    我尝试使用如下的递归来解决这个问题,但这并不能给出任何正确的结果。任何帮助都将不胜感激。

    • 对输入数组排序
    • 如果 A[开始]*2>=[结束]找到,然后 add 1 结果重复 start +1 end - 1
    • 其他的 重现 (start + 1, end) , (start, end - 1) (start + 1, end - 1)

    想法是一致的 a[start] 具有 剩下的 数组中的元素并获得最大结果。

        public static int countPairs(int[] a){
           Arrays.sort(a);
           return countPairs(a,a.length,0,a.length-1);
        }
    
        public static int countPairs(int[] a, int n, int start, int end){
    
    
            if(end == start){
                return 0;
            }
            if(start >= n || end < 0){
                return 0;
            }
    
             System.out.print("matching start: "+start + " and end "+end+"   ");System.out.println("matching "+a[start] + " and "+a[end]);
    
            if(a[start] < a[end] && a[start] * 2 >= a[end]  ){
    
                int res = countPairs(a,n,start+1,end-1) +1;
                 //System.out.print("inside if matching start: "+start + " and end "+end+"   ");System.out.println("matching "+a[start] + " and "+a[end] + " count is "+res);
                return res;
            }
            else{
    
                return max(countPairs(a,n,start+1,end) ,
                        countPairs(a,n,start,end - 1),countPairs(a,n,start+1,end - 1));
            }
    
        }
    

    测验:

    import org.junit.Test;
    
    import java.util.Arrays;
    import java.util.Random;
    
    
    public class CountingPairsTest {
    
        static int countPairs(int[] a){
            return PairingNumbers.countPairs(a);
        }
    
        @Test
         public void test1(){
            int[] a = {1,2,3,4,5};
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public void test2(){
            int[] a = {1,2,3,4,5,6};
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public void test5(){
            int[] a = {1,2,3,7,4,5,6};
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public void test6(){
            int[] a = {9,8,20,15,21};
    
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public  void test3(){
            int[] a = {5,4,3,2,1};
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public void test4(){
            int[] a = {2,4,5,3,1};
    
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    
        @Test public void test7(){
            int[] a = new Random().ints(10,1,100).toArray();// IntStream.range(1,100).toArray();
    
    
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
        @Test public void test8(){
            int[] a = new Random().ints(10,1,10).toArray();// IntStream.range(1,100).toArray();
    
    
            System.out.println("****************************************\n" + Arrays.toString(a));
            int count = countPairs(a);
            System.out.println("count "+count);
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  8
  •   Anonymous    6 年前

    我建议答案是 a.length / 2 . 数组长度的一半(如果长度为奇数,则向下取整)。你可以任意配对。非阴性 如果 * 2和lt; 只是交换 你会有 * 2 .因此,由于一对需要两个数字,所以您可以精确地形成与数组长度一半相同的对。

    示例(来自注释):[1,2,2,2]。长度是4,一半长度是2,所以应该有2对。让_s检查一下:(1,2)是一对不错的,因为1*2>=2。(2,2)是另一对漂亮的,因为2*2>=2。在这种情况下,我们甚至不需要任何交换(另一方面,相同的对会起作用) 具有 交换:2*2>=1和2*2>=2)。

    如果数组可能包含负数,则它不会始终工作。因此,您可能需要添加一个数组的验证,以检查它是否不存在。

    你的解决方案出了什么问题?

    您的递归算法错误。假设数组是[2,3,7,9]。显然(2,3)和(7,9)是很好的一对,所以这里有两对。您描述算法的方式,因为(2,9)不是一个有效的对,所以您丢弃了2和9中的至少一个,从而没有机会从剩余的数字中形成两个对。

        2
  •  1
  •   GolamMazid Sajib    6 年前

    你可以这样解决它:

    排序数组。

    二。对于每一个数字 找到最左边的位置 包含>=2*b的数组。然后您可以计算满足的数目。

    复杂性: o(nLogn)+nLogn