CSDN每日练习汇总 2023-9-20 java

CSDN每日练习汇总 2023-9-20 java

题目1名称:最优利润值

时间限制:1000ms 内存限制:256M

题目描述

你在读的经营课程上,老师布置了一道作业。在一家公司的日常运营中,是会对一些商品的价格走势根据一些经验和数据进行预估,并据此进行决策。例如,假设某商品每天的价格都有可能变动,我们要做的就是低买高卖获得最高利润。比如假设我们预估该商品接下来七天内的价格走势如下: 4 1 2 3 6 4 8 那我们采取的最佳策略是在价格1块钱的时候买入,在价格8块钱的时候卖出。为了简化整个过程,我们限定在此周期内只能有一次买入一次卖出,且商品在没有购入前是无法卖出的,即该商品不是期货而是现货。 现要求你用程序来实现自动决策。输入一定天数的商品预估价格,自动计算出最优利润值。例如,上面的例子中,最优利润值为8-1=7。(简单起见,只考虑0-100000之间的整数价格)

输入描述:

输入一个数组,分别表示各天的预估价格

输出描述:

根据输入的预估价格,算出并输出最优的利润值。

示例1

输入
4 1 2 3 6 4 8

输出7

算法思路解析:

  • 为了找到最大的利润,我们可以遍历数组的同时保持一个变量来记录目前为止的最低价格和一个变量来记录目前为止的最高利润。

  • 在每一天,我们可以计算如果今天卖出的利润是多少(当天的价格减去目前为止的最低价格),并更新最高利润。同时,我们也更新目前为止的最低价格。

java代码实现(执行用时141ms,内存消耗25mb):

import java.util.ArrayList;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        String str_0 = scan.nextLine();
        String[] line_list_0 = str_0.trim().split(" ");        
        ArrayList<Integer> arr = new ArrayList<>();
        for(int i = 0; i < line_list_0.length; i++){
            arr.add(Integer.parseInt(line_list_0[i]));
        }

        scan.close();

        int result = solution(arr);

        System.out.println(result);

    }

    public static int solution(ArrayList<Integer> arr){
        if(arr.size() < 2) {
            return 0;
        }

        int minPrice = arr.get(0);
        int maxProfit = 0;

        for(int i = 1; i < arr.size(); i++){
            if(arr.get(i) < minPrice) {
                minPrice = arr.get(i);
            } else if(arr.get(i) - minPrice > maxProfit) {
                maxProfit = arr.get(i) - minPrice;
            }
        }

        return maxProfit;
    }
}

代码解析:

  • 首先检查数组的长度是否小于2,如果是,则返回0,因为我们不能获得任何利润。然后,它初始化最低价格为数组的第一个元素和最高利润为0。接着它遍历数组,更新最低价格和最高利润。最终,它返回最高利润。

题目2名称:非降序数组

时间限制:1000ms **内存限制:256M

题目描述

写一个函数,传入两个非降序的整数数组(A, B),将 A, B 合并成一个非降序数组 C,返回 C(不要使用内置 sort 函数)。(测试用例仅做参考,我们会根据代码质量进行评分)

输入描述:

第一行输入两个整数n,m。(1<=n,m<=100000)分别表示数组A,B的大小。 第二行给出n个整数a。(1<=a<=10000) 第三行给出m个整数b。(1<=b<=10000)

输出描述:

输出合并之后排序好的数组。

示例1

输入
3 3
1 9 10
3 12 41

输出
1 3 9 10 12 41

算法思路解析:

  • 需要实现solution方法来合并两个非降序数组为一个非降序数组。由于输入数组已经是非降序的,可以使用两个指针分别从两个数组的开头开始遍历,然后依次将较小的元素添加到结果数组中。

java代码实现(执行用时450msms,内存消耗40mb):

import java.util.ArrayList;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        String str_0 = scan.nextLine();
        String[] line_list_0 = str_0.trim().split(" ");        
        ArrayList<Integer> arr0 = new ArrayList<>();
        for(int i = 0; i < line_list_0.length; i++){
            arr0.add(Integer.parseInt(line_list_0[i]));
        }
    
        
        String str_1 = scan.nextLine();
        String[] line_list_1 = str_1.trim().split(" ");        
        ArrayList<Integer> arr1 = new ArrayList<>();
        for(int i = 0; i < line_list_1.length; i++){
            arr1.add(Integer.parseInt(line_list_1[i]));
        }
    
        
        String str_2 = scan.nextLine();
        String[] line_list_2 = str_2.trim().split(" ");        
        ArrayList<Integer> arr2 = new ArrayList<>();
        for(int i = 0; i < line_list_2.length; i++){
            arr2.add(Integer.parseInt(line_list_2[i]));
        }
    

        scan.close();

        ArrayList<Integer> result = solution(arr0, arr1, arr2);

        
        for (int i = 0; i < result.size(); i++){
            System.out.print(result.get(i) + " ");
        }
    

    }

    public static ArrayList<Integer> solution(ArrayList<Integer> arr0, ArrayList<Integer> arr1, ArrayList<Integer> arr2){
    ArrayList<Integer> result = new ArrayList<>();
    
    int n = arr0.get(0);
    int m = arr0.get(1);

    int i = 0, j = 0;
    while(i < n && j < m){
        if(arr1.get(i) <= arr2.get(j)){
            result.add(arr1.get(i));
            i++;
        } else {
            result.add(arr2.get(j));
            j++;
        }
    }

    // 如果其中一个数组已完全合并到结果数组中,则将另一个数组的剩余元素全部添加到结果数组中
    while(i < n){
        result.add(arr1.get(i));
        i++;
    }
    
    while(j < m){
        result.add(arr2.get(j));
        j++;
    }
    
    return result;
}
}

代码解析:

  • 首先从两个数组的开始位置进行比较,并将较小的元素添加到结果数组中,然后移动到下一个位置继续比较,直到一个数组完全合并到结果数组中。然后,如果另一个数组还有剩余的元素,它们将被全部添加到结果数组中,因为这些元素一定都是比结果数组中的所有元素大的。

单选题

题目1名称:滑动窗口最大值

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回滑动窗口中的最大值。

示例 1:

**输入:**nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:

滑动窗口的位置最大值
[1 3 -1]3
1 [3 -1 -3]3
1 3 [-1 -3 5]5
1 3 -1 [-3 5 3]5
1 3 -1 -3 [5 3 6]6
1 3 -1 -3 5 [3 6 7]7

示例 2:

**输入:**nums = [1], k = 1
输出:[1]

示例 3:

**输入:**nums = [1,-1], k = 1
输出:[1,-1]

示例 4:

**输入:**nums = [9,11], k = 2
输出:[11]

示例 5:

**输入:**nums = [4,-2], k = 2
输出:[4]

提示:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • 1 <= k <= nums.length

选项:

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k < 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k < 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null & nums.length < 0 & k <= 0 & k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null & nums.length < 0 & k <= 0 & k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k > 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k > 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k <= 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null || nums.length < 0 || k <= 0 || k == 1)
            return nums;
        queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int[] max = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length; i++) {
            if (i < k - 1) {
                queue.add(nums[i]);
            } else if (i == k - 1) {
                queue.add(nums[i]);
                max[0] = queue.peek();
            } else {
                queue.remove(nums[i - k]);
                queue.add(nums[i]);
                max[i - k + 1] = queue.peek();
            }
        }
        return max;
    }
}

选项解析:

  • b选项:条件检查中使用了 || 而不是 &&,导致几乎所有情况都会返回 nums
  • c选项:条件检查使用了 &(位与)而不是逻辑与 &&,并且循环条件是 i >= nums.length,它永远不会进入循环。
  • d选项:与a选项相比,多了一个不必要的条件检查在循环内部。

正确选项 A

题目2名称:青蛙过河

一只青蛙想要过河。 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有)。 青蛙可以跳上石子,但是不可以跳入水中。

给你石子的位置列表 stones(用单元格序号 升序 表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一块石子上)。

开始时, 青蛙默认已站在第一块石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格 1 跳至单元格 2 )。

如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1 个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。

示例 1:

输入:stones = [0,1,3,5,6,8,12,17]
输出:true

解释:
青蛙可以成功过河,按照如下方案跳跃:跳 1 个单位到第 2 块石子, 然后跳 2 个单位到第 3 块石子, 接着 跳 2 个单位到第 4 块石子, 然后跳 3 个单位到第 6 块石子, 跳 4 个单位到第 7 块石子, 最后,跳 5 个单位到第 8 个石子(即最后一块石子)。

示例 2:

输入:stones = [0,1,2,3,4,8,9,11]
输出:false

解释:

这是因为第 5 和第 6 个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。

提示:

  • 2 <= stones.length <= 2000
  • 0 <= stones[i] <= 231> -1
  • stones[0] == 0

选项:

class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i < stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (stones[i] > stones[i - 1] + i)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i < stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (stones[i] > stones[i - 1] + i)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i == stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i == stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i < stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i < stones.length; i++)
            if (stones[i] > stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i == stones.length; i++)
            if (stones[i] >= stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}
class Solution {
    public boolean canCross(int[] stones) {
        for (int i = 1; i == stones.length; i++)
            if (stones[i] >= stones[i - 1] + i)
                return false;
        return dfs(stones, 0, 1);
    }
    public boolean dfs(int[] stones, int index, int v) {
        if (index < 0 || v < 1)
            return false;
        return index == stones.length - 1
                || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) ||
                dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1);
    }
}

选项解析:

  • A选项: 在for循环中使用了 i == stones.length 而不是 i < stones.length,这导致循环体内的代码永远不会执行。此外,dfs 函数中的一些条件判断也有问题,比如 index < 0 && v < 1 永远不会为真,因为v的初始值为1。

  • B选项: 同样在for循环中使用了 i == stones.length,而且有数组越界的风险,因为在循环内部尝试访问 stones[i + 1]

  • C选项: 此选项首先检查每一步的距离是否超过了青蛙当前能够跳跃的最大距离。如果没有,则使用深度优先搜索来尝试找到一条通往终点的路径。这是一个有效的方法来解决这个问题。

正确选项C

题目3名称:阶乘后的零

给定一个整数 n ,返回 n! 结果中尾随零的数量。

提示:

n! = n * (n - 1) * (n - 2) * … * 3 * 2 * 1

示例 1:

输入:n = 3
输出:0
解释:

3! = 6 ,不含尾随 0

示例 2:

输入:n = 5
输出:1
解释:

5! = 120 ,有一个尾随 0

示例 3:

输入:n = 0
输出:0

提示:

0 <= n <= 104

选项:

// a选项

class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        while (n < 5) {
            count += n / 5;
            n /= 5;
        }
        return count;
    }
}

// b选项

class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        while (n == 5) {
            count += n / 5;
            n /= 5;
        }
        return count;
    }
}
// c选项

class Solution {
    public int trailingZeroes(int n) {
        int count = 0;
        while (n > 5) {
            count += n / 5;
            n /= 5;
        }
        return count;
    }
}
// d选项 以上都不对

选项解析:

  • a选项: 循环条件是 n < 5,这意味着如果n大于或等于5,循环体内的代码不会执行,这不是我们想要的。
  • b选项: 循环条件是 n == 5,这将只在n等于5时执行循环体内的代码,这也不是我们想要的。

正确选项C


解题思路多样,以上仅为个人做法,有更好的算法也可以分享一下。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evan_422

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值