2019招行信用卡中心-IT(开发方向)第一批编程题

第一题

//C左移相当于D右移。暴力搜索,直接过
class Solution {
    public int minChange(String index) {
        int minChangeTimes = 0;
        if (index.indexOf("C") < 0 || index.indexOf("D") < 0) {
            return 0;
        }
        StringBuilder strBuilder = new StringBuilder(index);
        //C左移情况
        int currentIndex = 0;
        String str = strBuilder.toString();
        for (int i = 0; i < str.length(); i++) {
            int firstTime = str.indexOf('C', currentIndex);
            if (firstTime >= 0) {
                if (firstTime != currentIndex) {
                    minChangeTimes += firstTime - currentIndex;
                }
                currentIndex++;
                strBuilder.setCharAt(firstTime, 'D');
                str = strBuilder.toString();
            }
        }

        //D左移情况
        strBuilder = new StringBuilder(index);
        str = strBuilder.toString();
        int tempMinChangeTimes = 0;
        currentIndex = 0;
        for (int i = 0; i < str.length(); i++) {
            int firstTime = str.indexOf('D', currentIndex);
            if (firstTime >= 0) {
                if (firstTime != currentIndex) {
                    tempMinChangeTimes += firstTime - currentIndex;
                }
                currentIndex++;
                strBuilder.setCharAt(firstTime, 'C');
                str = strBuilder.toString();
            }
        }
        return Math.min(minChangeTimes, tempMinChangeTimes);
    }
}

第二题

//动态规划,空间复杂度O(1),时间复杂度O(n)
class Solution {
    public int profit(int[] price) {
        if (price.length == 0) {
            return 0;
        }
        int currentPrice = price[0];//当前价格
        int mostProfit = 0;//当前最高利润
        for (int i = 0; i < price.length; i++) {
            currentPrice = Math.min(currentPrice, price[i]);
            mostProfit = Math.max(mostProfit, price[i] - currentPrice);
        }
        return mostProfit;
    }
}

第三题

//总食物量除以时间得到速度,之后按每一堆进行速度验证,失败则速度+1再次验证,直接暴力过了。
class Solution {
    static int speed = 0;

    public int minSpeed(int[] amount, int time) {
        int sumFood = 0;
        for (int i = 0; i < amount.length; i++) {
            sumFood += amount[i];
        }

        if (sumFood % time == 0) {
            speed = sumFood / time;
        } else {
            speed = sumFood / time + 1;
        }

        if (testSpeed(amount, time)) {
            return speed;
        }
        return speed;
    }

    //检验当前速度是否符合时间要求
    public boolean testSpeed(int[] amount, int time) {
        int sumTime = 0;
        for (int i = 0; i < amount.length; i++) {
            if (amount[i] % speed == 0) {
                sumTime += amount[i] / speed;
            } else {
                sumTime += amount[i] / speed + 1;
            }
        }

        if (sumTime <= time) {
            return true;
        } else {
            speed = speed + 1;
            testSpeed(amount, time);
        }
        return false;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值