欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-15 16:10:53
...

第一题

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);
    }
}

第二题

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

//动态规划,空间复杂度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;
    }
}

第三题

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

//总食物量除以时间得到速度,之后按每一堆进行速度验证,失败则速度+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;
    }
}

 

相关标签: 笔试