当前位置:网站首页>Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
Lecture 84 Biweekly t4 6144 and Lecture 305 t4 6138
2022-08-08 04:29:00 【Yu Niangniang】
6144. Minimum number of replacements to sort an array
Give you an array of integers
numsstarting at 0 in the following table.In each operation, you can replace any element of the array with any two and the number for that element.
- For example,
nums = [5,6,7].In one operation, we can replacenums[1]with2and4, andnumsintonumscode>[5,2,4,7] .Please perform the above operation to turn the array into an array of elements in non-decreasing order, and return the minimum number of operations required.
Example 1:
Input:nums = [3,9,3]Output:2Explanation: Here are the steps to turn the array into non-decreasing order:- [3,9,3] , turn 9 into 3 and 6 , get array [3,3,6,3]- [3,3,6,3] , turn 6 into 3 and 3 , get array [3,3,3,3,3]It takes a total of 2 steps to turn the array into non-decreasing order, so we return 2 .Example 2:
Input:nums = [1,2,3,4,5]Output:0Explanation: The array is already in non-decreasing order, so we return 0 .Tip:
1 <= nums.length <= 1e51 <= nums[i] <= 1e9
Source: LeetCode
Link: https://leetcode.cn/problems/maximum-employees-to-be-invited-to-a-meeting
Copyright ownershipNetwork all.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Results of the questions
Failed, I was stuck by writing a case [2,7,3] by myself, and the replacement strategy for the second 7 could not be figured out
Method: Greedy
1. Look from the back to the front
2. If it is larger than the latter, divide it equally according to the latter number to see how many parts it can be divided into. If it is divisible, the subsequent number remains unchanged and divided into part-1 parts (the watermelon is cut in half with only one knife)
3. If it is not divisible, divide as much as possible and increase the number at the beginning. Since it is not divisible, divide an extra copy of next+1, and make the number at the beginning as large as possible.
class Solution {public long minimumReplacement(int[] nums) {int n = nums.length;long ans = 0L;int next = nums[n-1];for(int i = n-2; i >= 0; i--){if(nums[i]>next){int part = nums[i]/next;if(nums[i]%next==0) {ans += part-1;}else{ans += part;next = nums[i]/(part+1);}}else next = nums[i];}return ans;}}6138. Longest Ideal Subsequence
You are given a string of lowercase letters
s, and an integerk.The stringtcan be considered a ideal string if the following conditions are met:
- The absolute difference between every two adjacent letters in
tis a subsequence of the strings.tin the alphabet is less than or equal tok.Returns the length of the longest desired string.
A subsequence of a string is also a string, and the subsequence also satisfies: it can be obtained by deleting some characters (or not deleting) from other strings without changing the order of the remaining characters.
Note: The alphabetical order does not cycle.For example, the absolute difference between
'a'and'z'in the alphabet is25, not1.Example 1:
Input:s = "acfgbd", k = 2Output:4Explanation: The longest ideal string is "acbd" .The string length is 4 , so 4 is returned.Note that "acfgbd" is not an ideal string because 'c' and 'f' differ in alphabetical order by 3 .Example 2:
Input:s = "abcd", k = 3Output:4Explanation: The longest ideal string is "abcd" , which is 4 in length, so 4 is returned.Tip:
1 <= s.length <= 1050 <= k <= 25sconsists of lowercase English lettersSource: LeetCode
Link: https://leetcode.cn/problems/longest-ideal-subsequence
The copyright belongs to LeetCode.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Results of the questions
Success, this question is medium, just seconds away
Method: Dynamic Programming
The previous range from max(index-k) to min(index+k 25) can be extended by one to the current letter. Select the longest one and connect it to yourself. Note that if your length is 1, you need to add an additional one.
class Solution {public int longestIdealString(String s, int k) {int[] dp = new int[26];int n = s.length();int ans = 0;for(int i = 0; i < n; i++){int v =0 ;int index = s.charAt(i)-'a';for(int start = Math.max(index-k,0); start<=Math.min(index+k,25);start++){v=Math.max(dp[start],v);}dp[index] = v+1;ans = Math.max(v+1,ans);}return ans;}}边栏推荐
- 让你的文字被更多人看到:来投稿吧,稿酬靠谱!
- 强网杯 2019-随便注 (堆叠注入)
- After being unemployed for 6 months at home, I bought a house with full payment through outsourcing: the industries you look down on are often very profitable
- Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
- 亚马逊云科技Build On学习心得
- Some excellent blog recommendations for Qt event learning reference
- MYSQL导出数据字典
- L3-006 Slash in the wind
- 如何在推荐系统中玩转知识图谱
- 二维码生成工具
猜你喜欢

类似Bugfree的9大在线缺陷管理软件

【Win10】若干睡眠问题及对策

leetcode: 874. 模拟行走机器人

06 tp6 的数据更新(改)及删除 《ThinkPHP6 入门到电商实战》

A line of code counts the number of occurrences of the specified string in the text

vulnhub-DC-5靶机渗透记录

vulnhub-DC-3 drone penetration record

The live broadcast of agricultural products continues to heat up, Economic Daily: Don’t forget quality when rushing

Risk control strategy must be learned | This method of mining rules with decision trees

XDR技术
随机推荐
XDR technology
Mini Program Optimization Practice
Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
package package
MySQL4(多表查询)
06 tp6 的数据更新(改)及删除 《ThinkPHP6 入门到电商实战》
强网杯 2019-随便注 (堆叠注入)
ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅
MySQL from entry to entry [20W word collection]
数据库缓冲区高速缓存中块的类型
C语言-函数
分类、目标检测、语义分割、实例分割的区别
LeetCode_485_最大连续1的个数
【保研面试】英文问题
vulnhub-DC-5 target drone penetration record
C language minesweeping
6G-Oriented Communication Perception Integrated Architecture and Key Technologies
Building a High-Performance Platform on AWS Using Presto and Alluxio to Support Real-Time Gaming Services
手把手教你手撕SPF生成树(OSPF区域内防环)
语音鉴定软件