作者: 负雪明烛 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…
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 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…