【Leetcode_easy】908. Smallest Range I】的更多相关文章

problem 908. Smallest Range I solution: class Solution { public: int smallestRangeI(vector<int>& A, int K) { ], mn = A[]; for(auto a:A) { mx = max(mx, a); mn = min(mn, a); } , mx-mn-*K); } }; 参考 1. Leetcode_easy_908. Smallest Range I; 2. discuss…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetcode.com/problems/smallest-range-i/description/ 题目描述 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ad…
题目如下: 解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K.遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差(小于零则取零)就是答案. 代码如下: class Solution(object): def smallestRangeI(self, A, K): """ :type A: List[int] :type K: int :rtype: int """…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest-range/description/ 题目描述: You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/smallest-range-ii/description/ 题目描述 Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and add…
题目如下: 解题思路:我的思路是先找出最大值.对于数组中任意一个元素A[i]来说,如果A[i] + K 是B中的最大值,那么意味着从A[i+1]开始的元素都要减去K,即如果有A[i] + K >= A[-1] - K,那么A[i] + K 就可以作为最大值而存在:如果A[i] + K是最大值,那么最大的最小值是多少呢?因为A[i] 左边的元素都比A[i]小,所以其左边的元素都可以加上K,最大的最小值就会在 A[0] + K 和 A[i+1] - K 之间产生.遍历数组,计算每一个A[i] + K…
Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the range of elements are [0, 99] inclusive, return itsmissing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”]Examp…
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B…
[题目]E. New Year and Entity Enumeration [题意]给定集合T包含n个m长二进制数,要求包含集合T且满足以下条件的集合S数:长度<=m,非和与的结果都在集合中.(详细的题意见原题) [算法]数学(贝尔数) [题解]这道题确实不太能理解这种做法,所以就简单写写了. 先不考虑S须包含集合T. 对于一个方案,按位考虑,所有含位 i 的数字and起来得到含该位的最小数字,记为f[i]. 对于f[x]≠f[y],有f[x]&f[y]=0,证明:!(f[x]&f…
[题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符串末尾+a,有pb/(pa+pb)的可能在字符串末尾+b,求加到组成至少k对子序列“ab"时的期望子序列“ab”数.k<=1000,pa,pb<=10^6. [算法]期望DP [题解]主要问题在于字符串无限延伸,那么需要考虑记录前缀的关键量来为DP设置终止状态. 设f[i][j]表示前缀…
problem 1170. Compare Strings by Frequency of the Smallest Character 参考 1. Leetcode_easy_1170. Compare Strings by Frequency of the Smallest Character; 完…
problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完…
problem 744. Find Smallest Letter Greater Than Target 题意:一堆有序的字母,然后又给了一个target字母,让求字母数组中第一个大于target的字母,数组是循环的,如果没有,那就返回第一个字母. solution1:注意数组已经是有序数组啦...注意mid的计算,注意最后返回的元素位置. class Solution { public: char nextGreatestLetter(vector<char>& letters,…
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int m, int n, vector<vector<int>>& ops) { for(auto op : ops) { m = min(m, op[]); n = min(n, op[]); } return m*n; } }; 参考 1. Leetcode_easy_598.…
题目如下: You are given some lists of regions where the first region of each list includes all other regions in that list. Naturally, if a region X contains another region Y then X is bigger than Y. Given two regions region1, region2, find out the smalle…
题目如下: You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. You can swap the characters at any pair of indices in the given pairs any number of times. Retu…
题目如下: In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skill…
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once. Example 1: Input: "cdadabcc" Output: "adbc" Example 2: Input: "abcd" Output: "abcd" Exa…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/smallest-integer-divisible-by-k/ 题目描述 Given a positive integer K, you need find the smallest positive integer N such that N is divisible b…
题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value…
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add xto A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B.…
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 思路: 1.计算左子树元素个数leftSize. 2. leftSize+1 = K,则根节点即为第K个元素 leftSize >=k, 则第K个元素在左子树…
题目 : Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value…
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. If k…
原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of…
题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. Return the length of N.  If there is no such N, return -1. Example 1: Input: 1 Output: 1 Explanation: The…
题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of thi…
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i]. After this process, we have some array B. Return the smallest possible difference between the maximum value of B and the minimum value of B…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目描述 Given the root of a binary tree, each node has a value from 0 to 25 representing the lett…
传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Input Sample Output Hint k<=750 Solution 显然可以一行一行做,同时如果S+now[j]最小,需要S最小.即我们只需要记录前i行的最小的k个ans,分别与当前行的k个相加,从k2个ans中选择前k个小的记录.显然前k小的可以开一个大根堆维护.即如果size>k则pop…