count apples and oranges
/** * count apples and oranges whether land on the inclusive [s,t] * a is apple tree's cordination, b is orange tree's corditation * apples is apple tree's relative cordination ,left relative apple tree is represention by negative value * right relative is represention by positive value * oranges is same as apples * constrant condition : * 1<=s,t,a,b,m,n<=10to5 m is apples' size, n is oranges' size * -10to5<=d<=10to5 d is apples' cordation plus a's cordation and generate out donation distance and compute oranges' d as well * a<s<t<b * @param s * @param t * @param a * @param b * @param apples * @param oranges */ public static void countApplesAndOranges(int s, int t,int a,int b,List<Integer> apples,List<Integer> oranges){ int val=1; for (int i = 0; i < 5; i++) { val*=10; } if (s<1 || s>val){ return; } if (a<1 || a>val){ return; } if (b<1 || a>val){ return; } if (apples.size()<1 || apples.size()>val){ return; } if (oranges.size()<1 || apples.size()>val){ return; } ArrayList<Integer> appleDonationList = new ArrayList<>(); apples.forEach(e->{ int i = a + e; appleDonationList.add(i); }); ArrayList<Integer> orangeDonationList = new ArrayList<>(); oranges.forEach(e->{ int i = b + e; orangeDonationList.add(i); }); int negativeVal = -val; ArrayList<Integer> donationList = new ArrayList<>(); donationList.addAll(appleDonationList); donationList.addAll(orangeDonationList); for (Integer i : donationList) { if (i<negativeVal || i>val){ return; } } if (a>=s){ return; } if (a>=t){ return; } if (a>=b){ return; } if (s<=a){ return; } if (s>=t){ return; } if (s>=b){ return; } if (t<=a){ return; } if (t<=s){ return; } if (t>=b){ return; } if (b<=a){ return; } if (b<=s){ return; } if (b<=t){ return; } int appleCount=0; for (Integer i : appleDonationList) { if (i>=s && i<=t){ appleCount++; } } System.out.println(appleCount); int orangeCount=0; for (Integer i : orangeDonationList) { if (i>=s && i<=t){ orangeCount++; } } System.out.println(orangeCount); return; }#笔试##金融银行面经##牛客创作赏金赛##技术岗笔试题求解#
Java技术 文章被收录于专栏
JavaEE技术 编程开发经验 企业通用技术