Untitled

 avatar
unknown
plain_text
5 months ago
4.9 kB
3
Indexable
package h2432;

import java.util.HashMap;
import java.util.TreeSet;
 
class UserSolution {
     
    class Stock implements Comparable<Stock>{       
        int stockCode, bizCode, type, price;
      
        public Stock(int mStockCode, int mBizCode, int mType, int mPrice) {
            this.stockCode = mStockCode;
            this.bizCode = mBizCode;
            this.type = mType;
            this.price = mPrice;
        }
        public int compareTo(Stock s) {
            return (this.price == s.price) ? this.stockCode - s.stockCode : this.price - s.price;
        }
    }
     
    HashMap<Integer,Stock> stockMap;
    TreeSet<Stock> stockSetMatrix[][];
    
    public void init() {
         
        stockMap = new HashMap<Integer, Stock>();
        stockSetMatrix = new TreeSet[3][2];
         
        for(int i = 0 ; i < 3 ; i++){ // bizCode
            for(int j = 0 ; j < 2 ; j++){ // type
                stockSetMatrix[i][j] = new TreeSet<Stock>();
            }
        }
        return;
    }
 
    public int add(char mStockInfo[]) {
         
        String[] stockInfo = String.valueOf(mStockInfo).split(",");
        int stockCode = 0 ,bizcode = 0 , type = 0 , price = -1;
        for(int i = 0 ; i < stockInfo.length ; i++){
             
            if(stockInfo[i].contains("STOCKCODE")){
                stockCode = Integer.parseInt(stockInfo[i].substring(stockInfo[i].indexOf("{")+1, stockInfo[i].indexOf("}")));
            }else if(stockInfo[i].contains("BIZCODE")){
                bizcode = Integer.parseInt(stockInfo[i].substring(stockInfo[i].indexOf("{")+1, stockInfo[i].indexOf("}"))) - 1;
            }else if(stockInfo[i].contains("TYPE")){
                if(stockInfo[i].contains("common")){
                    type = 0;
                }else{
                    type = 1;
                }
            }else if(stockInfo[i].contains("PRICE")){
                price = Integer.parseInt(stockInfo[i].substring(stockInfo[i].indexOf("{")+1, stockInfo[i].indexOf("}")));
            }
        }
         
        Stock stock = new Stock(stockCode,bizcode,type,price);
        stockMap.put(stockCode, stock);
        stockSetMatrix[bizcode][type].add(stock);
         
        return stockSetMatrix[bizcode][type].last().stockCode;
    }
 
    public int remove(int mStockCode) {
         
        if(stockMap.containsKey(mStockCode)){
             
            Stock stock = stockMap.get(mStockCode);
            int bizCode = stock.bizCode;
            int type = stock.type;
            stockSetMatrix[bizCode][type].remove(stock);
            stockMap.remove(stock);
            if(!stockSetMatrix[bizCode][type].isEmpty()){
                return stockSetMatrix[bizCode][type].first().stockCode;
            }
        }
 
        return -1;
    }
 
    public int search(char mCondition[]) {
         
        int[] bizCodes = new int[3];
        bizCodes[0] = bizCodes[1] = bizCodes[2] = -1;
        int[] types = new int[2];
        types[0] = types[1] = -1;
        int price = -1;
        TreeSet<Stock> answerSet = new TreeSet<Stock>();
         
        String[] condition = String.valueOf(mCondition).split(";");
         
        for(int i = 0 ; i < condition.length ; i++){
             
            if(condition[i].contains("BIZCODE")){
                String[] bizcode = condition[i].substring(condition[i].indexOf("{")+1, condition[i].indexOf("}")).split(",");
                for(int c = 0 ; c < bizcode.length ; c++){
                    bizCodes[c] = Integer.parseInt(bizcode[c])-1;
                }
            }else if(condition[i].contains("TYPE")){
                String[] type = condition[i].substring(condition[i].indexOf("{")+1, condition[i].indexOf("}")).split(",");
                for(int t = 0 ; t < type.length ; t++){
                    if(type[t].contains("common")){
                        types[t] = 0;
                    }else{
                        types[t] = 1;
                    }
                }
            }else if(condition[i].contains("PRICE")){
                price = Integer.parseInt(condition[i].substring(condition[i].indexOf("{")+1, condition[i].indexOf("}")));
            }
        }
 
         
        for(int i = 0 ; i < 3 ; i++){
            if(bizCodes[i] == -1) break;
            for(int j = 0 ; j < 2 ; j++){
                if(types[j] == -1) break;
                Stock stock = stockSetMatrix[bizCodes[i]][types[j]].ceiling(new Stock(0,0,0,price));
                if(stock != null){
                    answerSet.add(stock);
                }
            }
        }
         
        if(!answerSet.isEmpty()){
            return answerSet.first().stockCode;
        }
         
        return -1;
    }
}
Editor is loading...
Leave a Comment