📌  相关文章
📜  通过给定操作最小化数组中的非零元素

📅  最后修改于: 2021-09-06 06:29:24             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是通过将当前元素的值添加到其任何相邻元素并从当前元素中减去最多一次来最小化非零元素的数量。
例子:

方法:思想是使用贪心算法在每一步贪婪地选择。问题中的关键观察是三个连续的索引 i、j 和 k 只能存在三种可能性,即

  • 两个结束索引都有非零元素。
  • 任何一个索引都有非零元素。

在上述两种情况下,我们总是可以将值添加到中间元素并减去相邻元素。同样,我们可以贪婪地选择操作并更新数组的元素以最小化非零元素。
下面是上述方法的实现:

C++
// C++ implementation to minimize the
// non-zero elements in the array
 
#include 
using namespace std;
 
// Function to minimize the non-zero
// elements in the given array
int minOccupiedPosition(int A[], int n)
{
 
    // To store the min pos needed
    int minPos = 0;
 
    // Loop to iterate over the elements
    // of the given array
    for (int i = 0; i < n; ++i) {
 
        // If current position A[i] is occupied
        // the we can place A[i], A[i+1] and A[i+2]
        // elements together at A[i+1] if exists.
        if (A[i] > 0) {
            ++minPos;
            i += 2;
        }
    }
 
    return minPos;
}
 
// Driver Code
int main()
{
    int A[] = { 8, 0, 7, 0, 0, 6 };
    int n = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << minOccupiedPosition(A, n);
    return 0;
}


Java
// Java implementation to minimize the
// non-zero elements in the array
 
class GFG{
 
// Function to minimize the non-zero
// elements in the given array
static int minOccupiedPosition(int A[], int n)
{
     
    // To store the min pos needed
    int minPos = 0;
 
    // Loop to iterate over the elements
    // of the given array
    for (int i = 0; i < n; ++i)
    {
 
        // If current position A[i] is occupied
        // the we can place A[i], A[i+1] and A[i+2]
        // elements together at A[i+1] if exists.
        if (A[i] > 0) {
            ++minPos;
            i += 2;
        }
    }
    return minPos;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 8, 0, 7, 0, 0, 6 };
    int n = A.length;
 
    // Function Call
    System.out.print(minOccupiedPosition(A, n));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation to minimize the
# non-zero elements in the array
 
# Function to minimize the non-zero
# elements in the given array
def minOccupiedPosition(A, n):
 
    # To store the min pos needed
    minPos = 0
 
    # Loop to iterate over the elements
    # of the given array
    i = 0
    while i < n:
 
        # If current position A[i] is
        # occupied the we can place A[i],
        # A[i+1] and A[i+2] elements
        # together at A[i+1] if exists.
        if(A[i] > 0):
 
            minPos += 1
            i += 2
        i += 1
         
    return minPos
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 8, 0, 7, 0, 0, 6 ]
    n = len(A)
 
    # Function Call
    print(minOccupiedPosition(A, n))
     
# This code is contributed by Shivam Singh


C#
// C# implementation to minimize the
// non-zero elements in the array
using System;
 
class GFG {
     
// Function to minimize the non-zero
// elements in the given array
static int minOccupiedPosition(int[] A, int n)
{
         
    // To store the min pos needed
    int minPos = 0;
     
    // Loop to iterate over the elements
    // of the given array
    for(int i = 0; i < n; ++i)
    {
         
       // If current position A[i] is occupied
       // the we can place A[i], A[i+1] and A[i+2]
       // elements together at A[i+1] if exists.
       if (A[i] > 0)
       {
           ++minPos;
           i += 2;
       }
    }
    return minPos;
}
 
// Driver code   
static void Main()
{
    int[] A = { 8, 0, 7, 0, 0, 6 };
    int n = A.Length;
 
    // Function Call
    Console.WriteLine(minOccupiedPosition(A, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
2

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live