1984.学生分数的最小差值
给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。
从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分 和 最低分 的 差值 达到 最小化 。
返回可能的 最小差值。
输入:nums = [90], k = 1
输出:0
解释:选出 1 名学生的分数,仅有 1 种方法:
- [90] 最高分和最低分之间的差值是 90 - 90 = 0
可能的最小差值是 0
输入:nums = [9,4,1,7], k = 2
输出:2
解释:选出 2 名学生的分数,有 6 种方法:
- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5
- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8
- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2
- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3
- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3
- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6
可能的最小差值是 2
- 1 <= k <= nums.length <= 1000
- 0 <= nums[i] <= 10^5
nt cmp(const void * a, const void * b){
return *(int*)a - *(int*)b;
}
int minimumDifference(int* nums, int numsSize, int k){
if(k == 1){
return 0;
}
qsort(nums, numsSize, sizeof(int), cmp); //(1)
int i = 0;
int min = 100001;
for(i = 0; i < numsSize - k + 1; i++){ //(2)
int ret = nums[i+k-1] - nums[i];
min = min < ret ? min : ret; //(3)
}
return min;
}
解题思路:
(1)从小到大排序。
(2)区间长度为k,说明子数组下标之差为k-1。
(3)找出差值的最小值。

1876.长度为3且字符不同的子字符串
如果一个字符串不含有任何重复字符,我们称这个字符串为 好 字符串。
给你一个字符串 s ,请你返回 s 中长度为 3 的 好子字符串 的数量。
注意,如果相同的好子字符串出现多次,每一次都应该被记入答案之中。
子字符串 是一个字符串中连续的字符序列。
输入:s = “xyzzaz”
输出:1
解释:总共有 4 个长度为 3 的子字符串:“xyz”,“yzz”,“zza” 和 “zaz” 。
唯一的长度为 3 的好子字符串是 “xyz” 。
输入:s = “aababcabc”
输出:4
解释:总共有 7 个长度为 3 的子字符串:“aab”,“aba”,“bab”,“abc”,“bca”,“cab” 和 “abc” 。
好子字符串包括 “abc”,“bca”,“cab” 和 “abc” 。
- 1 <= s.length <= 100
- s 只包含小写英文字母
int countGoodSubstrings(char * s){
int count = 0;
int i = 0;
int size = strlen(s);
if(size < 3){ //(1)
return 0;
}
while(s[i+2]){
if(s[i] != s[i+1] && s[i+1] != s[i+2] && s[i] != s[i+2]){
count++; //(2)
}
i++;
}
return count;
}
解题思路:
(1)字符串长度小于3,则没有好字符串。
(2)找出3个字符各不相等的好子字符串的个数。

1839.所有元音字母按顺序排布的最长字符串
当一个字符串满足如下条件时,我们称它是 美丽的 :
所有 5 个英文元音字母(‘a’ ,‘e’ ,‘i’ ,‘o’ ,‘u’)都必须 至少 出现一次。
这些元音字母的顺序都必须按照 字典序 升序排布(也就是说所有的 ‘a’ 都在 ‘e’ 前面,所有的 ‘e’ 都在 ‘i’ 前面,以此类推)
比方说,字符串 “aeiou” 和 “aaaaaaeiiiioou” 都是 美丽的 ,但是 “uaeio” ,“aeoiu” 和 “aaaeeeooo” 不是美丽的 。
给你一个只包含英文元音字母的字符串 word ,请你返回 word 中 最长美丽子字符串的长度 。如果不存在这样的子字符串,请返回 0 。
子字符串 是字符串中一个连续的字符序列。
输入:word = “aeiaaioaaaaeiiiiouuuooaauuaeiu”
输出:13
解释:最长子字符串是 “aaaaeiiiiouuu” ,长度为 13 。
输入:word = “aeeeiiiioooauuuaeiou”
输出:5
解释:最长子字符串是 “aeiou” ,长度为 5 。
输入:word = “a”
输出:0
解释:没有美丽子字符串,所以返回 0 。
- 1 <= word.length <= 5 * 105
- word 只包含字符 ‘a’,‘e’,‘i’,‘o’ 和 ‘u’ 。
解题思路:
int longestBeautifulSubstring(char * word){
int l = 0;
int r = 0;
int len = 0;
while(word[r]){
r = l + 1;
if(word[l] != 'a'){ //(1)左指针停在第一个'a'的位置
l++;
}
if(word[r+1] =! 'a' && word[r+1]){ //(2)右指针停在下一个'a'之前的位置
r++;
}
//(3)传入左右指针
//写一个函数判断是不是美丽字符串, 并且返回美丽字符串的长度。
i++; j++;
}
}
1052.爱生气的书店老板
There is a bookstore owner that has a store open for n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the ith minute and all those customers leave after the end of that minute.
On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise.
When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
The bookstore owner knows a secret technique to keep themselves not grumpy for minutes consecutive minutes, but can only use it once.
Return the maximum number of customers that can be satisfied throughout the day.
输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
输出:16
解释:书店老板在最后 3 分钟保持冷静。
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
输入:customers = [1], grumpy = [0], minutes = 1
输出:1
- n == customers.length == grumpy.length
- 1 <= minutes <= n <= 2 * 10^4
- 0 <= customers[i] <= 1000
- grumpy[i] == 0 or 1
解题思路:
(1)定义一个不断变化的数组,老板在第0时刻一直到第 i-minutes 使用自己的能力。 这一步不会。
(2)ret = !grumpy[i] 生气就是0,不生气就是1。
(3)满意的人数就为:sum += ret * customers[i] 累加。
(4)max = max > sum ? max : sum; 找出满意人数的最大值。
这篇博客介绍了使用滑动窗口算法解决的三道LeetCode题目:1984.学生分数的最小差值,目标是最小化k个分数之间的差值;1876.长度为3且字符不同的子字符串,求好子字符串的数量;1839.所有元音字母按顺序排布的最长字符串,寻找最长的美丽子字符串。文章提供了详细的解题思路。
1109

被折叠的 条评论
为什么被折叠?



