【lintcode】二分法总结 II】的更多相关文章

Half and Half 类型题 二分法的精髓在于判断目标值在前半区间还是后半区间,Half and Half类型难点在不能一次判断,可能需要一次以上的判断条件. Maximum Number in Mountain Sequence Given a mountain sequence of n integers which increase firstly and then decrease, find the mountain top. 样例  Given nums = [1, 2, 4,…
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意…
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra…
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Notice You may assume all input has valid answer. ExampleGiven nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums = [1, 3,…
There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of pai…
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor o…
背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的物品总体积需要小于等于给定的m. 您在真实的面试中是否遇到过这个题? Yes 样例 对于物品体积[2, 3, 5, 7]和对应的价值[1, 5, 2, 4], 假设背包大小为10的话,最大能够装入的价值为9. 解题 动态规划,和上一题很类似的 状态转移方程: f[i][j]表示前i个商品在j的空间时…
排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2, 1, 4],k=4, 你的代码应该在原地操作使得数组变成[1, 2, 2, 3, 4] 解题 直接快排 class Solution { /** * @param colors: A list of integer * @param k: An integer * @return: nothin…
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次) 解题 Jump1 终于自己还是没有解决出来 参考链接  理解不透 public class Solution { /** * @param A: A lis…
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II public class Solution { /** * @par…
Given a string s, cut s into some substrings such that every substring is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Given s = "aab", Return 1 since the palindrome partitioning ["aa", "b"…
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.   Example Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5]. 快速排序是排序算法中比较重要一种,也是经常容易考的一种排序算法,务必要掌握好.快排的优点是其平均时间复杂度为O(nlgn),这样在给大数据集排序的时候,…
Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start, end), return the sum from index start to index end in the given array. For modify(index, value), modify the number in t…
Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Have you met this question in a real interview? Yes Example Given the…
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. Note You are not suppose to use the library's sort function for this probl…
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note The subarray should contain at least one number Example For given [1, 3, -1, 2,…
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the array Example For [1, 2, 1, 2, 1, 3, 3] return 1 Challenge O(n) time and O(1) spa…
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you use a balanced binary search tree, you can get O(nlgn) - like std::set<int> class Solution { public: /** * @param A an integer array * @param start a…
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Have you met this question in a real interview? Yes Example Given [1, 2, 1, 2, 1, 3, 3], return 1. Note There is only one major…
一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如果这个比特位只出现1次,那么这比特位就会余1,否则就会整除. 把每个余下的比特位求出来,就知道是哪个数只出现1次了. class Solution { public: /** * @param A : An integer array * @return : An integer */ int si…
C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) { if (sum > target) { return ; } if (now >= a.size()) { if (sum ==…
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k…
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度. 如: 给出[100, 4, 200, 1, 3, 2], 最长的连续元素序列是[1, 2, 3, 4].返回它的长度:4. 你的算法必须有O(n)的时间复杂度 . 解法: 初始思路 要找连续的元素,第一反应一般是先把数组排序.但悲剧的是题目中明确要求了O(n)的时间复杂度,要做一次排序,是不能达…
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array. Find it. Have you met this question in a real interview? Yes Example Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3. Note…
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! 参考Lint…
原题网址:https://www.lintcode.com/problem/k-sum-ii/description 描述 Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. 您在真实的面试中是否遇到过这个题?  是 样例 给出,返回 [[1,4],[2,3]] 标签 LintCode 版权所有 Depth-first Se…
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, wh…
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 II 给定一个整数矩阵(其中,有 n 行, m 列),请找出矩阵中的最长上升连续子序列.(最长上升连续子序列可从任意行或任意列开始,向上/下/左/右任意方向移动). 样例 给定一个矩阵 [ [1 ,2 ,3 ,4 ,5], [16,17,24,23,6], [15,18,25,22,7], [14,1…
寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在重复的元素. 解题 暴力直接线性查找 或者,线性找到第一个开始降序的位置对应的数 应该考虑二分法 递归 + 二分 public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in…
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3,4,4,5,7,0,1,2]和target=4,返回 true 解题 直接法 class Solution: """ @param A : an integer ratated sorted array and duplicates are allowed @param targ…