首页 > 其他分享 >LeeCode 318周赛复盘

LeeCode 318周赛复盘

时间:2022-11-06 15:34:11浏览次数:88  
标签:周赛 318 return nums int res LeeCode o2 o1

T1: 对数组执行操作

思路:模拟

public int[] applyOperations(int[] nums) {
  int n = nums.length;
  for (int i = 0; i < n - 1; ++i) {
    if (nums[i] == nums[i + 1]) {
      nums[i] = 2 * nums[i];
      nums[i + 1] = 0;
    }
  }

  int[] res = new int[n];

  int index = 0;
  for (int i = 0; i < n; i++) {
    if (nums[i] != 0) {
      res[index++] = nums[i];
    }
  }

  return res;
}

T2: 长度为 K 子数组中的最大和

思路:滑动窗口

public long maximumSubarraySum(int[] nums, int k) {
  int n = nums.length;

  if (k > n) {
    return 0;
  }


  // 哈希存储当前窗口内的数组元素
  Map<Integer, Integer> map = new HashMap<>();
  long res = 0;
  long sum = 0;
  int left = 0, right = 0;

  while (right < n) {
    sum += nums[right];
    map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);

    if (right - left + 1 == k) {
      if (map.size() == k) {
        res = Math.max(res, sum);
      }

      sum -= nums[left];
      map.put(nums[left], map.get(nums[left]) - 1);
      if (map.get(nums[left]) == 0) {
        map.remove(nums[left]);
      }

      left += 1;
    }

    right += 1;
  }

  return res;
}

T3: 雇佣 K 位工人的总代价

思路:小顶堆模拟

  • 左堆堆顶元素 $$\le$$ 右堆堆顶元素:弹出左堆值
  • 左堆堆顶元素 $$\gt$$ 右堆堆顶元素:弹出右堆值
public long totalCost(int[] costs, int k, int candidates) {
  int n = costs.length;
  long res = 0;

  // 考虑特殊情况,快速返回结果
  if (k == n) {
    for (int cost : costs) {
      res += cost;
    }

    return res;
  }

  if (candidates * 2 >= n) {
    Queue<Integer> heap = new PriorityQueue<>();
    for (int cost : costs) {
      heap.offer(cost);
    }

    while (k > 0) {
      res += heap.poll();
      k -= 1;
    }

    return res;
  }

  // 小顶堆模拟
  Queue<int[]> leftMin = new PriorityQueue<>((o1, o2) -> {
    if (o1[0] == o2[0]) {
      return o1[1] - o2[1];
    }
    else {
      return o1[0] - o2[0];
    }
  });

  Queue<int[]> rightMin = new PriorityQueue<>((o1, o2) -> {
    if (o1[0] == o2[0]) {
      return o1[1] - o2[1];
    }
    else {
      return o1[0] - o2[0];
    }
  });

  int curLen = n;

  for (int i = 0; i < candidates; ++i) {
    leftMin.offer(new int[]{costs[i], i});
    rightMin.offer(new int[]{costs[n - 1 - i], n - 1 - i});
  }


  int leftIndex = candidates;
  int rightIndex = n - candidates - 1;
  while (curLen > 2 * candidates && k > 0) {
    if (leftMin.peek()[0] <= rightMin.peek()[0]) {
      res += leftMin.poll()[0];
      leftMin.offer(new int[]{costs[leftIndex], leftIndex});
      leftIndex += 1;
      curLen -= 1;
    }
    else {
      res += rightMin.poll()[0];
      rightMin.offer(new int[]{costs[rightIndex], rightIndex});
      rightIndex -= 1;
      curLen -= 1;
    }

    k -= 1;
  }

  while (k > 0) {
    if (leftMin.isEmpty()) {
      res += rightMin.poll()[0];
    }
    else if (rightMin.isEmpty()) {
      res += leftMin.poll()[0];
    }
    else {
      res += (leftMin.peek()[0] <= rightMin.peek()[0] ? leftMin.poll()[0] : rightMin.poll()[0]);
    }

    k -= 1;
  }

  return res;
}

T4: 最小移动总距离

标签:周赛,318,return,nums,int,res,LeeCode,o2,o1
From: https://www.cnblogs.com/ylyzty/p/16862680.html

相关文章

  • Acwing76场周赛
    题目链接这次还是只做出来两道题,前两题都挺简单的,注意第二题需要开longlong不开会wa,代码粘上来,以后可能会看吧第一题#include<iostream>#include<string>usingnam......
  • 20201318李兴昕第十二章学习笔记
    第十二章:块设备I/O和缓冲区管理知识点归纳总结:本章讨论了块设备I/O和缓冲区管理;解释了块设备I/O的原理和I/O缓冲的优点;论述了Unix的缓冲区管理算法,并指出了其不足之......
  • 洛谷 P3183 [HAOI2016]食物链(dfs+记忆化搜索)
    https://www.luogu.com.cn/problem/P3183题目大意:给定n个节点,标号分别为1——n,然后给出m条有向边,问我们不同的食物链路径有多少?输入#1101612141102325......
  • OJ周赛第一场——连环画
    连环画 问题描述连环画是一种特殊类型的填字游戏。小马和小亮最近迷上了这个游戏。他们一共有n个不同的单词,每个单词有无限个。他们很好奇如果各自拿若干个单......
  • OJ周赛第一次——Good morning
    Goodmorning 问题描述一天,小马在A点过B分准时起床(24小时制),而小亮在C点过D分1秒准时起床。 输入 输入4个整数A,B,C,D0≤A≤230≤B≤590≤C≤230≤D≤5......
  • OJ周赛第一场——数列
    数列 问题描述给你一个长度为N的由0和1组成的整数序列:A=(A1,A2,⋯,AN​)。你可以选择是否进行一个操作。该操作为选择一个区间(l,r),使得区间的0变成1,1变成0。......
  • 2022年zzuli周赛(2)
    消消乐我们可以考虑贪心,想一想,如果\(s\)串和\(t\)串中有一个字母相同的话,是不是就相当于必然存在\(S,t\)相同(将两个字符串删减成一个字母就可以了)C:#include<stdio......
  • LeeCode 317周赛复盘
    T1:可被3整数的偶数的平均值思路:数组遍历被3整数的偶数\(\Leftrightarrow\)被6整数的数publicintaverageValue(int[]nums){intsum=0;intcount=0;......
  • leetcode 第90场双周赛
    6226.摧毁一系列目标题意:对于数组中每一个数nums[i],可以摧毁数组中值等于nums[i]+c*space的数(c为非负整数),求摧毁最大数量时的最小nums[i]思路:如果两个数x,y可以同时被摧......
  • acwing第75场周赛
    这次题比较水,但是还是没能ak,自己小结一下吧第一道题就是自己枚举相加就行第二道题是一个多关键字排序,wa了几次,是因为优先级有两个是相同的需要特判一下,然后可以把字符转......