Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
2
Indexable
Never
class UserSolution {

    static Person[][] mapHead = new Person[6][6];

    public void init() {
        for (int team = 1; team <= 5; team++) {
            for (int score = 1; score <= 5; score++) {
                mapHead[team][score] = new Person();
            }
        }
    }

    void insertFirst(Person soldier, int score) {
        soldier.next = mapHead[soldier.mTeam][score].next;
        mapHead[soldier.mTeam][score].next = soldier;
    }

    public void hire(int mID, int mTeam, int mScore) {
        Person soldier = new Person(mID, mTeam, mScore);
        insertFirst(soldier, mScore);
    }

    public void fire(int mID) {
        Person soldier = listSoildier[mID];
        soldier.prev.next = soldier.next;
        soldier.next.prev = soldier.prev;
        soldier.mID = -1;
        soldier.mTeam = -1;
        soldier.mScore = -1;
    }

    public void updateSoldier(int mID, int mScore) {
        Person soldier = listSoildier[mID];
        soldier.prev.next = soldier.next;
        soldier.mScore = mScore;
        insertFirst(soldier, mScore);
    }

    public void updateTeam(int mTeam, int mChangeScore) {
        // Tương tự như trước, có thể giữ nguyên logic cập nhật team.
    }

    public int bestSoldier(int mTeam) {
        for (int score = 5; score >= 1; score--) {
            Person soldier = mapHead[mTeam][score].next;
            if (soldier != null && soldier.mID != -1) {
                return soldier.mID;
            }
        }
        return -1;
    }
}

class Person {
    int mTeam;
    int mScore;
    int mID;
    Person next;
    Person prev;

    public Person() {
        mID = -1;
        mTeam = -1;
        mScore = -1;
        next = prev = null;
    }

    public Person(int ID, int mTeam, int mScore) {
        this.mID = ID;
        this.mTeam = mTeam;
        this.mScore = mScore;
        next = prev = null;
    }
}
Leave a Comment