通过用它们的绝对最大值替换相邻的相反符号对来减少数组
给定一个大小为N的数组arr[] ,如果两个符号相反的元素相邻,任务是通过重复执行以下操作来找到最终数组:
- 从数组中删除两个相反的符号元素,并插入具有最大绝对值的元素及其符号。
- 如果两个元素具有相同的绝对值,则两者都将从数组中删除。
例子:
Input: arr[] = {10, -5, -8, 2, -5}
Output: 10
Explanation :
At Index 0 : Element 10 has positive sign.
At Index 1 : -5 has lesser absolute value than 10. Replace both of them with 10.
At Index 2 : -8 has lesser absolute value than 10. Replace both of them with 10.
At Index 3 : 2 has positive sign. So it will be in the array.
At Index 4 : -5 has greater absolute value than 2. Replace both of them with 5.
Now -5 has lesser absolute value than 10. Replace both of them with 10.
Input: arr[] = {5, -5, -2, -10}
Output: -2 -10
Explanation: 1st and 2nd element gets discarded because
both elements have same values but opposite sign.
3rd and 4th elements have same sign. So both will be in the array.
方法:可以通过以下思路解决问题:
At any moment the previous elements can also be required, so a Stack data structure can be used to hold the elements, and smaller elements can be popped efficiently from the stack due to its last-in-first-out property.
请看下图以获得更好的理解:
Consider array arr[] = {10, -5, -8, 2, -5}.
Initially stack is empty, st = { }
At 0th index:
=> arr[0] = 10
=> st = {}
=> Push 10 into the stack
=> The stack is st = {10}
At 1st index:
=> arr[1] = -5
=> st = {10}
=> The top most element of stack is positive and arr[1] is negative.
=> As arr[1] has lesser absolute value i.e. 5 than top most element of stack so no changes in stack.
=> The stack is st = {10}
At 2nd index:
=> arr[2] = -8
=> st = {10}
=> The top most element of stack is positive and arr[2] is negative.
=> As arr[2] has lesser absolute value i.e. 8 than top most element of stack so no changes in stack.
=> The stack is st = {10}
At 3rd index:
=> arr[3] = 2
=> The top most element of stack is positive and arr[3] is also positive.
=> Push 2 in the stack.
=> The stack is st = {10, 2}
At 4th index:
=> arr[4] = -5
=> st = {10, 2}
=> The top most element of stack is positive and arr[4] is negative.
=> As arr[4] has greater absolute value i.e. 5 than top most element of stack, pop the top most element of stack.
=> The stack changes from st = {10, 2} to st = {10}
=> Now again, the top most element of stack is positive and arr[4] is negative.
=> arr[4] has lesser absolute value i.e. 5 than top most element. So no changes in stack.
=> The stack remains st = {10}
The elements finally remaining in the stack are the final elements of the array.
So the elements remaining in the array are arr = {10}
按照下面提到的步骤来实施该方法:
- 声明一个堆栈来保存数组元素。
- 遍历数组,如果元素为正,直接压栈。
- 否则,如果当前 arr[i] 为负,则
- 尝试从堆栈中弹出所有为正的较小元素,说明绝对值较小的元素已被丢弃。
- 如果当前元素和栈顶相等且栈顶为正,则从栈中弹出,说明两个值相等但符号相反的元素已被丢弃。
- 最后,如果堆栈为空或最后一个元素为负数,则将当前arr[i]元素压入堆栈。因为所有剩余的元素都会有一个负号。
- 最后,返回堆栈,显示剩余的元素。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
class Solution {
public:
// Function to find the remaining elements
vector solve(vector& arr)
{
// Stack to store elements
vector st;
// Traverse entire array
for (auto element : arr) {
// If positive element,
// directly push
if (element >= 0)
st.push_back(element);
else {
// Pop all the smaller elements
// moving in the right direction
while (st.size() > 0 && st.back() >= 0
&& abs(element) > st.back())
st.pop_back();
// Top of stack and current
// element same value and top of
// stack moving in right direction
if (st.size() > 0 && st.back() >= 0
&& st.back() == abs(element))
st.pop_back();
// No more elements remaining or
// remaining elements
// moving in left direction
else if (st.size() == 0 || st.back() < 0)
st.push_back(element);
}
}
// Finally return stack
return st;
}
};
// Driver code
int main()
{
Solution obj;
vector arr = { 5, -5, -2, -10 };
vector ans = obj.solve(arr);
for (auto x : ans)
cout << x << " ";
return 0;
}
// This code is contributed by rakeshsahni
Python3
# Python code to implement the approach
class Solution:
# Function to find the remaining elements
def solve(self, arr):
# Stack to store elements
stack = []
# Traverse entire array
for element in arr:
# If positive element,
# directly push
if (element >= 0):
stack.append(element)
else:
# Pop all the smaller elements
# moving in the right direction
while(stack and stack[-1] >= 0 \
and abs(element) > stack[-1]):
stack.pop()
# Top of stack and current
# element same value and top of
# stack moving in right direction
if (stack and stack[-1] >= 0 \
and stack[-1] == abs(element)):
stack.pop()
# No more elements remaining or
# remaining elements
# moving in left direction
elif(len(stack) == 0 \
or stack[-1] < 0):
stack.append(element)
# Finally return stack
return stack
# Driver code
if __name__ == '__main__':
obj = Solution()
arr = [5, -5, -2, -10]
ans = obj.solve(arr)
for x in ans:
print(x, end = " ")
Javascript
-2 -10
时间复杂度: O(N)
辅助空间: O(N)