Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
import java.util.*;
class UserSolution {
    TreeMap<String, Integer> tree = new TreeMap<>();
    boolean[] isDel = new boolean[50010];
    int cnt, start;
   
    void init() {
        cnt = 0;
        start = 0;
        tree.clear();
    }
   
    void loginID(char[] mID) {
        String id = String.valueOf(mID);
        if (tree.containsKey(id))
            isDel[tree.get(id)] = true;
        isDel[cnt] = false;
        tree.put(id, cnt++);
    }
   
    int closeIDs(char[] mStr) {
        String prefix = String.valueOf(mStr).trim();    // chuyen sang string
        int ret = 0;
         
        String key = tree.ceilingKey(prefix);        // lay gia tri theo key 
        while (key != null && key.startsWith(prefix)) {
            int k = tree.get(key);
            if (!isDel[k]) {
                isDel[k] = true;
                ret++;
            }
            key = tree.higherKey(key);
        }
        return ret;
    }
   
    void connectCnt(int mCnt) {
        int i = start;
        while (mCnt > 0) {
            if (!isDel[i]) {
                isDel[i] = true;
                --mCnt;
            }
            i++;
        }
        start = i;
    }
   
    int waitOrder(char[] mID) {
        int idx = tree.getOrDefault(String.valueOf(mID), -1); // trả về value theo key
        if (idx == -1 || isDel[idx])
            return 0;
        int ret = 0;
        for (int i = start; i <= idx; i++) {
            if (!isDel[i])
                ret++;
        }
        return ret;
    }
}