天池 在线编程 数组划分III(计数)
生活随笔
收集整理的這篇文章主要介紹了
天池 在线编程 数组划分III(计数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
https://tianchi.aliyun.com/oj/231188302809557697/235445278655844965
給你一個整數數組和一個整數K,請你判斷數組是否可以劃分為若干大小為k序列,并滿足以下條件:
- 數組中的每一個數恰恰出現在一個序列中
- 一個序列中的數都是互不相同的
- 數組中相同元素是被劃分到不同序列中的
如何可以劃分,返回True,否則返回False。
數組長度小于等于10^5。
示例 例1: input: array=[1,2,3,4], k = 2 output:true例2: input: array=[1,2,2,3], k = 3 output:false2. 解題
class Solution { public:/*** @param arr: the input array* @param k: the sequence length* @return: if it is possible, return true, otherwise false*/bool partitionArray(vector<int> &arr, int k) {// write your code hereint n = arr.size();if(n%k != 0) return false; // 不能整除int bucket = n/k; // 桶的個數map<int,int> m;for(auto a : arr) {if(++m[a] > bucket)//個數超過桶的個數,肯定不滿足各個數不一樣的條件return false;}return true;} };151ms C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的天池 在线编程 数组划分III(计数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow 2.0 - Hub
- 下一篇: LeetCode 1566. 重复至少