📌  相关文章
📜  给定棒形成的矩形和正方形的长度之和的最大值

📅  最后修改于: 2021-04-18 02:42:01             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,代表棒的长度,任务是找到使用这些棒构造的正方形和矩形的所有长度的最大和。
注意只能用一根棍子来代表单面。

例子:

方法:请按照以下步骤解决问题:

  • 遍历数组以计算所有数组元素的频率,例如freq。
  • 计数频率至少为2
  • 将频率转换为最接近的较小偶数值。
  • 将频率按降序转换为一维数组。
  • 现在,对元素求和直至索引为4的倍数。
  • 打印所有这些元素的总和

下面是上述方法的实现:

C++14
#include 
using namespace std;
 
// Function to find the maximum of sum
// of lengths of rectangles and squares
// formed using given set of sticks
void findSumLength(vector arr,int n)
{
 
    // Stores the count of frequencies
    // of all the array elements
    map freq;
    for(int i:arr) freq[i] += 1;
 
    // Stores frequencies which are at least 2
    map freq_2;
 
    for (auto i:freq)
    {
        if (freq[i.first] >= 2)
            freq_2[i.first] = freq[i.first];
    }
   
    // Convert all frequencies to nearest
    // smaller even values.
    vector arr1;
    for (auto i:freq_2)
        arr1.push_back((i.first) * (freq_2[(i.first)]/2)*2);
    sort(arr1.begin(), arr1.end());
 
    // Sum of elements up to
    // index of multiples of 4
    int summ = 0;
    for (int i:arr1)
        summ += i;
 
    // Print the sum
    cout << summ;
}
 
// Driver Code
int main()
{
  vector arr = {5, 3, 2, 3, 6, 4, 4, 4, 5, 5, 5};
  int n = arr.size();
  findSumLength(arr, n);
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Python3
# Python3 implementation of
# the above approach 
 
from collections import Counter
 
# Function to find the maximum of sum
# of lengths of rectangles and squares
# formed using given set of sticks
def findSumLength(arr, n) : 
     
    # Stores the count of frequencies
    # of all the array elements
    freq = dict(Counter(arr))
     
    # Stores frequencies which are at least 2
    freq_2 = {}
 
    for i in freq:
        if freq[i]>= 2:
            freq_2[i] = freq[i]
             
    # Convert all frequencies to nearest
    # smaller even values.
    arr1 = []
    for i in freq_2:
      arr1.extend([i]*(freq_2[i]//2)*2)
 
    # Sort the array in descending order
    arr1.sort(reverse = True)
 
    # Sum of elements up to
    # index of multiples of 4
    summ = 0
    for i in range((len(arr1)//4)*4):
        summ+= arr1[i]
         
    # Print the sum
    print(summ)
   
# Driver Code 
if __name__ == "__main__" : 
   
    arr = [5, 3, 2, 3, 6, 4, 4, 4, 5, 5, 5]; 
    n = len(arr); 
    findSumLength(arr, n);


C#
using System;
using System.Collections.Generic;
class GFG{
 
  // Function to find the maximum of sum
  // of lengths of rectangles and squares
  // formed using given set of sticks
  static void findSumLength(List arr, int n)
  {
 
    // Stores the count of frequencies
    // of all the array elements
    Dictionary freq = new Dictionary();
    foreach (int i in arr){
      if(freq.ContainsKey(i))
        freq[i] += 1;
      else
        freq[i] = 1;
    }
 
    // Stores frequencies which are at least 2
    Dictionary freq_2 = new Dictionary();
    foreach(KeyValuePair entry in freq)
    {
      if (freq[entry.Key] >= 2)
        freq_2[entry.Key] = freq[entry.Key];
    }
 
    // Convert all frequencies to nearest
    // smaller even values.
    List arr1 = new List();
    foreach(KeyValuePair entry in freq_2)
      arr1.Add(entry.Key * (freq_2[entry.Key]/2)*2);
    arr1.Sort();
 
    // Sum of elements up to
    // index of multiples of 4
    int summ = 0;
    foreach (int i in arr1){
      summ += i;
    }
     
    // Print the sum
    Console.WriteLine(summ);
  }
 
  // Driver Code
  public static void Main()
  {
    List arr = new List(){5, 3, 2, 3, 6, 4, 4, 4, 5, 5, 5};
    int n = arr.Count;
    findSumLength(arr, n);
 
  }
}
 
// THIS CODE IS CONTRIBUTED BY SURENDRA_GANGWAR.


输出:
34

时间复杂度: O(NlogN)
辅助空间: O(1)