zoukankan      html  css  js  c++  java
  • 768. Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.


    Given an array arr of integers (not necessarily distinct), we split the array into some number of "chunks" (partitions), and individually sort each chunk.  After concatenating them, the result equals the sorted array.

    What is the most number of chunks we could have made?

    Example 1:

    Input: arr = [5,4,3,2,1]
    Output: 1
    Explanation:
    Splitting into two or more chunks will not return the required result.
    For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.
    

    Example 2:

    Input: arr = [2,1,3,4,4]
    Output: 4
    Explanation:
    We can split into two chunks, such as [2, 1], [3, 4, 4].
    However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.
    

    Note:

    • arr will have length in range [1, 2000].
    • arr[i] will be an integer in range [0, 10**8].

    解题思路:

    注意一下一个空vector含两个指针,为2b,32位电脑为8B,64位为16B。这里数字是10^8用vector是放不下的!!

    1. class Solution {  
    2. public:  
    3.     int maxChunksToSorted(vector<int>& arr) {  
    4.         unordered_map<int ,int> ss;  
    5.         vector<int> temp(arr);  
    6.         sort(arr.begin(),arr.end());  
    7.         multiset<int> ret;  
    8.         unordered_map<int,vector<int>> store;  
    9.           
    10.           
    11.         for(int i=0;i<arr.size();i++){  
    12.             store[arr[i]].push_back(i);  
    13.         }  
    14.           
    15.         int max_index=-99999;int count=0;  
    16.         for(int i=0;i<temp.size();i++){  
    17.             int now_index;  
    18.             if(store[temp[i]].size()>0)  
    19.              now_index = store[temp[i]][0];  
    20.             store[temp[i]].erase(store[temp[i]].begin());  
    21.             if(now_index>max_index) max_index=now_index;  
    22.               
    23.             if(max_index==i) {count++;max_index=-99999;}  
    24.               
    25.         }  
    26.         return count;  
    27.     }  
    28. };  
  • 相关阅读:
    nginx $remote_addr 详解
    Alipay SDK验签PHP低于5.5版本错误
    Alipay支付宝调用错误:Call to undefined function openssl_sign()
    nginx.conf 下日志host.access.log 说明
    vim全选,全部复制,全部删除
    jquery 获取上传文件大小
    linux网络配置
    crontab 定时任务简单备份数据库
    linux进程管理
    mysql 动态增加列,查找表中有多少列,具体什么列。 通过JSON生成mysql表 支持子JSON
  • 原文地址:https://www.cnblogs.com/liangyc/p/8849043.html
Copyright © 2011-2022 走看看