Skip to content
Open
Changes from all 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
70 changes: 70 additions & 0 deletions dsa-pros/leetcode/array/JavaScript/JuwonAdeyemi/ArrayWeek4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @param {number[]} nums
* @return {boolean}
*
* Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
*/
var containsDuplicate = function (nums) {
nums.sort();
for (let i = 0; i <= nums.length - 1; i++) {
if (nums[i] === nums[i + 1]) {
return true;
}
}
return false;
};

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*
* Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.


*/
var search = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] > nums[i + 1]) {
// Swap elements
var temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}

if (nums[i] === target) {
return i;
}
}
return -1; // Element not found
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Irene stated a condition that you missed here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the question requires that You must write an algorithm with O(log n) runtime complexity. This solution does not satisfy that requirement even though it works. You might want to check that out

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the question requires that You must write an algorithm with O(log n) runtime complexity. This solution does not satisfy that requirement even though it works. You might want to check that out

Okay
Done, Thank you

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution is O(n) time complexity. Look up Binary search and view some examples with various solutions as Mary suggested, will help a lot in understanding.


/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
* Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
*/
var rotate = function (nums, k) {
k %= nums.length; // actual number of steps to rotate
nums.unshift(...nums.splice(-k));
};

/**
* @param {number[]} nums
* @return {number}
* Given an integer array nums, return the length of the longest strictly increasing
subsequence
.
*/
var lengthOfLIS = function (nums) {
let count = 1;
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i + 1] > nums[i]) {
count++;
}
}
return count;
};