GOOGLE MOST ASKED QUESTION 2021 - Maximum Points you can Obtain from Cards - Leetcode 1423 - Python

Поділитися
Вставка
  • Опубліковано 16 вер 2024

КОМЕНТАРІ • 47

  • @shivaprasad8919
    @shivaprasad8919 Рік тому +10

    Who and all directly jumped into recursive solution(there were decisions to take either left or right) like me??

  • @atift5465
    @atift5465 2 роки тому +8

    This is such a simple solution but so difficult to think of! Jeez. Thank you!

  • @mohamedshihabaaqil6280
    @mohamedshihabaaqil6280 2 роки тому +6

    Java solution:
    class Solution {
    public int maxScore(int[] cardPoints, int k) {
    int len = cardPoints.length;
    int l = 0, r = len - k;
    int total = 0;
    for(int i = r ; i < len; i++)
    total += cardPoints[i];
    int res = total;
    while(r < len)
    {
    total += (cardPoints[l] - cardPoints[r]);
    res = Math.max(res, total);
    l++;
    r++;
    }
    return res;
    }
    }

  • @sumathia.8798
    @sumathia.8798 3 роки тому +8

    One of the clearest explanations I've seen! Thank you so much

  • @aryanyadav3926
    @aryanyadav3926 2 роки тому +5

    Wonderful explanation! Thank you!
    I always search for Neetcode's video while looking for solution of a coding problem.

    • @orangefield2308
      @orangefield2308 11 місяців тому

      awesome work, why is this categorized under Greedy? It should be sliding window on Neetcode page, thanks

  • @TechOnScreen
    @TechOnScreen 2 роки тому +2

    my easy solution to this question.
    initially i just consider first k integers from left.
    and then start deleting on element from left and adding one element to right side sum.
    and just check which is max sum..
    java code below.
    class Solution {
    public int maxScore(int[] cardPoints, int k) {

    int len=cardPoints.length;
    int sum=findsum(cardPoints,k);
    int maxSum=Math.max(0,sum);

    for(int i=1;i

  • @JW-bx4su
    @JW-bx4su 3 роки тому +2

    If the length of the cards is n, you need to slide the window n-k times. So the time complexity is O(n) when n is bigger than k. When k^2

    • @vamsikrishnagannamaneni912
      @vamsikrishnagannamaneni912 7 місяців тому +1

      Dont think so, what ever is the size of the cards , we need to shift the slider k times, so its O(k)

  • @momozkichutney
    @momozkichutney 2 роки тому +2

    Hey neetcode, if I use prefix and suffix sum to optimise the brute force approach it may work in O(n), but with 3 passes

  • @kobo3344
    @kobo3344 2 роки тому

    The explanantion was detailed and clear. Amazing!! Thank you!

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk 2 роки тому +1

    Wow came here struggling with this question and you said this is super easy for Google :( I have my interview on the 15th fingers crossed and thank you for a great explanation

  • @XYZ-nz5gm
    @XYZ-nz5gm 2 роки тому +2

    brute force is 2^k no?

    • @lisali5719
      @lisali5719 2 роки тому

      Yea that’s what I thought

  • @akhileshshahare
    @akhileshshahare 2 роки тому

    This is so clean and easy to understand. Thank you!

  • @siqb
    @siqb 3 роки тому +2

    For me it was a hard problem to solve since the key is to see the array as cyclical. I missed that. I went ahead and did a DP:
    def maxScore(cards, k):
    if k == 1:
    return max(cards[0], cards[-1])
    @functools.lru_cache(maxsize=None)
    def dfs(l, r):
    if l + abs(r) - 1 == k:
    return 0
    left = cards[l] + dfs(l + 1, r)
    right = cards[r] + dfs(l, r - 1)
    return max(left, right)
    return dfs(0, -1)
    which works just fine but is no match to the time (and space) complexity of your solution. I guess for me now the problems that DONT fit neatly in the standard categories (trees, dyn. prog etc.) are harder to solve bec. they sneak up on you.

    • @EshwarNorthEast
      @EshwarNorthEast 3 роки тому +1

      How is this dp? Looks like normal recursion to me

    • @42611628
      @42611628 2 роки тому

      There are a couple of problems like Stone Game that perform well with this approach, I guess the fact that it is only you that is selecting i.e. there isn't another person a clue that we need to think differently ? I did what you did only to TLE

  • @42611628
    @42611628 2 роки тому

    @NeetCode , there are a couple of problems such as Stone Game that perform well with the left & right pointer approach, simulating the entire process in a way.
    The fact that it is only you that is selecting i.e. there isn't another person involved in selecting the cards, is that a clue that we need to think differently
    ? I used left and right pointers with a cache using (l,r) as key, it TLE'd. Would like to know if you thought of that approach as well, trying to understand the intuition behind sliding window :) Great work btw :)

    • @joya9785
      @joya9785 2 роки тому

      I used left and right pointers as well but when there's [2,2,2] and k=2 I'm getting wrong o/p. idk I'm literally stuck there and everyone's using sliding window that I've no idea of😅

  • @xueli5641
    @xueli5641 2 роки тому

    Great explanation! Thank you Neetcode!

  • @Your_Boy_Suraj
    @Your_Boy_Suraj Рік тому

    Using Prefix Sum array:
    class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
    forwardPrefixSum = [None] * len(cardPoints)
    backwardPrefixSum = [None] * len(cardPoints)
    summ = 0
    for i in range(len(cardPoints)):
    summ += cardPoints[i]
    forwardPrefixSum[i] = summ
    reversedCardPoints = cardPoints[::-1]
    summ = 0
    for i in range(len(cardPoints)):
    summ += reversedCardPoints[i]
    backwardPrefixSum[i] = summ
    maxx = max(forwardPrefixSum[k-1], backwardPrefixSum[k-1])
    for i in range(1, k):
    temp = k - i
    t1 = forwardPrefixSum[i-1]
    t2 = backwardPrefixSum[temp-1]
    print(t1, end=" ")
    print(t2)
    summ = t1 + t2
    maxx = max(maxx, summ)
    return maxx

  • @md.marufurrahmanremon3095
    @md.marufurrahmanremon3095 2 роки тому

    Your explainations are so good...Thanks...

  • @geekydanish5990
    @geekydanish5990 2 роки тому

    This solution does use extra k space but I feel its more intuitive
    class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
    left_card_sum = [0]*(k+1)
    right_card_sum = [0]*(k+1)
    max_score = 0
    _sum_so_far = 0
    for i in range(1,k+1):
    _sum_so_far += cardPoints[i-1]
    left_card_sum[i] = _sum_so_far
    _sum_so_far = 0
    for i in range(1,k+1):
    _sum_so_far += cardPoints[len(cardPoints) - i]
    right_card_sum[i] = _sum_so_far
    for i in range(len(right_card_sum)):
    max_score = max(max_score,left_card_sum[i]+right_card_sum[len(right_card_sum)-1-i])
    return max_score

    • @zorth4729
      @zorth4729 Рік тому

      There's a lack of good formatting here and introducing extra loops makes this way more ambiguous than it has to be. This solution takes the sum of the elements outside of a sliding window, a well-known concept, whereas this just feels spaghettified.

  • @amogchandrashekar8159
    @amogchandrashekar8159 3 роки тому +2

    which is the other question? Great explaination. Thanks :)

    • @NeetCode
      @NeetCode  3 роки тому +2

      "Guess the Word". It's a weird question for leetcode, it would probably be better with a real interviewer where you could discuss the solution with someone

    • @amogchandrashekar8159
      @amogchandrashekar8159 3 роки тому

      @@NeetCode I have my interview coming up soon, all thanks to you!

  • @TheProximator
    @TheProximator 3 роки тому +1

    Excellent :) Thanks a lot

  • @lali.p7951
    @lali.p7951 2 роки тому

    Wow! that was a really nice explanation! Thank you! 🎉

  • @ayeshaadhikari6123
    @ayeshaadhikari6123 2 роки тому

    well explained! Thanks a lot

  • @aishwaryaranghar3385
    @aishwaryaranghar3385 2 роки тому

    thank you!

  • @krateskim4169
    @krateskim4169 2 роки тому

    a very beautiful solution

  • @jonaskhanwald566
    @jonaskhanwald566 3 роки тому

    Not so efficient. But A noob's first intuition. Prefix sum
    class Solution:
    def maxScore(self, arr: List[int], k: int) -> int:
    n = len(arr)
    arr = arr*2
    start = n-k
    res = 0
    for i in range(1,len(arr)):
    arr[i] += arr[i-1]
    for i in range(start,n+1):
    ans = arr[i+k-1] - arr[i-1]
    res = max(res,ans)
    return res

  • @ttttully
    @ttttully 3 роки тому

    Great problem.

  • @rajsuriyang3427
    @rajsuriyang3427 9 місяців тому

    I thought this problem was Dynamic programming. I would've failed the interview😥.

  • @TheElementFive
    @TheElementFive 3 роки тому +1

    Very clever solution. I recreated something similar (in Java) and despite getting 0ms when I hit “run code” I keep getting time limit exceeded after trying to submit it. What gives??

  • @mohamedshihabaaqil6280
    @mohamedshihabaaqil6280 2 роки тому

    It will be better if the solution is available for a java too.

  • @jyothi9082
    @jyothi9082 2 роки тому

    Which is the other most frequently asked google question?

  • @dheerajsharma5492
    @dheerajsharma5492 2 роки тому

    i thought of recursion

  • @edwardteach2
    @edwardteach2 2 роки тому

    U a God

  • @scchouhansanjay
    @scchouhansanjay 2 роки тому

    🤯