-
Notifications
You must be signed in to change notification settings - Fork 6
Week 4 Array #24
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
Open
Samju200
wants to merge
3
commits into
main
Choose a base branch
from
week-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Week 4 Array #24
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
dsa-pros/leetcode/array/JavaScript/JuwonAdeyemi/ArrayWeek4.js
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
| 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 | ||
| }; | ||
|
|
||
| /** | ||
| * @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; | ||
| }; | ||
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 outThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
Done, Thank you
There was a problem hiding this comment.
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.