题目1:

​3033. 修改矩阵​

给你一个下标从 0 开始、大小为 ​​m x n​​​ 的整数矩阵 ​​matrix​​ ,新建一个下标从 0 开始、名为 ​​answer​​​ 的矩阵。使 ​​answer​​​ 与 ​​matrix​​​ 相等,接着将其中每个值为 ​​-1​​ 的元素替换为所在列的 最大 元素。

返回矩阵 ​​answer​​ 。

示例 1:

输入:matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
输出:[[1,2,9],[4,8,6],[7,8,9]]
解释:上图显示了发生替换的元素(蓝色区域)。
- 将单元格 [1][1] 中的值替换为列 1 中的最大值 8 。
- 将单元格 [0][2] 中的值替换为列 2 中的最大值 9 。

示例 2:

输入:matrix = [[3,-1],[5,2]]
输出:[[3,2],[5,2]]
解释:上图显示了发生替换的元素(蓝色区域)。

提示:

  • ​m == matrix.length​
  • ​n == matrix[i].length​
  • ​2 <= m, n <= 50​
  • ​-1 <= matrix[i][j] <= 100​
  • 测试用例中生成的输入满足每列至少包含一个非负整数。

题解:

使用列遍历,先扫一遍找到每一列的最大值,再扫一遍把 −1 替换成这一列的最大值即可

go代码:

func modifiedMatrix(matrix [][]int) [][]int {
    n := len(matrix)
    m := len(matrix[0])

    for j :=0;j<m; j++ {
        maxColumn := getMaxColumn(matrix, j, n)

        for i :=0;i<n;i++ {
            if(matrix[i][j] == -1) {
                matrix[i][j] = maxColumn
            }
        }
    }

    return matrix
}

func getMaxColumn(matrix [][]int, j, n int) int {
     zd := matrix[0][j]
    for i:=0;i<n;i++ {
        zd = max(zd, matrix[i][j])
    }
    return zd
}

java代码:

class Solution {
    public int[][] modifiedMatrix(int[][] matrix) {
        int n = matrix.length;
        int m = matrix[0].length;

        
        for(int j=0; j<m;j++) {
            int columnMax = getColumnMax(matrix, j, n);
            
            for(int i=0;i<n;i++) {
                if(matrix[i][j]==-1) {
                    matrix[i][j] = columnMax;
                }
            }
        }

        return matrix;
    }

    public int getColumnMax(int[][] matrix, int j, int n) {
        int max = matrix[0][j];
        for(int i=0;i<n;i++) {
            max = Math.max(max, matrix[i][j]);
        }
        return max;
    }
}

题目2:

​3046. 分割数组​

给你一个长度为 偶数 的整数数组 ​​nums​​​ 。你需要将这个数组分割成 ​​nums1​​​ 和 ​​nums2​​ 两部分,要求:

  • ​nums1.length == nums2.length == nums.length / 2​​ 。
  • ​nums1​​ 应包含 互不相同 的元素。
  • ​nums2​​也应包含 互不相同 的元素。

如果能够分割数组就返回 ​​true​​ ,否则返回 ​​false​​ 。

示例 1:

输入:nums = [1,1,2,2,3,4]
输出:true
解释:分割 nums 的可行方案之一是 nums1 = [1,2,3] 和 nums2 = [1,2,4] 。

示例 2:

输入:nums = [1,1,1,1]
输出:false
解释:分割 nums 的唯一可行方案是 nums1 = [1,1] 和 nums2 = [1,1] 。但 nums1 和 nums2 都不是由互不相同的元素构成。因此,返回 false 。

提示:

  • ​1 <= nums.length <= 100​
  • ​nums.length % 2 == 0​
  • ​1 <= nums[i] <= 100​

题解:

go代码:

func isPossibleToSplit(nums []int) bool {
    count := make(map[int]int)
    for i :=0; i<len(nums); i++ {
        count[nums[i]]++

        if(count[nums[i]]>2) {
            return false
        }
    }
    return true
}

java代码:


// 方法1:先排序,然后看有没有2个以上相等的元素
// class Solution {
//     public boolean isPossibleToSplit(int[] nums) {
//         if(nums.length<=2) {
//             return true;
//         }
//         Arrays.sort(nums);
//         for(int i=2; i<nums.length; i++) {
//             if(nums[i] == nums[i-2]) {
//                 return false;
//             }
//         }
//         return true;
//     }
// }


// 方法2:用哈希表统计每个元素的出现次数,看有没有2个以上相等的元素
class Solution {
    public boolean isPossibleToSplit(int[] nums) {
        Map<Integer, Integer> count = new HashMap<>();
        for (int num : nums) {
            count.put(num, count.getOrDefault(num, 0) + 1);
            if (count.get(num) > 2) {
                return false;
            }
        }
        return true;
    }
}

题目3:

​3074. 重新分装苹果​

给你一个长度为 ​​n​​​ 的数组 ​​apple​​​ 和另一个长度为 ​​m​​​ 的数组 ​​capacity​​ 。

一共有 ​​n​​ 个包裹,其中第 ​​i​​ 个包裹中装着 ​​apple[i]​​ 个苹果。同时,还有 ​​m​​ 个箱子,第 ​​i​​ 个箱子的容量为 ​​capacity[i]​​ 个苹果。

请你选择一些箱子来将这 ​​n​​ 个包裹中的苹果重新分装到箱子中,返回你需要选择的箱子的 最小 数量。

注意,同一个包裹中的苹果可以分装到不同的箱子中。

示例 1:

输入:apple = [1,3,2], capacity = [4,3,1,5,2]
输出:2
解释:使用容量为 4 和 5 的箱子。
总容量大于或等于苹果的总数,所以可以完成重新分装。

示例 2:

输入:apple = [5,5,5], capacity = [2,4,2,7]
输出:4
解释:需要使用所有箱子。

提示:

  • ​1 <= n == apple.length <= 50​
  • ​1 <= m == capacity.length <= 50​
  • ​1 <= apple[i], capacity[i] <= 50​
  • 输入数据保证可以将包裹中的苹果重新分装到箱子中。

题解:

先计算苹果总数,然后按照容量从大到小选择箱子装苹果,直到所有苹果均装入箱子为止。注意题目保证可以将包裹中的苹果重新分装到箱子中。

go代码:

func minimumBoxes(apple []int, capacity []int) int {
    totalApple := getTotalApple(apple)

    sort.Ints(capacity)

    boxNum := 0

    for i:= len(capacity)-1; i>=0; i-- {
        if(totalApple>0) {

            totalApple -= capacity[i]
            boxNum++
        }

        if totalApple<=0 {
            return boxNum
        }
    }
    
    return boxNum

}

func getTotalApple(apple []int) int {
    s := 0
    for i :=0; i<len(apple); i++ {
        s += apple[i]
    }
    return s
}

java代码:

class Solution {
    public int minimumBoxes(int[] apple, int[] capacity) {
        int totalApple = getTotalApple(apple);

        Arrays.sort(capacity);

        int boxNum = 0;
        int i= capacity.length-1;

        while(totalApple>0) {
            totalApple -= capacity[i--];
            boxNum++;
        }
        
        return boxNum;
    }

    public int getTotalApple(int[] apple) {
        int n = 0;
        for(int i=0; i<apple.length; i++) {
            n+=apple[i];
        }
        return n;
    }
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐