VERY COMMON FAANG INTERVIEW QUESTION | Leetcode 27 - Remove Element

preview_player
Показать описание
dynamic programming, leetcode, coding interview question, data structures, data structures and algorithms, faang
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

I thought I was kinda ready for an interview but I had no idea what he was talking about I need to do more learning 💀

Nazaref
Автор

Fantastic work, Greg! I'm hooked on what you do and consider myself a fan of your work

bssandeep
Автор

prebuilt functions were invented in 1956
people in 1955:

Free_Palestine
Автор

To those who didn’t get it, please study basic sorting algo like insertion sort, bubble sort, exertion sort etc

hit
Автор

Or you can just use two indices from the beginning

left = 0

for right in range(len(nums)):
if nums[right] == val: continue
nums[left] = nums[right]
left += 1

return left

rdubb
Автор

No need to swap the elements, just put the element at j as element at i: nums[i++]=nums[j--]

casper_hansen
Автор

We can keep i anf j at the zero th index move i and j by one position

Swap the values at i and j when i's value is not equivalent to the key

If the i's value is equivalent to key we stop incrementing the j untill i's value is equal to the key


Whrn i's value becomes not equivalent to key we then swap i's and j's value and increment i and j

_VETTRICHEZHIAN
Автор

what about this

index = 0

for i in range(len(nums)):
if nums[i] != val:
nums[index] = nums[i]
index += 1

return index

moe
Автор

Given the question. Isn’t the best approach is to keep chucking 3 to the end no matter what as that is the item to push to the end. Why do you have to have this 3 before that 3 or the other way.

sudeepranganath
Автор

int removeElement(int* nums, int numsSize, int val) {
int head = 0;
for (int i = 0; i < numsSize; i++)
if (nums[i] != val)
nums[head++] = nums[i];
return head;
}

beats 100% on Leetcode

simonbystricky
Автор

It's just like move zeroes to the end

ramandeepsingh
Автор

This is another javascript solution, if you want better benchmark just write up a swap function to swap elements.


function removeElement(nums: number[], val: number): number {
let left = 0, right = nums.length - 1;

while (left <= right) {
if (nums[left] === val) {
[nums[left], nums[right]] = [nums[right], nums[left]];
right -= 1;
} else {
left += 1;
}
}

return left;
}

zFGiveNLATAM
Автор

// My solution in JavaScript

// no order save
function removeElement(nums, val) {
let l = 0,
n = nums.length,
r = n - 1;
while (l <= r) {
[nums[l], nums[r]] = [nums[r], nums[l]];
while (nums[l] != val && ++l <= r);
while (nums[r] == val && l <= --r);
}
return l;
}

// save order
function removeElement(nums, val) {
let l = 0,
n = nums.length,
r = 1;
while (r < n) {
[nums[l], nums[r]] = [nums[r], nums[l]];
while (nums[l] != val && ++l < n);
while (nums[r] == val && ++r < n);
}
return l;
}

ydno
Автор

Hmm. Couldn’t you just replace every occurrence of Val with -1, then sort the array in reverse, and return the number of val occurrences you replaced? This would return the non-Val elements in the array, and all the -1’s would be at the end. O(nlogn) time, space complexity is O(1), or O(n) if you consider sorting to use an auxiliary array. I submitted this and it worked. Thoughts?

hyperboliq
Автор

this solution is valid but over-complicated.

zangdaarrmortpartout
Автор

You got the question wrong and therefore your solution is bloated.
In fact you need to have 2 pointers, but going the same way.
Something like this:
int tail=0;
for(i=0;i<nums.size;i++){
if(nums[i]!=val) {
if(tail!=i) nums[tail]=nums[i];
tail++;
}
}
return tail;

millfreedom
Автор

list1 = [2, 2, 3, 4, 3, 2, 4]
print(list1)
list1=[x for x in list1 if x!=2]+[x for x in list1 if x==2]
print(list1) is it correct? please tell

amogh_bakhle
Автор

ok, he blocked off part of the description with the cartoon animal. so i missed that

mazthespaz
Автор

why did he say two indexes instead of 2 pointers?

belphegor
welcome to shbcf.ru