2021-03-23 Leetcode-1801

1801.积压订单中的订单总数

今天又超时了:(

思路:维护两个集合sell和buy。遍历订单,根据订单数据判断入,出,删,略。
方法1:使用ArrayList作为集合,需要编写函数找出价格最低的销售订单,价格最高的采购订单。因此需要不断的遍历sell和buy两个集合。
方法2:使用TreeMap作为集合。根据文档。
TreeMap->NavigableMap->SortedMap->Map

The map is sorted according to the natural ordering of its keys
根据key值排序

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
操作的时间度为O(logn)。优于ArrayList遍历的O(n)。
同时TreeMap的firstKey(), lastKey()方法非常的方便

/*
执行用时:72 ms, 在所有 Java 提交中击败了15.89%的用户
内存消耗:86 MB, 在所有 Java 提交中击败了20.97%的用户
*/
public class Solution {
    public int getNumberOfBacklogOrders(int[][] orders) {
        TreeMap<Integer, Integer> sell = new TreeMap<>();
        TreeMap<Integer, Integer> buy = new TreeMap<>();

        for(int i=0; i < orders.length; i++){
            if(orders[i][2] == 0){
                int buyprice = orders[i][0];
                int buyamount = orders[i][1];
                if(sell.size() > 0){
                    //int minsellprice_i = findmaxminprice(sell)[0];
                    while(sell.size() > 0 &&  sell.firstKey() <= buyprice){
                        int minsellprice_i = sell.firstKey();
                        int left = sell.get(minsellprice_i) - buyamount;
                        if(left<= 0){
                            buyamount -= sell.get(minsellprice_i);
                            sell.remove(minsellprice_i);
                            if(buyamount == 0)  break;
                        }else{
                            sell.put(minsellprice_i,left);
                            buyamount=0;
                            break;
                        }
                    }
                }
                if(buyamount > 0){
                    buy.put(buyprice, buy.getOrDefault(buyprice, 0) + buyamount);
                }

            }
            if(orders[i][2] == 1){
                int sellprice = orders[i][0];                
                int sellamount = orders[i][1];
                if(buy.size() > 0){
                    //int maxbuyprice_i = findmaxminprice(buy)[1];
                    while(buy.size() > 0 && buy.lastKey() >= sellprice){
                        int maxbuyprice_i = buy.lastKey();
                        int left = buy.get(maxbuyprice_i) - sellamount;
                        if(left <= 0){
                            sellamount -= buy.get(maxbuyprice_i);
                            buy.remove(maxbuyprice_i);
                            if(sellamount == 0) break;
                        }else{
                            buy.put(maxbuyprice_i, left);
                            sellamount=0;
                            break;
                        }
                    }
                }
                if(sellamount > 0){
                    sell.put(sellprice, sell.getOrDefault(sellprice, 0) + sellamount);
                } 
                

            }
        }
        long amount = 0;
        for(Integer i : sell.values()){
            amount += i;
        }
        for(Integer i : buy.values()){
            amount += i; 
        }
        return (int)(amount % 1000000007);
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Java集合框架 Java中封装了许多常用的数据结构,称为集合框架,可以有效组织数据,提高程序性能。最初Java只...
    Steven1997阅读 4,535评论 0 2
  • 学习集合之前复习相关知识: Hash:翻译为散列、哈希,所以散列和哈希指的是同一个概念。散列码:一种标识码,由散列...
    教堂白鸽阅读 1,664评论 0 1
  • Java的TreeMap是集合框架中的一个实现类,TreeMap继承了AbstractMap。TreeMap实现了...
    稀里糊涂司小呆阅读 1,079评论 0 0
  • 学习目的 了解集合的概念 掌握java集合的分类以及作用 掌握java集合的对象创建,以及常用方法的使用 熟悉ja...
    从前的小余儿阅读 1,547评论 0 0
  • collection主要方法: boolean add(Object o)添加对象到集合boolean remov...
    becauseyou_90cd阅读 2,463评论 0 0