769. Max Chunks To Make Sorted (ver. 1) && 768. Max Chunks to Make Sorted (ver. 2)

Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], 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 = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.

Example 2:

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

Note:

  • arr will have length in range [1, 10].

  • arr[i] will be a permutation of [0, 1, ..., arr.length - 1].

class Solution:
    def maxChunksToSorted(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        n = len(arr)
        mins = [n-1]*n
        mins[-1] = arr[n-1]
        for i in range(n-2, -1, -1):
            mins[i] = min(mins[i+1], arr[i])
        
        ret = 0
        m = arr[0]
        for i in range(1,n):
            if mins[i]>m:
                ret += 1
                m = arr[i]
            else:
                m = max(m, arr[i])
        return ret+1
        
s=Solution()
print(s.maxChunksToSorted([4,3,2,1,0]))
print(s.maxChunksToSorted([1,0,2,3,4]))
print(s.maxChunksToSorted([1,2,3,4,5,0]))

This question is the same as "Max Chunks to Make Sorted (ver. 1)" 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].
class Solution:
    def maxChunksToSorted(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        n = len(arr)
        mins = [n-1]*n
        mins[-1] = arr[n-1]
        for i in range(n-2, -1, -1):
            mins[i] = min(mins[i+1], arr[i])
        
        ret = 0
        m = arr[0]
        for i in range(1,n):
            if mins[i]>=m:
                ret += 1
                m = arr[i]
            else:
                m = max(m, arr[i])
        return ret+1
        
s=Solution()
print(s.maxChunksToSorted( [2,1,3,4,4]))
#print(s.maxChunksToSorted([1,0,2,3,4]))
#print(s.maxChunksToSorted([1,2,3,4,5,0]))




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据错误:AttributeError: module 'networkx' has no attribute 'from_numpy_matrix',修改下述代码:import os import jieba.analyse from textrank4zh import TextRank4Keyword import concurrent.futures # 定义分块读取函数 def read_in_chunks(file_path, chunk_size=1024*1024): with open(file_path, 'r', encoding='utf-8') as f: while True: data = f.read(chunk_size) if not data: break yield data # 定义处理函数 def process_chunk(chunk): # 使用jieba分词提取关键词 jieba_keywords = jieba.analyse.extract_tags(chunk, topK=10, withWeight=True) # 使用textrank4zh提取关键词 tr4w = TextRank4Keyword() tr4w.analyze(chunk, lower=True, window=2) textrank_keywords = tr4w.get_keywords(10, word_min_len=2) # 合并两种方法提取的关键词 keywords = jieba_keywords + textrank_keywords return keywords # 读取文本文件,并按块处理 chunks = [] for chunk in read_in_chunks('input.txt'): chunks.append(chunk) # 多线程并行处理 results = [] with concurrent.futures.ThreadPoolExecutor() as executor: futures = [executor.submit(process_chunk, chunk) for chunk in chunks] for future in concurrent.futures.as_completed(futures): results.extend(future.result()) # 合并结果,并按权重降序排序 keywords = {} for keyword, weight in results: if keyword in keywords: keywords[keyword] += weight else: keywords[keyword] = weight keywords = sorted(keywords.items(), key=lambda x: x[1], reverse=True) keywords = [(keyword, weight) for keyword, weight in keywords if len(keyword) > 1][:10] # 输出到txt文件中 with open('output.txt', 'w', encoding='utf-8') as f: for keyword, weight in keywords: f.write(keyword + '\t' + str(weight) + '\n')
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值