leetcode_321 Create Maximum Number

  • 题目分析:

    给定两个长度分别为m和n的数组,数组元素为0-9,每个数组元素代表一个数字。从这两个数组中选出一些数字,组成一个数组,是这个数组中的数尽可能大,其长度k <= m + n。要求数组中选出的元素的相对顺序与原数组保持一致。最终返回一个包含k个数字的数组。

  • 解题思路:

    1)分别从nums1(长度为m)和nums2(长度为n)中挑选出i(max(0, k - n) <= i <= min(m, k) 和k-i个数,在保持挑选数组的元素相对顺序不变的情况下,使选出的子数组最大化,主要利用贪心算法进行选取;

    2)在保持元素相对位置不变的前提下,将数组nums1与nums2合并,使合并的数组最大化。

  • 实现程序
    • C++版本

      // 贪心求数组中的最大子数组
      vector<int> findMaxKValue(vector<int> &nums, int k)
      {
          vector<int> result;
          if (k == 0)
              return result;
          result = vector<int>(k, 0);
          int j = 0;
          int n = nums.size();
          for (int i = 0; i < n; i++)
          {
              // 出栈操作
              while (j > 0 && n - i + j > k && nums[i] > result[j - 1])
                  j--;
              // 入栈操作
              if (j < k)
                  result[j++] = nums[i];
          }
          return result;
      }
      // merge操作中的对比函数
      bool compare(vector<int> &result1, int i, vector<int> &result2, int j)
      {
          while (i < result1.size() && j < result2.size() && result1[i] == result2[j])
          {
              i++;
              j++;
          }
          return j == result2.size() || (i < result1.size() && result1[i] > result2[j]);
      }
      // 两个数组的merge操作
      vector<int> merge(vector<int> &nums1, vector<int> &nums2)
      {
          int m = nums1.size();
          int n = nums2.size();
          if (!m)
              return nums2;
          if (!n)
              return nums1;
          vector<int> result(m + n, 0);
          int i = 0;
          int j = 0;
          int k = 0;
          // 数组合并操作
          while (i < m || j < n)
          {
              result[k++] = compare(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
          }
          return result;
      }
      // 返回两个数组中最大的k个数,并保持每个数组中的元素相对位置不变
      vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)
      {
          int m = nums1.size();
          int n = nums2.size();
          vector<int> result(k, 0);
          // 从数组nums中挑选k个数,在保持元素相对顺序不变的情况下,使得选出的子数组最大化。
          for (int i = max(0, k - n); i <= min(m, k); i++)
          {
              // 在数组nums1中挑选i个数
              vector<int> result1 = findMaxKValue(nums1, i);
              // 在数组nums2中挑选k-i个数
              vector<int> result2 = findMaxKValue(nums2, k - i);
              // 将两个挑选出的子数组进行合并
              vector<int> temp = merge(result1, result2);
              // 比较大小,来判断是否更新数组
              if (compare(temp, 0, result, 0))
                  result = temp;
          }
          return result;
      }
      
    • Java版本
      private boolean compare(int[] result1, int pos1, int[] result2, int pos2) {
          for ( ; pos1 < result1.length && pos2 < result2.length; pos1++, pos2++){
              if (result1[pos1] > result2[pos2])
                  return true;
              if (result1[pos1] < result2[pos2])
                  return false;
          }
          return pos1 != result1.length;
      }
      
      private int[] findMaxKValue(int[] nums, int k) {
          int[] result = new int[k];
          int len = 0;
          for (int i = 0; i < nums.length; i++){
              while (len > 0 && len + nums.length - i > k && result[len - 1] < nums[i]){
                  len--;
              }
              if (len < k)
                  result[len++] = nums[i];
          }
          return result;
      }
      
      public int[] maxNumber(int[] nums1, int[] nums2, int k){
          int[] result = new int[k];
          for (int i = Math.max(k - nums2.length, 0); i <= Math.min(nums1.length, k); i++){
              int[] result1 = findMaxKValue(nums1, i);
              int[] result2 = findMaxKValue(nums2, k - i);
              // 对两个数组执行merge操作
              int[] temp = new int[k];
              int pos1 = 0;
              int pos2 = 0;
              int tpos = 0;
              while (pos1 < result1.length || pos2 < result2.length){
                  temp[tpos++] = compare(result1, pos1, result2, pos2) ? result1[pos1++] : result2[pos2++];
              }
              if (!compare(result, 0, temp, 0))
                  result = temp;
          }
          return result;
      }
      
  • 参考文献

    http://bookshadow.com/weblog/2015/12/24/leetcode-create-maximum-number/

    http://blog.csdn.net/murmured/article/details/50392234

时间: 2024-10-16 15:10:09

leetcode_321 Create Maximum Number的相关文章

[LintCode] Create Maximum Number 创建最大数

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

Leetcode 321: Create Maximum Number

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + nfrom digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits

321. Create Maximum Number (c++ ——&gt; lexicographical_compare)

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

Create Maximum Number

1 public class Solution { 2 public int[] maxNumber(int[] nums1, int[] nums2, int k) { 3 int[] result = new int[k]; 4 5 for (int i = Math.max(0, k - nums2.length); i <= k && i <= nums1.length; i++) { 6 int[] candidate = merge(getMaxArray(nums

The maximum number of cell styles was exceeded. You can define up to 4000 styles

POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 报错如下: Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

LeetCode-Create Maximum Number

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

Maximum number of WAL files in the pg_xlog directory (1)

Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue with our customers' servers who have a wal_keep_segments setting higher than 0. We have a monitoring script that checks the number of WAL files in the

414. Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). 给定一个非空数组的整数,返回该数组中的第三个最大数. 如果不存在,返回最大数量. 时间复杂度必须在O(n)中. Example 1: Input: [3, 2,