zoukankan      html  css  js  c++  java
  • LeetCode 第 28 场双周赛

    5420.商品折扣后的最终价格

    题目链接:5420.商品折扣后的最终价格

    给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。

    商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i
    prices[j] <= prices[i]最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。

    请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。

    样例输入与样例输出 Sample Input and Sample Output

    示例 1:

    **输入:** prices = [8,4,6,2,3]
    **输出:** [4,2,4,2,3]
    **解释:**
    商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
    商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
    商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
    商品 3 和 4 都没有折扣。
    

    示例 2:

    **输入:** prices = [1,2,3,4,5]
    **输出:** [1,2,3,4,5]
    **解释:** 在这个例子中,所有商品都没有折扣。
    

    示例 3:

    **输入:** prices = [10,1,1,6]
    **输出:** [9,0,1,6]
    

    提示 Hint

    提示:

    • 1 <= prices.length <= 500
    • 1 <= prices[i] <= 10^3

    题解

    class Solution {
     public:
      vector<int> finalPrices(vector<int>& prices) {
        const int n = prices.size();
        vector<int>ans(n);
        for(int i = 0; i < n; ++i) {
          ans[i] = prices[i];
          for(int j = i + 1; j < n; ++j) {
            if(prices[j] <= prices[i]) {
              ans[i] -= prices[j];
              break;
            }
          }
        }
        return ans;
      }
    };
    
    

    5422.子矩形查询

    题目链接:5422.子矩形查询

    请你实现一个类 SubrectangleQueries ,它的构造函数的参数是一个 rows x cols
    的矩形(这里用整数矩阵表示),并支持以下两种操作:

    1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)

    • newValue 更新以 (row1,col1) 为左上角且以 (row2,col2) 为右下角的子矩形。

    2. getValue(int row, int col)

    • 返回矩形中坐标 (row,col) 的当前值。

    样例输入与样例输出 Sample Input and Sample Output

    示例 1:

    **输入:**
    ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
    [[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
    **输出:**
    [null,1,null,5,5,null,10,5]
    **解释:**
    SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);
    // 初始的 (4x3) 矩形如下:
    // 1 2 1
    // 4 3 4
    // 3 2 1
    // 1 1 1
    subrectangleQueries.getValue(0, 2); // 返回 1
    subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
    // 此次更新后矩形变为:
    // 5 5 5
    // 5 5 5
    // 5 5 5
    // 5 5 5
    subrectangleQueries.getValue(0, 2); // 返回 5
    subrectangleQueries.getValue(3, 1); // 返回 5
    subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
    // 此次更新后矩形变为:
    // 5   5   5
    // 5   5   5
    // 5   5   5
    // 10  10  10
    subrectangleQueries.getValue(3, 1); // 返回 10
    subrectangleQueries.getValue(0, 2); // 返回 5
    

    示例 2:

    **输入:**
    ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
    [[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
    **输出:**
    [null,1,null,100,100,null,20]
    **解释:**
    SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
    subrectangleQueries.getValue(0, 0); // 返回 1
    subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
    subrectangleQueries.getValue(0, 0); // 返回 100
    subrectangleQueries.getValue(2, 2); // 返回 100
    subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
    subrectangleQueries.getValue(2, 2); // 返回 20
    

    提示 Hint

    提示:

    • 最多有 500updateSubrectanglegetValue 操作。
    • 1 <= rows, cols <= 100
    • rows == rectangle.length
    • cols == rectangle[i].length
    • 0 <= row1 <= row2 < rows
    • 0 <= col1 <= col2 < cols
    • 1 <= newValue, rectangle[i][j] <= 10^9
    • 0 <= row < rows
    • 0 <= col < cols

    题解

    class SubrectangleQueries {
     public:
      vector<vector<int>>rect;
      SubrectangleQueries(vector<vector<int>>& rectangle) {
        rect = rectangle;
      }
    
      void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        for(int i = row1; i <= row2; i++) {
          for(int j = col1; j <= col2; j++) {
            rect[i][j] = newValue;
          }
        }
      }
    
      int getValue(int row, int col) {
        return rect[row][col];
      }
    };
    
    /**
     * Your SubrectangleQueries object will be instantiated and called as such:
     * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);
     * obj->updateSubrectangle(row1,col1,row2,col2,newValue);
     * int param_2 = obj->getValue(row,col);
     */
    
    

    5423.找两个和为目标值且不重叠的子数组

    题目链接:5423.找两个和为目标值且不重叠的子数组

    给你一个整数数组 arr 和一个整数值 target

    请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target 。可能会有多种方案,请你返回满足要求的两个子数组长度和的
    最小值

    请返回满足要求的最小长度和,如果无法找到这样的两个子数组,请返回 -1

    样例输入与样例输出 Sample Input and Sample Output

    示例 1:

    **输入:** arr = [3,2,2,4,3], target = 3
    **输出:** 2
    **解释:** 只有两个子数组和为 3 ([3] 和 [3])。它们的长度和为 2 。
    

    示例 2:

    **输入:** arr = [7,3,4,7], target = 7
    **输出:** 2
    **解释:** 尽管我们有 3 个互不重叠的子数组和为 7 ([7], [3,4] 和 [7]),但我们会选择第一个和第三个子数组,因为它们的长度和 2 是最小值。
    

    示例 3:

    **输入:** arr = [4,3,2,6,2,3,4], target = 6
    **输出:** -1
    **解释:** 我们只有一个和为 6 的子数组。
    

    示例 4:

    **输入:** arr = [5,5,4,4,5], target = 3
    **输出:** -1
    **解释:** 我们无法找到和为 3 的子数组。
    

    示例 5:

    **输入:** arr = [3,1,1,1,5,1,2,1], target = 3
    **输出:** 3
    **解释:** 注意子数组 [1,2] 和 [2,1] 不能成为一个方案因为它们重叠了。
    

    提示 Hint

    提示:

    • 1 <= arr.length <= 10^5
    • 1 <= arr[i] <= 1000
    • 1 <= target <= 10^8

    题解

    class Solution {
     public:
    
      bool cross(pair<int, int>a, pair<int, int>b) {
        swap(a.first, a.second);
        swap(b.first, b.second);
        if(a.first > b.first + b.second - 1)
          return false;
        if(b.first > a.first + a.second - 1)
          return false;
        return true;
      }
    
      int minSumOfLengths(vector<int>& arr, int target) {
        const int n = arr.size();
        vector<int>s(n + 1, 0);
        vector<pair<int, int>>ans;
    
        int p = 0;
        for(int i = 0; i < n; ++i) {
          s[i + 1] = s[i] + arr[i];
          while(s[i + 1] - s[p] > target)
            p++;
          if(s[i + 1] - s[p] == target) {
            cout << p << " " << i - p + 1 << endl;
            ans.push_back(make_pair(i - p + 1, p));
          }
        }
        sort(ans.begin(), ans.end());
        if(ans.size() > 1) {
          for(int j = 0; j < ans.size(); j++)
            for(int k = j + 1; k < ans.size(); k++) {
              if(!cross(ans[j], ans[k]))
                return ans[j].first + ans[k].first;
            }
        }
    
        //for(auto i:ans) cout << i.first << "," << i.second<<")(";
        return -1;
      }
    };
    
    

    5421.安排邮筒

    题目链接:5421.安排邮筒

    给你一个房屋数组houses 和一个整数 k ,其中 houses[i] 是第 i 栋房子在一条街上的位置,现需要在这条街上安排 k
    个邮筒。

    请你返回每栋房子与离它最近的邮筒之间的距离的 最小 总和。

    答案保证在 32 位有符号整数范围以内。

    样例输入与样例输出 Sample Input and Sample Output

    示例 1:

    ![](https://assets.leetcode-cn.com/aliyun-lc-
    upload/uploads/2020/06/13/sample_11_1816.png)

    **输入:** houses = [1,4,8,10,20], k = 3
    **输出:** 5
    **解释:** 将邮筒分别安放在位置 3, 9 和 20 处。
    每个房子到最近邮筒的距离和为 |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5 。
    

    示例 2:

    ![](https://assets.leetcode-cn.com/aliyun-lc-
    upload/uploads/2020/06/13/sample_2_1816.png)

    **输入:** houses = [2,3,5,12,18], k = 2
    **输出:** 9
    **解释:** 将邮筒分别安放在位置 3 和 14 处。
    每个房子到最近邮筒距离和为 |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9 。
    

    示例 3:

    **输入:** houses = [7,4,6,1], k = 1
    **输出:** 8
    

    示例 4:

    **输入:** houses = [3,6,14,10], k = 4
    **输出:** 0
    

    提示 Hint

    提示:

    • n == houses.length
    • 1 <= n <= 100
    • 1 <= houses[i] <= 10^4
    • 1 <= k <= n
    • 数组 houses 中的整数互不相同。

    题解

    对于只有一个邮箱的情况,显然有取中位数位置最优。
    对于多个的情况,dp 枚举第 i 个在第几组,各组内取最优解即可。
    dp[i][j] 需要枚举上一块的尾部位置
    dp[n-1][k-1] 即答案。

    class Solution {
     public:
      int parse(vector<int>&houses, int l, int r)const {
        int ans(0);
        int m = (r + l) >> 1;
        for(int i = l; i <= r; ++i) {
          ans += abs(houses[i] - houses[m]);
          //cout << houses[i]<< " ";
        }
        //cout << ans <<endl;
        return ans;
      }
    
      int minDistance(vector<int>& houses, int k) {
        const int n = houses.size();
        if(n <= k)
          return 0;
        vector<vector<int>>ans(n, vector<int>(k, INT_MAX));
        vector<vector<int>>pp(n, vector<int>(n));
        sort(houses.begin(), houses.end());
    
        for(int i = 0; i < n; ++i)
          for(int j = i; j < n; ++j)
            pp[i][j] = parse(houses, i, j);
    
        for(int i = 0; i < n; ++i) {
          ans[i][0] = pp[0][i];
          for(int j = 1; j < k; ++j) {
            ans[i][j] = INT_MAX;
            for(int k = 0; k < i; ++k)
              if(ans[k][j - 1] != INT_MAX)
                ans[i][j] = min(ans[i][j], ans[k][j - 1] + pp[k + 1][i]);
            //cout << "ans[" << i << ","<<j << "]=" << ans[i][j] << endl;
          }
        }
        return ans[n - 1][k - 1];
      }
    };
    
    
  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/Forgenvueory/p/13129126.html
Copyright © 2011-2022 走看看