Skip to content

Update 283-Move-Zeroes.js #1596

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

Merged
merged 5 commits into from
Dec 28, 2022
Merged

Update 283-Move-Zeroes.js #1596

merged 5 commits into from
Dec 28, 2022

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Dec 24, 2022

Added solution with linear space just for reference's sake.

File(s) Modified: 283-Move-Zeroes.js
Language(s) Used: JavaScript

Added solution with linear space just for reference's sake.

for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
right--;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add continue an remove else branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aakhtar3, I updated the code, we don't need else or the continue part now.

Updated the logic. We don't need the "else" part nor do we require "continue".
const zeroAtTheEnd = Array(nums.length).fill(0);
let left = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should define your if conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aakhtar3, What do you mean by defining if conditions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Look at the Space O(1) if condition


const zeroAtTheEnd = Array(nums.length).fill(0);
let left = 0;
for (let i = 0; i < nums.length; i++) {
Copy link
Collaborator

@aakhtar3 aakhtar3 Dec 25, 2022

Choose a reason for hiding this comment

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

Most of the solutions for 2 pointer, sliding window, and cyclic sort are in while loop. Easier to remember that pattern over for loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @aakhtar3, check now. Is this ok?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's do this

const arr = new Array(nums.length).fill(0);

let [ left, right ] = [ 0, 0 ];

while (right < nums.length) {
	const isZero = (nums[right] === 0);
	if (!isZero) {
		arr[left] = nums[right];
		left++;
	}

	right++;
}

return arr;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That could work, let me try to submit it on leetCode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, shoot! we won't be able to because we're creating a new array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I did try some test cases and it seems to run just fine. I'll update the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check now @aakhtar3

Added a descriptive if condition.
Updated the code as suggested.
@@ -1,3 +1,29 @@
/**
* Linear Time
Copy link
Collaborator

Choose a reason for hiding this comment

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

Two Pointer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -1,3 +1,29 @@
/**
* Linear Time
* Time Complexity O(N) | Space Complexity O(N);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Time O(N) | Space O(N)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

updating solution description.
@aadil42
Copy link
Contributor Author

aadil42 commented Dec 27, 2022

@aakhtar3, I update the file with the suggested changes. Please have a look.

@aakhtar3 aakhtar3 merged commit 2e826d0 into neetcode-gh:main Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants