滴滴2017校园招聘笔试题

这里写图片描述

/**
 * 以第i个元素结尾且和最大的连续子数组实际上,
 * 要么是以第i-1个元素结尾且和最大的连续子数组加上这个元素,
 * 要么是只包含第i个元素,
 * sum[i] = max(sum[i-1] + a[i], a[i])
 */
public class Main {

    private static long solve(int[] arr, int n) {
        long maxSum = arr[0];
        long sum = arr[0];
        boolean flag = false;
        for (int i = 0; i < n; i++) {
            if (arr[i] < 0) {
                maxSum = Math.max(maxSum,arr[i]);
            }else {
                flag = true; //数组不全为负数
                break;
            }
        }
        if (!flag) {
            return maxSum;
        }
        maxSum = arr[0];
        for (int i = 1; i < n; i++) {
            if (sum > 0) {
                sum += arr[i];
            }else {
                sum = arr[i];
            }
            maxSum = Math.max(maxSum,sum);
        }
        return maxSum;
    }

    public static void main(String[] arg) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        System.out.println(solve(arr,n));
        scan.close();
    }

}

这里写图片描述

这里写图片描述

/**
 * 餐桌容纳人数:从小到大排序
 * 客人:按消费金额从大到小排序
 * 遍历餐桌:每一桌选择消费金额最大且人数小于餐桌容纳人数
 * ps:未在OJ测试,不知是否能AC
 */
public class Main {

    static class Customer {
        int b;
        int c;
    }

    private static int getCost(Customer[] customers,boolean[] flag,int m,int x) {
        for (int i = 0; i < m; i++) {
            Customer customer = customers[i];
            if (!flag[i] && customer.b < x) {
                flag[i] = true;
                return customer.c;
            }
        }
        return 0;
    }

    private static long solve(Customer[] customers,int[] arr,int n,int m) {
        //餐桌容纳人数从小到大排序
        Arrays.sort(arr);
        //消费金额从大到小排序
        Arrays.sort(customers, new Comparator<Customer>() {
            @Override
            public int compare(Customer c1, Customer c2) {
                return c2.c - c1.c;
            }
        });
        long ans = 0;
        boolean[] flag = new boolean[m];
        for (int i = 0; i < n; i++) {
            ans += getCost(customers,flag,m,arr[i]);
        }
        return ans;
    }

    public static void main(String[] arg) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[] arr = new int[n];
        Customer[] customers = new Customer[m];
        for (int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        for (int i = 0; i < m; i++) {
            Customer c = new Customer();
            c.b = scan.nextInt();
            c.c = scan.nextInt();
            customers[i] = c;
        }
        System.out.println(solve(customers,arr,n,m));
        scan.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值