1984.学生分数的最小差值
给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。
从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分 和 最低分 的 差值 达到 最小化 。
返回可能的 最小差值。
- 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;
}
1876.长度为3且字符不同的子字符串
如果一个字符串不含有任何重复字符,我们称这个字符串为 好 字符串。
给你一个字符串 s ,请你返回 s 中长度为 3 的 好子字符串 的数量。
注意,如果相同的好子字符串出现多次,每一次都应该被记入答案之中。
子字符串 是一个字符串中连续的字符序列。
- 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;
}
1839.所有元音字母按顺序排布的最长字符串
当一个字符串满足如下条件时,我们称它是 美丽的 :
所有 5 个英文元音字母(‘a’ ,‘e’ ,‘i’ ,‘o’ ,‘u’)都必须 至少 出现一次。
这些元音字母的顺序都必须按照 字典序 升序排布(也就是说所有的 ‘a’ 都在 ‘e’ 前面,所有的 ‘e’ 都在 ‘i’ 前面,以此类推)
比方说,字符串 “aeiou” 和 “aaaaaaeiiiioou” 都是 美丽的 ,但是 “uaeio” ,“aeoiu” 和 “aaaeeeooo” 不是美丽的 。
给你一个只包含英文元音字母的字符串 word ,请你返回 word 中 最长美丽子字符串的长度 。如果不存在这样的子字符串,请返回 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.
- n == customers.length == grumpy.length
- 1 <= minutes <= n <= 2 * 10^4
- 0 <= customers[i] <= 1000
- grumpy[i] == 0 or 1