anoldvozer1710.v2
Senior Member
Bài key-value kia cũng dùng l < r được mà thím ơi. E vừa mới ăn xong giờ mới gửi lại đcBài này giải l < r là đúng rồi bác
https://leetcode.com/problems/time-based-key-value-store/description/
Bác thử giải bài này với template kia xem. Xưa giờ mình toàn xài c# để kiếm ăn thôi nên lúc nào cũng giải bằng C#
Hôm qua bài koko này xem NeetCode nó viết solution này thì pass test case mà mình convert qua C# lại chỉ pass 124/125
Python:View on Github class Solution: def minEatingSpeed(self, piles: List[int], h: int) -> int: l, r = 1, max(piles) res = max(piles) while l <= r: k = (l + r) // 2 totalTime = 0 for p in piles: totalTime += math.ceil(p / k) if totalTime <= h: res = min(res, k) r = k - 1 else: l = k + 1 return res

JavaScript:
type Pair = [number, string];
class TimeMap {
map: Map<string, Pair[]>
constructor() {
this.map = new Map();
}
set(key: string, value: string, timestamp: number): void {
if (!this.map.has(key)) {
this.map.set(key, []);
}
const val = this.map.get(key);
val.push([timestamp, value])
}
get(key: string, timestamp: number): string {
if (!this.map.has(key)) return '';
if (timestamp < this.map.get(key)[0][0]) return ''
let l = 0, r = this.map.get(key).length;
while (l < r) {
let m = (l + r) >> 1;
if (this.map.get(key)[m][0] > timestamp) {
r = m;
} else {
l = m + 1;
}
}
if (r === 0) {
return "";
}
return this.map.get(key)[r - 1][1];
}
}
/**
* 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)
*/







lăn tăn gì ko apply cty khác

