leetcode
leetcode 2751 ~ 2800
最高频率的 ID

最高频率的 ID

难度:

标签:

题目描述

The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, nums and freq, of equal length n. Each element in nums represents an ID, and the corresponding element in freq indicates how many times that ID should be added to or removed from the collection at each step.

  • Addition of IDs: If freq[i] is positive, it means freq[i] IDs with the value nums[i] are added to the collection at step i.
  • Removal of IDs: If freq[i] is negative, it means -freq[i] IDs with the value nums[i] are removed from the collection at step i.

Return an array ans of length n, where ans[i] represents the count of the most frequent ID in the collection after the ith step. If the collection is empty at any step, ans[i] should be 0 for that step.

 

Example 1:

Input: nums = [2,3,2,1], freq = [3,2,-3,1]

Output: [3,3,2,2]

Explanation:

After step 0, we have 3 IDs with the value of 2. So ans[0] = 3.
After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So ans[1] = 3.
After step 2, we have 2 IDs with the value of 3. So ans[2] = 2.
After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So ans[3] = 2.

Example 2:

Input: nums = [5,5,3], freq = [2,-2,1]

Output: [2,0,1]

Explanation:

After step 0, we have 2 IDs with the value of 5. So ans[0] = 2.
After step 1, there are no IDs. So ans[1] = 0.
After step 2, we have 1 ID with the value of 3. So ans[2] = 1.

 

Constraints:

  • 1 <= nums.length == freq.length <= 105
  • 1 <= nums[i] <= 105
  • -105 <= freq[i] <= 105
  • freq[i] != 0
  • The input is generated such that the occurrences of an ID will not be negative in any step.

代码结果

运行时间: 244 ms, 内存: 37.0 MB


// Solution in Java using Streams
// The approach remains the same as the Java solution but utilizing Streams for certain operations.

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.IntStream;

public class FrequencyTrackerStream {
    public int[] findMaxFrequencies(int[] nums, int[] freq) {
        int n = nums.length;
        int[] ans = new int[n];
        Map<Integer, Integer> idToFreq = new HashMap<>();
        TreeMap<Integer, Integer> freqCount = new TreeMap<>();
        
        IntStream.range(0, n).forEach(i -> {
            int id = nums[i];
            int change = freq[i];
            
            // Update the frequency in the map
            int oldFreq = idToFreq.getOrDefault(id, 0);
            int newFreq = oldFreq + change;
            idToFreq.put(id, newFreq);
            
            // Update the frequency count map
            if (oldFreq > 0) {
                freqCount.put(oldFreq, freqCount.get(oldFreq) - 1);
                if (freqCount.get(oldFreq) == 0) freqCount.remove(oldFreq);
            }
            freqCount.put(newFreq, freqCount.getOrDefault(newFreq, 0) + 1);
            
            // Get the maximum frequency
            ans[i] = freqCount.isEmpty() ? 0 : freqCount.lastKey();
        });
        return ans;
    }
}

解释

方法:

该题解采用了一个数组 id 来记录每个 ID 的出现频率。数组的索引对应 ID-1(因为 ID 从 1 开始,而索引从 0 开始)。遍历 nums 和 freq 数组,根据 freq 的值来增加或减少 id 数组对应索引的值。同时,使用 mx 来维护当前最高频率的数值,使用 mx_idx 来维护达到最高频率的 ID 的索引。在每次更新 id 数组后,需要检查是否需要更新 mx 和 mx_idx:如果当前 ID 的频率超过了 mx,更新 mx 和 mx_idx;如果当前 ID 是之前的最高频率 ID 但其频率降低了,则需要重新遍历 id 数组以找到新的最高频率和对应的 ID。最后,将 mx 的值添加到结果列表 ans 中。

时间复杂度:

O(n*m)

空间复杂度:

O(n)

代码细节讲解

🦆
题解中提到`如果当前 ID 的频率超过了 mx,更新 mx 和 mx_idx`,在实际操作中,如果存在多个 ID 频率相同且为最高频率,如何确定更新的 mx_idx?
题解中的逻辑处理确实没有明确指定如何处理多个 ID 频率相同且为最高频率的情况。通常,更新策略可能依赖于具体的应用需求。在题解的当前实现中,如果有多个 ID 的频率相同并且都是最高的,`mx_idx` 将更新为最先达到该频率的 ID 的索引。这是因为每次只在当前 ID 的频率超过现有的最大频率`mx`时更新`mx_idx`。如果需要考虑所有具有最高频率的 ID,可能需要额外的数据结构(如列表)来存储所有这些 ID,并在每次更新时维护此列表。
🦆
在处理频率减少的情况时,题解只有在`当前最大频率 ID 的频率减少`时才重新计算最大频率,如果减少的不是当前最高频率的 ID 但该操作导致其他 ID 的频率增加成为新的最高频率,这种情况是否考虑到了?
题解中确实没有明确考虑这种情况。题解所述的逻辑仅在当前最高频率的 ID 频率减少时重新计算最大频率。如果其他 ID 的频率由于某个操作而增加并超过当前最高频率,但不影响当前最高频率 ID 的频率,题解的逻辑不会更新最大频率。这可能导致算法无法正确反映实时的最高频率 ID。要完全准确地处理所有情况,每次频率更新后可能需要重新扫描整个频率数组以确认当前的最高频率,这会增加算法的时间复杂度。
🦆
在实际执行中,如果 `freq` 数组中的减少操作导致某个 ID 的频率变为负数,题解对此有什么处理策略?
题解中没有明确提到对频率可能变为负数的处理策略。在实践中,频率变为负数通常意味着输入数据有误或者处理逻辑有问题,因为 ID 的出现次数不可能是负数。一个合理的做法是在更新频率前先检查`freq`中的值是否会导致频率数组`id`中的值变为负,如果是,则应该抛出错误或进行某种形式的异常处理。这需要在实现中添加对应的错误检查和处理逻辑,以确保数据的准确性和算法的健壮性。

相关问题