Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2022-10-11 #74

Open
github-actions bot opened this issue Oct 10, 2022 · 1 comment
Open

2022-10-11 #74

github-actions bot opened this issue Oct 10, 2022 · 1 comment

Comments

@github-actions
Copy link

No description provided.

@gongpeione
Copy link
Contributor

981 Time Based Key-Value Store

/*
 * @lc app=leetcode id=981 lang=typescript
 *
 * [981] Time Based Key-Value Store
 */

// @lc code=start
class TimeMap {
    store = {} as { [index: string]: [string, number][] }
    constructor() {

    }

    set(key: string, value: string, timestamp: number): void {
        const val = [value, timestamp] as [string, number];
        if (!this.store[key]) {
            this.store[key] = [val];
        } else {
            this.store[key].push(val);
        }
    }

    get(key: string, timestamp: number): string {
        let ans = '';
        const val = this.store[key];

        let left = 0;
        let right = val.length - 1;

        while (left <= right) {
            const middle = ~~((left + right) / 2);
            if (val[middle][1] <= timestamp) {
                // if current timestamp is greater than the middle element's timestamp
                // then we can set it to answer becasue we need to return the value
                // associtated with the largest
                ans = val[middle][0];
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }

        return ans;
    }
}

/**
 * Your TimeMap object will be instantiated and called as such:
 * var obj = new TimeMap()
 * obj.set(key,value,timestamp)
 * var param_2 = obj.get(key,timestamp)
 */
// @lc code=end

Nickname: Geeku
From vscode-hzfe-algorithms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant