Concatenation of Array - Leetcode 1929 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
2:40 - Coding Explanation

leetcode 1929

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

finally an easy problem to boost my self confidence T_T

thespicycabbage
Автор

Another way is to create ans 2x len of nums with 0 as placeholder. Loop through nums and add the current value of nums to the corresponding index of ans and corresponding index ans + len of nums. So in the first iteration, we add the value at nums[0] to ans[0] and ans[0+len(nums)] which is ans[3] and so on. The time complexity improves to O(n) instead of O(n+n) lil difference.

the
Автор

Simple Sol:
def concatenation (nums, x):
return x * nums;

GoogleGamerIndia
Автор

Return nums + nums
Easy peasy
But your solution works better if there is a 3rd argument
Nice one 👍

piyusharyaprakash
Автор

Please do "Last day where you can still cross", even LC doesn't have an official solution yet on it

business_central
Автор

to see some easy problems solved!!! 🍻🍻🍻🍻

ingluissantana
Автор

return ( nums + nums ) is very pythonic. Instead we can directly use the formula given in the qn 😌

lavanya_m
Автор

Why did you gave up some of us were watching your videos daily

vishalroy
Автор

since its a nested loop though, wouldn't this be O(n^2)? Confused here, can anyone please help?

candylover
Автор

is only append is O(1) but append in the middle of the begining in list will be O(n) operation?

qazyhn
Автор

ans[i] = nums[i % len(nums)]

or ans.append and define the range of the for loop as 2 *len(nums)

mokoboko
Автор

class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# input: nums: number[] n1 = len(nums)
# output: ans: number[] n2 = 2 * n1 where ans[i] == nums[i] and ans[i + n1] == nums[i]
n = len(nums)
ans = [None] * (n * 2) # be aware that [None] * n * 2 is slower than [None] * (n * 2)
for idx in range(n):
ans[idx], ans[n + idx] = nums[idx], nums[idx] # two pointers O(n/2)

return ans

anthony_dracula
Автор

Mby extend with same nums works good ?

dusvn
Автор

Just return the list multiplied by 2 that is return nums * 2

gopro
Автор

var getConcatenation = function(nums) {
let ans = [];
for(i = 0; i < nums.length; i++){
ans[i] = nums[i];
ans[i+nums.length] = nums[i];
}
return ans;
}

1ms runtime is this good or bad and why? Thx

BennduR
Автор

why not just:

ans = nums * 2
return ans

muzaffergurersalan
Автор

basically just return nums + nums right?

turtlenekk
Автор

2444. Count Subarrays With Fixed Bounds plz solve this problem, I am stuck in it.

pritishbhakat
Автор

Try this.


def getConcatenation(nums):
n = len(nums)

for i in range(n):
nums.append( nums[i])

return nums

ShealImon
welcome to shbcf.ru