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








Auto comment: topic has been updated by thetwoface (previous revision, new revision, compare).
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 ?
If multiple minimums exists choose the one with the lowest index.
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.
Is this correct! Well i dun think, cause I tried implementing it using segment tree but it doesn't shift indices as you mentioned.
Auto comment: topic has been updated by thetwoface (previous revision, new revision, compare).
Is the linked list actually necessary? I believe this would be equivalent:
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.
This won't work as after every operation, the array will shrink so the indices. Hope this makes sense.
apologies if im misreading but can we not just
use a priority queue and a visited array?
with the pq we can do pair<int,int> index, value and do custom comparator
then we set that i, i+1 and i-1 to visited (if we can) on array, and if our pq top is on a visited i we keep checking top and popping until its not
additionally to find left and right we can use indexed set (pdbs) or just segtree walk does this not work?
also just thought of this but you can also use dsu when checking for already visited neighbors around it and holding a l and r value for the block of visited, then you check l-1 and r+1 for the left and right find
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.
yeah i just noticed i edited it
I think jdoe73x has given the promising code. you could check this out in the initial comments of this blog.
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
You can generate random numbers and try for array of size 600000. If it works then fine. But remember the problem conditions.
thats not the issue, the issue is checking it
also here is my general implementation
Check for a={10 4 8 3 1 5 8 3 1 6 2 5 1 7} the answer is 9.
Thanks i think i got it, here
please ignore the me forgetting to take input LOL
still wont work i guess