Sum of Absolute Differences in a Sorted Array - Leetcode 1685 - Python

  Рет қаралды 9,994

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 24
@aravindashokkumar5425
@aravindashokkumar5425 Жыл бұрын
Nice work! This problem cleverly uses what we know about prefixes and postfixes. Great job!
@arihantbedagkar7678
@arihantbedagkar7678 Жыл бұрын
Time: O(n) and Space: O(1) diff = sum(nums) for i, num in enumerate(nums): yield ((i
@shreehari2589
@shreehari2589 Жыл бұрын
Bro I was just solving this question, you uploaded this video, what a perfect timing
@sanooosai
@sanooosai 3 ай бұрын
thank your sir, learning a lot from you
@sauravsingh4497
@sauravsingh4497 Жыл бұрын
The saviour has arrived 🙏
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Will try to be consistent the rest of the year!
@sauravsingh4497
@sauravsingh4497 Жыл бұрын
@@NeetCodeIO i know there must be reason for not uploading so no pressure upload whenever you feel like it. Love your explaination ❤️
@soumyajitchatterjee5822
@soumyajitchatterjee5822 Жыл бұрын
Great Content as always.
@thor1626
@thor1626 Жыл бұрын
My java solution : class Solution { public int[] getSumAbsoluteDifferences(int[] nums) { int len = nums.length; int[] prefixSum = new int[len + 1]; prefixSum[0] = 0; int sum = 0; int index = 1; for (int i = 0; i < len; i++) { sum += nums[i]; prefixSum[index] = sum; index++; } for (int i = 0; i < len; i++) { int x = (prefixSum[len] - prefixSum[i]) - (nums[i] * (len - i)); int y = (nums[i] * i) - prefixSum[i]; nums[i] = x + y; } return nums; } }
@AbhishekMakwana-p1v
@AbhishekMakwana-p1v Жыл бұрын
Thankyou my niggahahahah loved your Solution it really helped ..may god make you a slave again in next life ...
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Uh thanks 😅
@leeroymlg4692
@leeroymlg4692 Жыл бұрын
wtf
@pastori2672
@pastori2672 Жыл бұрын
-Why does your code work but mine gets TLE they're almost identical: res = [0] * len(nums) s = sum(nums) prefix_sum = 0 postfix_sum = sum(nums) for i in range(len(nums)): postfix_sum -= nums[i] res[i] += postfix_sum - (len(nums[i + 1:]) * nums[i]) res[i] += (nums[i] * len(nums[:i])) - prefix_sum prefix_sum += nums[i] return res
@AbhishekMakwana-p1v
@AbhishekMakwana-p1v Жыл бұрын
you are using len operator on update list every time i mean you are using it on sliced array so it is calculated every single time loop runs and value of i gets updated every time ..when you use len(nums) without slicing it does not count length every time it is inbuily & more efficient thana o(N) ,,when you use slicing the len has to be calculated every time ...
@AbhishekMakwana-p1v
@AbhishekMakwana-p1v Жыл бұрын
by the i am a big fan of berserk ...griffith did nothing wrong i mean ...casca enjoyed it
@pastori2672
@pastori2672 Жыл бұрын
@@AbhishekMakwana-p1v ah i see
@pastori2672
@pastori2672 Жыл бұрын
@@AbhishekMakwana-p1v i'll touch you
@1vader
@1vader Жыл бұрын
​@@AbhishekMakwana-p1vI'm quite certain len is never "calculated"/computed. A list always stores its length. But the slicing is indeed the issue because it creates a whole new list with the relevant elements which ofc is an O(n) operation.
@shahwaliweb6505
@shahwaliweb6505 Жыл бұрын
Look to my code. nums = [2,3,5] new_list = [] count = 0 length_nums = len(nums) for i in range(length_nums ): for j in range(length_nums ): new_list.append(nums[count] - nums[j]) count += 1 positive_list = [abs(x) for x in new_list] result = [sum(positive_list[i:i+length_nums]) for i in range(0, len(positive_list), length_nums)] print(result)
@Vancha112
@Vancha112 Жыл бұрын
Yes this is code
@brahm_and_coding
@brahm_and_coding 9 ай бұрын
that's something man
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 167 М.
Minimum Time Difference - Leetcode 539 - Python
21:43
NeetCodeIO
Рет қаралды 9 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 16 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 55 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 100 МЛН
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 368 М.
Learning to code is WASTING your time
7:16
JCisWatching
Рет қаралды 18 М.
K-th Symbol in Grammar - Leetcode 779 - Python
10:19
NeetCodeIO
Рет қаралды 14 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,8 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 856 М.
Sum of Square Numbers - Leetcode 633 - Python
14:26
NeetCodeIO
Рет қаралды 13 М.
LeetCode 33. Search in Rotated Sorted Array
9:30
Nick White
Рет қаралды 99 М.
Is Functional Programming DEAD Already?
21:07
Continuous Delivery
Рет қаралды 72 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 16 МЛН