Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create 4-Median-Of-Two-Sorted-Arrays.js, 981-Time-Based-Key-Value-Store.js#418

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 2 commits intoneetcode-gh:mainfromAhmad-A0:main
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletionsjavascript/4-Median-Of-Two-Sorted-Arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
let [A, B] = [nums1, nums2]
const total = nums1.length + nums2.length
const half = Math.floor(total / 2)

if (B.length < A.length) {
[A, B] = [B, A]
}

let [l, r] = [0, A.length -1]
while (true) {
const i = (l + r)
const j = half - i - 2

const Aleft = i >= 0 ? A[i] : -Infinity
const Aright = (i + 1) < A.length ? A[i + 1] : Infinity
const Bleft = j >= 0 ? B[j] : -Infinity
const Bright = (j + 1) < B.length ? B[j + 1] : Infinity

if (Aleft <= Bright && Bleft <= Aright) {
if (total % 2) {
return Math.min(Aright, Bright)
}

return (Math.max(Aleft, Bleft) + Math.min(Aright, Bright)) / 2
}
else if (Aleft > Bright) r = i - 1
else l = i + 1
}
};

44 changes: 44 additions & 0 deletionsjavascript/981-Time-Based-Key-Value-Store.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
var TimeMap = function() {
this.keyStore = {}
};

/**
* @param {string} key
* @param {string} value
* @param {number} timestamp
* @return {void}
*/
TimeMap.prototype.set = function(key, value, timestamp) {
if (!this.keyStore[key]) this.keyStore[key] = []
this.keyStore[key].push([value, timestamp])
};

/**
* @param {string} key
* @param {number} timestamp
* @return {string}
*/
TimeMap.prototype.get = function(key, timestamp) {
let res = ""
const values = this.keyStore[key] || []
let l = 0, r = values.length -1

while (l <= r) {
const m = Math.floor((l + r) / 2)
if (values[m][1] <= timestamp) {
res = values[m][0]
l = m + 1
}
else r = m - 1
}

return res
};

/**
* 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)
*/


[8]ページ先頭

©2009-2025 Movatter.jp