thetwoface's blog

By thetwoface, history, 9 months ago, In English

The constraints are 1<n<=10⁵ and a[i]<=10⁹ Now you are given an array a. You task is to find minimum in the current sized array and remove that element and it's adjacent elements. The cost of this operation will be the minimum element choosen. Repeat this step till array becomes empty. Mathematically, for any i within array limit such that a[i] is minimum of array a, then remove a[i] and if i-1 exists remove a[i-1] and if i+1 exists a[i+1], the cost will be cost+=a[i] , the array will become {a1, a2,...ai-2,ai+2,....an} again find minimum and repeat the same till array becomes empty. NOTE: ELEMENTS NEED NOT TO BE UNIQUE. IF THERE ARE MULTIPLE MINIMIUMS CHOOSE THE ONE WITH LOWEST INDEX

  • Vote: I like it
  • -6
  • Vote: I do not like it

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by thetwoface (previous revision, new revision, compare).

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What if multiple minimum exists in the array eg {2,3,6,3,4,2,2} , here min. 2 appears 3 times , do we have to remove from all occurances and its adjacent elements or do we have to choose anyone of them . Another doubt , is it mentioned in the problem that all the elements in the array are unique ?

  • »
    »
    9 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    If multiple minimums exists choose the one with the lowest index.

    • »
      »
      »
      9 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      Edit :- I now realized my soln doesn't actually shift the element after deletions hence it won't give correct result ,although someone commented the correct soln. using doubly linked list below.

      solved using segtree
      • »
        »
        »
        »
        9 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Is this correct! Well i dun think, cause I tried implementing it using segment tree but it doesn't shift indices as you mentioned.

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by thetwoface (previous revision, new revision, compare).

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
Sorting + Doubly Linked List
  • »
    »
    9 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    Is the linked list actually necessary? I believe this would be equivalent:

    Code

    EDIT: Oh, my mistake. I didn't realize the elements need to be shifted. Sorry about that.

    However, if the constraint about "lowest index" did not exist, the problem would be a little bit harder to solve.

    • »
      »
      »
      9 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      This won't work as after every operation, the array will shrink so the indices. Hope this makes sense.

»
9 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

apologies if im misreading but can we not just

Spoiler
  • »
    »
    9 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    No the min Heap approach won't work. The logic behind is if you use min heap/ priority queue still you can't update indices, as array shrinks indices will change.

    • »
      »
      »
      9 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      yeah i just noticed i edited it

      • »
        »
        »
        »
        9 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        I think jdoe73x has given the promising code. you could check this out in the initial comments of this blog.

        • »
          »
          »
          »
          »
          9 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          nah I'm trying to solve it myself, but could you provide some polygon package or send link to leetcode (if the OA is taken off lc) so I could submit, im pretty sure of the DSU idea

          • »
            »
            »
            »
            »
            »
            9 months ago, hide # ^ |
            Rev. 2  
            Vote: I like it 0 Vote: I do not like it

            You can generate random numbers and try for array of size 600000. If it works then fine. But remember the problem conditions.

            • »
              »
              »
              »
              »
              »
              »
              9 months ago, hide # ^ |
              Rev. 2  
              Vote: I like it 0 Vote: I do not like it

              thats not the issue, the issue is checking it

              also here is my general implementation

              Spoiler
              • »
                »
                »
                »
                »
                »
                »
                »
                9 months ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                Check for a={10 4 8 3 1 5 8 3 1 6 2 5 1 7} the answer is 9.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  9 months ago, hide # ^ |
                   
                  Vote: I like it 0 Vote: I do not like it

                  Thanks i think i got it, here

                  Spoiler
              • »
                »
                »
                »
                »
                »
                »
                »
                9 months ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                please ignore the me forgetting to take input LOL