Search in Rotated Sorted Array#26
Open
prathyu116 wants to merge 21 commits intoWeCode-Community-Dev:mainfrom
Open
Search in Rotated Sorted Array#26prathyu116 wants to merge 21 commits intoWeCode-Community-Dev:mainfrom
prathyu116 wants to merge 21 commits intoWeCode-Community-Dev:mainfrom
Conversation
added 21 commits
March 18, 2025 13:17
const arr = []
const prefixArr = []
const suffixArr = []
for (let i = 0; i < nums.length; i++) {
if (i === 0) {
prefixArr[i] = 1
} else {
prefixArr[i] = prefixArr[i - 1] * nums[i - 1]
}
}
for (var i = nums.length - 1; i >= 0; i--) {
if (i === nums.length - 1) {
suffixArr[i] = 1
} else {
suffixArr[i] = suffixArr[i + 1] * nums[i + 1]
}
}
for (let i = 0; i < nums.length; i++) {
arr[i] = prefixArr[i] * suffixArr[i]
};
return arr
}
console.log(productExceptSelf([1, 2, 3, 4])); // [24, 12, 8, 6]
//
/*
rightProduct__index[i]___leftProduct
then rightProduct*leftProduct
https://youtu.be/TW2m8m_FNJE
*/
var s = 0, e = nums.length - 1;
var res = nums[0];
while (s <= e) {
if(nums[s] < nums[e]) {
res = nums[s];
break
}
var mid = Math.floor((s + e) / 2);
res = Math.min(res, nums[mid]);
if (nums[s] <= nums[mid]) { //left sort
s = mid + 1; //got to right
} else {
e = mid;
}
}
return res;
};
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.