LCR 059. 数据流中的第 K 大元素 03.30 (二)

hao

设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。

请实现 KthLargest 类:

  • KthLargest(int k, int[] nums) 使用整数 k 和整数流 nums 初始化对象。
  • int add(int val) 将 val 插入数据流 nums 后,返回当前数据流中第 k 大的元素。

示例:

输入:
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
输出:
[null, 4, 5, 5, 8, 8]

解释:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3);   // return 4
kthLargest.add(5);   // return 5
kthLargest.add(10);  // return 5
kthLargest.add(9);   // return 8
kthLargest.add(4);   // return 8

提示:

  • 1 <= k <= 104
  • 0 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • -104 <= val <= 104
  • 最多调用 add 方法 104 次
  • 题目数据保证,在查找第 k 大元素时,数组中至少有 k 个元素
public class KthLargest {
    int k;
    List<int> count = new List<int>();
    public KthLargest(int k, int[] nums) {
        this.k = k;
        foreach(var item in nums){
            count.Add(item);
        }

    }
    
    public int Add(int val) {
        count.Add(val);
        count.Sort();
        return count[count.Count - k];
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.Add(val);
 */

耗时居然1700ms,

public class KthLargest {
    int k;
    List<int> count = new List<int>();
    public KthLargest(int k, int[] nums) {
        this.k = k;
        foreach(var item in nums){
            count.Add(item);
        }
        Sort();
    }
    
    public int Add(int val) {
        count.Add(val);
        Sort();
        return count[0];
    }
    private void Sort(){
        count.Sort();
        if(count.Count<k) return;
        List<int> temp= new List<int>();
        for(int i = k;i>0;i--){
            temp.Add(count[count.Count - i]);
        }
        count = temp;
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.Add(val);
 */

耗时1600ms,还是不行

public class KthLargest {
    int k;
    List<int> res = new List<int>();
    public KthLargest(int k, int[] nums) {
        this.k = k;
        foreach(var item in nums){
            res.Add(item);
        }
        Sort();
    }
    
    public int Add(int val) {
        res.Add(val);
        Sort();
        return res[0];
    }
    private void Sort(){
        res.Sort();
        while (res.Count > k)
            res.RemoveAt(0);
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.Add(val);
 */

1100ms,还差好多啊!!!!!

public class KthLargest {
    int[] arr;
    public KthLargest(int k, int[] nums) {
        arr = new int[k];
        for(int i = 0;i<k;i++) arr[i] = int.MinValue;
        for(int i =0;i<nums.Length;i++) Add(nums[i]);
    }
    
    public int Add(int val) {
        if(val>arr[0]){
            arr[0] = val;
            Sort(0);
        }
        return arr[0];
    }
    public void Sort(int index){
        int left=2*index+1;
        int right=left+1;
        if(left>=arr.Length)
            return;
        int min=arr[left]<arr[index]?left:index;
        if(right<arr.Length)
            min=arr[right]<arr[min]?right:min;
        if(min==index)
            return;
        int tmp=arr[min];
        arr[min]=arr[index];
        arr[index]=tmp;
        Sort(min);
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.Add(val);
 */

108ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小路灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值