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

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 24. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    đŸ§‘â€đŸ’Œ LinkedIn: / navdeep-singh-3aaa14161
    đŸ„· Discord: / discord
    🐩 Twitter: / neetcode1
    🐼 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: ‱ Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: ‱ House Robber - Leetco...
    Problem Link: leetcode.com/problems/sum-of-...
    0:00 - Read the problem
    0:30 - Drawing Explanation
    8:20 - Coding Explanation
    leetcode 1685
    #neetcode #leetcode #python

Komentáƙe • 23

  • @aravindashokkumar5425
    @aravindashokkumar5425 Pƙed 8 měsĂ­ci +1

    Nice work! This problem cleverly uses what we know about prefixes and postfixes. Great job!

  • @shreehari2589
    @shreehari2589 Pƙed 8 měsĂ­ci +3

    Bro I was just solving this question, you uploaded this video, what a perfect timing

  • @soumyajitchatterjee5822
    @soumyajitchatterjee5822 Pƙed 8 měsĂ­ci

    Great Content as always.

  • @arihantbedagkar7678
    @arihantbedagkar7678 Pƙed 8 měsĂ­ci

    Time: O(n) and Space: O(1)
    diff = sum(nums)
    for i, num in enumerate(nums):
    yield ((i

  • @sauravsingh4497
    @sauravsingh4497 Pƙed 8 měsĂ­ci +3

    The saviour has arrived 🙏

    • @NeetCodeIO
      @NeetCodeIO  Pƙed 8 měsĂ­ci +2

      Will try to be consistent the rest of the year!

    • @sauravsingh4497
      @sauravsingh4497 Pƙed 8 měsĂ­ci +1

      @@NeetCodeIO i know there must be reason for not uploading so no pressure upload whenever you feel like it.
      Love your explaination ❀

  • @thor1626
    @thor1626 Pƙed 8 měsĂ­ci

    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;
    }
    }

  • @user-mh4lq5ce6u
    @user-mh4lq5ce6u Pƙed 8 měsĂ­ci

    Thankyou my niggahahahah loved your Solution it really helped ..may god make you a slave again in next life ...

  • @pastori2672
    @pastori2672 Pƙed 8 měsĂ­ci

    -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

    • @user-mh4lq5ce6u
      @user-mh4lq5ce6u Pƙed 8 měsĂ­ci

      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 ...

    • @user-mh4lq5ce6u
      @user-mh4lq5ce6u Pƙed 8 měsĂ­ci

      by the i am a big fan of berserk ...griffith did nothing wrong i mean ...casca enjoyed it

    • @pastori2672
      @pastori2672 Pƙed 8 měsĂ­ci

      @@user-mh4lq5ce6u ah i see

    • @pastori2672
      @pastori2672 Pƙed 8 měsĂ­ci

      @@user-mh4lq5ce6u i'll touch you

    • @1vader
      @1vader Pƙed 8 měsĂ­ci +1

      ​@@user-mh4lq5ce6uI'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 Pƙed 8 měsĂ­ci +1

    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)