JAVA-度小满2019编程题

火车站台

火车站台题目
注意:注意时间复杂度和空间复杂度问题
解法一:数组法,构建data[n][2],笔者在笔试之后重新思考的解法,不过空间占用比较大,没有测试过大数据量的情况

    public static void getMaxValueByArr(){
        Scanner cin = new Scanner(System.in);

        //代表接下来的数据组数
        int n = cin.nextInt();
        //构建一个二维数组存储数据
        int[][] data = new int[n][2];
        for (int i = 0; i < n; i++) {
            data[i][0] = cin.nextInt();
            data[i][1] = cin.nextInt();
        }
        //关闭资源
        cin.close();
        //遍历得到的二维数组得到最大的data[i][1]
        int max = 0;
        for (int i = 0; i < data.length; i++) {
            if(max<data[i][1])
                max = data[i][1];
        }
        //通过最大值构建数组,并进行初始化
        int[] results = new int[max];
        for (int i = 0; i < results.length; i++) {
            results[i] = 0;
        }
        //开始计算每个站点所经过的线路
        for (int i = 0; i < data.length; i++) {
            for (int j = data[i][0]; j < data[i][1]; j++) {
                results[j] = results[j]+1;
            }
        }
        //查找站点和经过的线路
        int count = 0;
        for (int i = 0; i < results.length; i++) {
            if(results[i]>count)
                count = results[i];
        }
        System.out.println(count);
    }

解法二:HashMap,通过hashMap来操作,通过率36%,程序超时。

    public static void getMaxValueByMap() {
        Scanner cin = new Scanner(System.in);
        // 获得输入
        int n = cin.nextInt();
        // 封装数据<站点,线路数>
        HashMap<Integer, Integer> results = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            int startIndex = cin.nextInt();
            int endIndex = cin.nextInt();
            for (int j = startIndex; j < endIndex; j++) {
                int value = results.get(j) == null ? 1 : results.get(j) + 1;
                results.put(j, value);
            }
            System.out.println(results);
        }
        // 找出最大值
        int max = 0;
        for (int i : results.values()) {
            if (i > max)
                max = i;
        }
        System.out.println(max);
        // 关闭资源
        cin.close();
    }

买卖商品

买卖商品题目
解题思路:寻找极小值–寻找极大值–循环直至结束
1、基本想法是在第一个最低价买入,然后在随后的第一个最高价卖出。
2、那么如何求出第一个最低价呢?可以将当前价位紧随其后的值进行比较,直到找出最低价。
3、那么如果求出第一个最低价的随后的第一个最高价呢?从找到的最低价后面的价位开始,寻找需要的第一个最高价
4、得到了第一次需要买入的最低价出售的最高价之后,需要通过做差值得到利润,然后交易次数加2(买和卖)
然后循环2-4步直至遍历完所有数据。

import java.util.Scanner;
public class SecondMain {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] aiList = new int[n];
        for (int i = 0; i < n; i++) {
            aiList[i] = cin.nextInt();
        }
        int prof = 0;//利润
        int count = 0;//交易的次数
        int minPrice = 0;//
        for (int i = 0; i < n; i++) {
            // 找到第一个买入的低价 
            while (i < n - 1 && aiList[i + 1] <= aiList[i])
                i += 1;
            minPrice = aiList[i];

            //从最低价的后一个元素起,开始寻找第一个卖出的高价
            i += 1;
            while (i < n - 1 && aiList[i + 1] >= aiList[i])
                i += 1;
            /*
             * 情况1:但最低价为最后一个元素n-1,if语句不执行
             * 情况2:当最低价为倒数第二个的元素时,那么最高价必定为最后一个.
             * */
            if (i < n) {
                // 找到的高价与找到的低价做差即可
                prof += aiList[i] - minPrice;
                // 交易的次数为偶数,买和卖,土豪不会将神秘石留在手上的
                count += 2;
            }
        }
        System.out.println(prof + " " + count);
        cin.close();
    }
}

猜你喜欢

转载自blog.csdn.net/CTPeng/article/details/82716509