Problem
You are given an array nums
consisting of positive integers.
Starting with score = 0
, apply the following algorithm:
- Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
- Add the value of the chosen integer to
score
. - Mark the chosen element and its two adjacent elements if they exist.
- Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
Solution
Video explanation
Here is the video explaining this method in detail. Please check it out:
Method 1 - Naive way to finding min element in array
Here is the approach:
- Initial Setup:
- Start with a score (
score = 0
). - Use a boolean array to keep track of which elements are marked.
- Start with a score (
- Iteration:
- Continuously search for the smallest unmarked integer in the array.
- Add the value of this integer to
score
. - Mark this integer and its adjacent elements (if they exist) as processed.
- Repeat the steps until all elements are marked.
- Termination:
- The process stops when all elements in the
nums
array are marked.
- The process stops when all elements in the
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- Finding the smallest unmarked element in each iteration takes
O(n)
. - In the worst case, this operation is repeated
n
times, leading to a total time complexity ofO(n^2)
.
- Finding the smallest unmarked element in each iteration takes
- 🧺 Space complexity:
O(n)
- We use an additional boolean array of size
n
to keep track of marked elements, making the space complexityO(n)
.
- We use an additional boolean array of size
Method 2 - Using Minheap
The key observation for an efficient solution is that we need to repeatedly find the smallest unmarked element and mark it along with its adjacent elements. Using a min-heap (priority queue) allows us to efficiently retrieve the smallest element in logarithmic time. By marking adjacent elements immediately upon processing the smallest element, we ensure that these elements are not reconsidered in future iterations, preserving the constraint of marking and managing all elements efficiently.
Here is the approach:
- Initial Setup:
- Create a priority queue (min-heap) and insert each element of the array along with its index into the heap.
- Maintain a boolean array to keep track of which elements are marked.
- Processing Elements:
- Continuously extract the smallest element from the heap.
- If the extracted element is already marked, skip it.
- If it is unmarked, add its value to the score and mark it.
- Additionally, mark its adjacent elements (if they exist).
- Termination:
- The process stops when all elements of the array are either extracted from the heap or marked.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- Inserting all elements into the min-heap initially takes
O(n log n)
. - Each extraction and insertion operation in the heap is
O(log n)
. - Given that each element is processed once, the total time complexity for heap operations is
O(n log n)
.
- Inserting all elements into the min-heap initially takes
- 🧺 Space complexity:
O(n)