首页 > 其他分享 >LeetCode 2468. Split Message Based on Limit

LeetCode 2468. Split Message Based on Limit

时间:2024-04-07 11:45:08浏览次数:26  
标签:Based getLen int 2468 length parts Limit limit message

原题链接在这里:https://leetcode.com/problems/split-message-based-on-limit/description/

题目:

You are given a string, message, and a positive integer, limit.

You must split message into one or more parts based on limit. Each resulting part should have the suffix "<a/b>", where "b" is to be replaced with the total number of parts and "a" is to be replaced with the index of the part, starting from 1 and going up to b. Additionally, the length of each resulting part (including its suffix) should be equal to limit, except for the last part whose length can be at most limit.

The resulting parts should be formed such that when their suffixes are removed and they are all concatenated in order, they should be equal to message. Also, the result should contain as few parts as possible.

Return the parts message would be split into as an array of strings. If it is impossible to split message as required, return an empty array.

Example 1:

Input: message = "this is really a very awesome message", limit = 9
Output: ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
Explanation:
The first 9 parts take 3 characters each from the beginning of message.
The next 5 parts take 2 characters each to finish splitting message. 
In this example, each part, including the last, has length 9. 
It can be shown it is not possible to split message into less than 14 parts.

Example 2:

Input: message = "short message", limit = 15
Output: ["short mess<1/2>","age<2/2>"]
Explanation:
Under the given constraints, the string can be split into two parts: 
- The first part comprises of the first 10 characters, and has a length 15.
- The next part comprises of the last 3 characters, and has a length 8.

Constraints:

  • 1 <= message.length <= 104
  • message consists only of lowercase English letters and ' '.
  • 1 <= limit <= 104

题解:

The key is to find out the value of b.

To find out value of b, we can try from 0 and keep increamenting until it fulfills two conditions.

First, <b/b>, its length is getLen(b) * 2 + 3, its length must be < limit. Otherwise, there is no room for the submessage.

Second, overall length, sum of a's length + message length + (getLen(b + 3)) * b needs to be <= limit * b.

Keep trying until we find the b. Then add get the submessages one by one.

Time Complexity: O(n). n = message.length().

Space: O(n).

AC Java:

 1 class Solution {
 2     public String[] splitMessage(String message, int limit) {
 3         int sumOfA = 0;
 4         int b = 0;
 5         int messageLen = message.length();
 6         while(getLen(b) * 2 + 3 < limit && sumOfA + messageLen + (getLen(b) + 3) * b > limit * b){
 7             b++;
 8             sumOfA += getLen(b);
 9         }
10 
11         if(getLen(b) * 2 + 3 < limit){
12             String[] res = new String[b];
13             int pos = 0;
14             for(int i = 0; i < b; i++){
15                 StringBuilder sb = new StringBuilder();
16                 int subMessLen = limit - (getLen(b) + getLen(i + 1) + 3);
17                 sb.append(message.substring(pos, Math.min(pos + subMessLen, messageLen)));
18                 sb.append("<").append(i + 1).append("/").append(b).append(">");
19                 res[i] = sb.toString();
20                 pos += subMessLen;
21             }
22             return res;
23         }
24 
25         return new String[0];
26     }
27 
28     private int getLen(int num){
29         return String.valueOf(num).length();
30     }
31 }

 

标签:Based,getLen,int,2468,length,parts,Limit,limit,message
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18118749

相关文章

  • Linux ulimit命令教程:如何查看和设置系统资源限制(附实例详解和注意事项)
    Linuxulimit命令介绍ulimit是一个内置的Linuxshell命令,它允许查看或限制单个用户可以消耗的系统资源量。在有多个用户和系统性能问题的环境中,限制资源使用是非常有价值的。Linuxulimit命令适用的Linux版本ulimit命令在所有主流的Linux发行版中都是可用的,包括Debian、U......
  • Java.lang.OutOfMemoryError: GC overhead limit exceeded
    缘由系统是微服务架构,在服务器上跑了近11个微服务,某天发布更新部署新功能,几分钟后发现系统跑着跑着崩了。。。排查通过对11个微服务运行打印的日志,发现只有基础微服务日志中出现了GCoverheadlimitexceeded报错信息,然后从报GC异常的上一个报错的异常进行定位,发现是因为某......
  • Microservice - Distributed Transactions Based on Saga and Kafka in Practice
       ......
  • setrlimit函数限制进程资源
    setrlimit设置参数满足structrlimit{rlim_trlim_cur;//软限制rlim_trlim_max;//硬限制}可以设置的参数:RLIMIT_AS:进程总的可用的存储空间的大小。此外,自动堆栈扩展也将失败(并生成一个SIGSEGV,当没有备用堆栈可用时,它会终止进程)RLIMIT_CORE:核心文件的最大......
  • 关于 ulimit 的两个天坑
    稍微有点Linux经验的人一定会遇到过“Toomanyopenfiles”错误,这个错误本质是ulimit设置不合理导致的。关于ulimit设置,有哪些需要注意的点呢?本文给大家做一个介绍,希望对大家有所帮助。如何确认ulimit设置生效了?很多人设置了ulimit最后发现还是报错“Toomanyope......
  • DMKD: IMPROVING FEATURE-BASED KNOWLEDGE DISTILLATION FOR OBJECT DETECTION VIA DU
    摘要最近主流的掩模蒸馏方法是通过从教师网络的特征图中重建学生网络的选择性掩模区域来实现的。在这些方法中,需要适当的选择掩模区域,使重构的特征像教师特征一样具有足够的识别和表示能力。然而,以前的掩模蒸馏方法只关注空间掩模,使得得到的掩模区域偏向于空间重要性,而没有......
  • envoy&istio 对接ratelimit 实现限流之envoy配置
    envoy与ratelimit对接需要完成两步,1启用ratelimit过滤器,2配置触发条件。一、启用ratelimit过滤器envoy要与ratelimit服务对接,需要在lds的http_filter配置上启用ratelimitfilter,启用方式如下1http_filters:2-name:envoy.filters.http.ratelimit......
  • Where to Go Next for Recommender Systems? ID- vs. Modality-based Recommender Mod
    目录概符号/缩写说明TrainingdetailsDatasetsE2E下MoRec是否优于IDRec?RegularsettingWarmsetting越好的encoder带来越好的推荐效果?TSversusE2E?总结代码YuanZ.,YuanF.,SongY.,LiY.,FuJ.,YangF.,PanY.andNiY.Wheretogonextforrecommendersys......
  • 论文阅读RangeDet: In Defense of Range View for LiDAR-based 3D Object Detection
    文章目录RangeDet:InDefenseofRangeViewforLiDAR-based3DObjectDetection问题笛卡尔坐标结构图Meta-KernelConvolutionRangeDet:InDefenseofRangeViewforLiDAR-based3DObjectDetection论文:https://arxiv.org/pdf/2103.10039.pdf代码:https://......
  • IfcConversionBasedUnit
    IfcConversionBasedUnit实体定义IfcConversionBasedUnit用于定义具有基本单位转换率的单位。为了识别一些常用的基于转换的单位,表4中列出了Name属性的标准名称(不区分大小写)。 NameDescription'inch'Lengthmeasureequalto25.4mm'foot'Lengthmeasureequalto30......