力扣2831.找出最长等值子数组

力扣2831.找出最长等值子数组

  • 思路:用二维数组存每个数字的出现下标

    • 遍历所有数字求结果
    • 当前子数组大小:pos[i] - pos[j] + 1;
    • 当前相同数个数:i - j + 1;
    • 需要删去的数的个数:pos[i] - pos[j] - i + j;
  •   class Solution {
      public:
          int longestEqualSubarray(vector<int>& nums, int k) {
              int n = nums.size();
              vector<vector<int>> pos_list(n+1);
              for(int i=0;i<n;i++)
              {
                  pos_list[nums[i]].emplace_back(i);
              }
      
              int res =0;
              for(auto pos:pos_list)
              {
                  for(int i=0,j=0;i<pos.size();i++)
                  {
                      while(pos[i] - pos[j] - i + j > k)
                          j ++;
                      res = max(res,i-j+1);
                  }
              }
              return res;
          }
      };
    

相关推荐

  1. 2831.等值数组

    2024-06-06 07:30:10       30 阅读
  2. 2834. 美丽数组的小和

    2024-06-06 07:30:10       39 阅读
  3. 2765-交替数组

    2024-06-06 07:30:10       68 阅读
  4. 每日一题】2765交替数组

    2024-06-06 07:30:10       72 阅读
  5. 2024.1.23每日一题——交替数组

    2024-06-06 07:30:10       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 07:30:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 07:30:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 07:30:10       82 阅读
  4. Python语言-面向对象

    2024-06-06 07:30:10       91 阅读

热门阅读

  1. axios

    axios

    2024-06-06 07:30:10      29 阅读
  2. iOS中的UIScene和UISceneDelegate

    2024-06-06 07:30:10       27 阅读
  3. Android AAudio——音频流释放死锁(七)

    2024-06-06 07:30:10       29 阅读
  4. Python中的上下文管理:深入探索contextlib模块

    2024-06-06 07:30:10       27 阅读
  5. centos系统编译openssl和openssl-lib的rpm安装包

    2024-06-06 07:30:10       21 阅读
  6. godot.bk2

    godot.bk2

    2024-06-06 07:30:10      30 阅读
  7. Git commit规范

    2024-06-06 07:30:10       24 阅读
  8. 入门级python编程题(12)洛谷(分类平均)

    2024-06-06 07:30:10       19 阅读
  9. Chatgpt-4o:人工智能领域的革新与未来展望

    2024-06-06 07:30:10       34 阅读
  10. 提交一个Bug需要哪些信息?

    2024-06-06 07:30:10       26 阅读