• AutoX-1. 网页瀑布流 && AutoX-2. 蚂蚁王国的蜂蜜


    1.Description of Problems

    AutoX-1. 网页瀑布流
    通过的用户数272
    尝试过的用户数286
    用户总通过次数276
    用户总提交次数381
    题目难度Easy
    网页布局中有一种瀑布流布局方式,表现为参差不齐的多栏布局。随着页面滚动条向下,还会不断加载数据块并附加至当前尾部。页面在加载时遵循以下规则:

    当有数据块需要加载时,优先加载在高度最短的那一列;
    若存在多个高度相同且最短的情况,则加载在其中最靠左的那一列
    已知当前网页共分割为 num 列,该网页有若干数据块可以加载,block[i] 表示第 i 个数据块的高度。当页面按顺序加载完所有的数据块后,请返回高度最大的那一列的高度。

    示例 1:

    输入:num = 3, block = [5,9,8,6]

    输出:11

    解释:如下图所示,返回 11
    image.png

    示例 2:

    输入:num = 2, block = [9,1,1,1,1,1]

    输出:9

    提示:

    0 < num <= 100
    0 < block.length <= 10^4
    0 < block[i] <= 10^3
    [
    ]
    https://leetcode.cn/contest/autox2023/problems/l9HbCJ/
    AutoX-2. 蚂蚁王国的蜂蜜
    通过的用户数240
    尝试过的用户数246
    用户总通过次数241
    用户总提交次数395
    题目难度Easy
    蚂蚁王国的蜂蜜专家旺财最近在研究蜂蜜的价格,为了估算出真实的蜂蜜价格,旺财以所有用有效报价的平均值作为蜂蜜均价,稳定的报价往往方差也比较小。因为情报具有时效性,所以每隔一段时间,旺财也会删除一些老旧报价。
    因为计算平均值和方差对于蚂蚁是一个困难的问题,所以旺财希望你帮他设计一个系统,handle[i] = [type] 或 [type, value] 表示对于旺财的第 i 次的操作有:

    若 type 为 1,表示获取了一份价格为 value 的报价
    若 type 为 2,表示删除了一个价格为 value 的报价
    若 type 为 3,表示计算当前蜂蜜的均价;若当前不存在任何有效报价,返回 -1
    若 type 为 4,表示计算当前价格的方差;若当前不存在任何有效报价,返回 -1
    请按操作的顺序,依次返回所有计算均价和方差的结果。

    提示:

    用例保证所有删除的报价都是有效的。
    示例 1:

    输入:handle = [[1,1],[1,2],[1,3],[1,2],[3],[4],[2,1],[2,2],[2,3],[3],[4]]

    输出:[2.00000,0.50000,2.00000,0.00000]
    https://leetcode.cn/contest/autox2023/problems/8p6t8R/

    2.My Solution

    AutoX-1. 网页瀑布流

    class Solution {
    public:
        int getLengthOfWaterfallFlow(int num, vector<int>& block) {
            
            vector<int> result;
            
              if(num > block.size()){
                 int max =0;
                 for(int i=0;i<block.size();i++){
                 if(block[i]>max){
                       max = block[i];
                    }
                 }
                return max;
            }
            
            for(int i = 0;i < num;i++){
                result.push_back(block[i]);
            }
            
            
          
            for(int i = num;i < block.size(); i++){
               int min   = 100000;
               int index = 0;
               for(int j=0;j < num;j++){
                   if(result[j]<min){
                       index = j;
                       min = result[j];
                   }
               }
          //cout<
               result[index] += block[i];
            }
            int max = 0;
            //sort(result.begin(),result.end());
            for(int i=0;i<result.size();i++){
                if(result[i]>max){
                    max = result[i];
                }
            }
            return max;
            
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    AutoX-2. 蚂蚁王国的蜂蜜

    class Solution {
    public:
        vector<double> honeyQuotes(vector<vector<int>>& handle) {
           vector<double> resValue;
           vector<double> array;
           vector<double> temp;
           for(int i=0;i<handle.size();i++){
              // for(int j=0;j
                   //if(handle[i][0] == 3 && handle[i].size  || handle)
                   //处理若 type 为 1,表示获取了一份价格为 value 的报价
                   if(handle[i][0] == 1 && handle[i].size()==2){
                       array.push_back(handle[i][1]);
                   }
                   //若 type 为 2,表示删除了一个价格为 value 的报价
                   if(handle[i][0] == 2 && handle[i].size()==2){
                       int times = 0;
                       for(int k = 0;k<array.size();k++){
                           if(times != 0 || handle[i][1] != array[k]){
                               temp.push_back(array[k]);
                           }
                           else {
                               times++;
                           }
                       }
                       array.clear();
                       array = temp;
                       temp.clear();
                   }
               
                  //若 type 为 3,表示计算当前蜂蜜的均价;若当前不存在任何有效报价,返回 -1
                  if(handle[i][0] == 3 && array.size() == 0){
                      resValue.push_back(-1);
                  } 
                  double average=0; 
                  if(handle[i][0] == 3 && array.size() != 0){
                      for(int g = 0;g<array.size();g++){
                          average += array[g];   
                      }
                      average = average/array.size();
                      resValue.push_back(average);
                  }
                  
                  //若 type 为 4,表示计算当前价格的方差;若当前不存在任何有效报价,返回 -1
                   if(handle[i][0] == 4 && array.size() == 0){
                      resValue.push_back(-1);
                  } 
                   double s2 = 0;
                   average = 0;
                   if(handle[i][0] == 4 && array.size() != 0){
                      for(int g = 0;g<array.size();g++){
                          average += array[g];   
                      }
                      average = average/array.size();
                      for(int g = 0;g<array.size();g++){
                          s2 += (array[g] - average) * (array[g] - average);
                      }
                      s2 = s2/array.size();
                      resValue.push_back(s2);
               }
              
           }
             return resValue;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
  • 相关阅读:
    Dart 2.18 正式发布
    redux原理分享
    C/C++---------------LeetCode第1394.找出数组中的幸运数
    C51项目 - 可调万年历
    556. 下一个更大元素 III
    读取不同格式文件中的内容(xlsx,csv,txt,npz,yaml)
    Kafak简单使用
    使用Python进行数据可视化
    大厂镜像库
    Android 13.0 framework修改AlertDialog对话框的button样式
  • 原文地址:https://blog.csdn.net/qq_40464599/article/details/126609988