自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

roufoo的博客

纸上得来终觉浅 绝知此事要躬行 https://github.com/luqian2017

  • 博客(30)
  • 收藏
  • 关注

原创 LintCode 562: Backpack IV (完全背包问题变种,DP经典)

这题就是完全背包的变种。dp[x]表示能装满size 为x的背包的最多方法总数。注意:dp[j] += dp[j - nums[i]];旧的dp[j]: 上次的# of ways to fill size=j knapsack with item i旧的dp[j-nums[i]]: 上次的# of ways to fill size=j-nums[i] knapsack without...

2018-10-26 14:04:52 784 1

原创 LintCode 801: Backpack X (完全背包类型题,DP)

经典的完全背包例题。优化后2层循环,一重数组。class Solution {public: /** * @param n: the money you have * @return: the minimum money you have to give */ int backPackX(int n) { vec...

2018-10-25 13:48:43 290

原创 LintCode 799: Backpack VIII (多重背包问题变种,DP经典题, 难!)

我开始觉得这就是多重背包问题的变种,但是解法1会超时。因为有3重循环,所以当amount[i]的值都很大就超时了。解法1:dp[i]表示coins是否可以组合成i的值。class Solution {public: /** * @param n: the value from 1 - n * @param value: the value of coins ...

2018-10-24 15:39:13 825 2

原创 LintCode 564: Combination Sum IV (DP 经典题,难)

这题类似完全背包。代码如下:class Solution {public: /** * @param nums: an integer array and all positive numbers, no duplicates * @param target: An integer * @return: An integer */ in...

2018-10-20 10:38:55 349

原创 LintCode798: Backpack VII (多重背包问题 DP经典)

完全背包问题可以视为01背包的变形。解法1:没有经过优化的DP。dp[i][j] 表示前i件物品在价值不超过j的情况下的最大重量。若输入为:8 //n = money[3,2] //prices[300,160] //weight[1,6] //amounts输出为 640dp[][] 为:0 0 0 0 0 0 0 0 00 0 0 300 300 300 300 300...

2018-10-16 15:00:24 1222

原创 LintCode 125: Backpack II (0-1背包问题, DP经典)

解法1:经典DP解法。时间复杂度O(MN),空间复杂度O(MN)。注意:dp[m+1][n+1],如果定义成dp[m][n],dp[0][]就有歧义:到底表示不取任何包,还是取包0呢?class Solution {public: /** * @param m: An integer m denotes the size of a backpack * @pa...

2018-10-14 16:20:49 629

原创 LintCode 594: strStr II (Rabin Karp算法)

Solution是基于Rabin Karp算法,参考了九章的内容。Rabin Karp算法用hash function来算出source 和 target的hash code来比较,从而将时间复杂度从O(n^2)降到O(n)。该算法的关键在于计算source的hash code的过程中,采用循环移位来计算。例如,source = “abcde”, target = “cd”.计算sour...

2018-10-07 06:11:33 509

原创 LintCode 76: Longest Increasing Subsequence (最典型的DP题)

解法1:DP。没有优化的版本,时间复杂度O(n^2)。优化版本,时间复杂度O(nlogn)。没有优化的版本代码如下:class Solution {public: /** * @param nums: An integer array * @return: The length of LIS (longest increasing subsequence) ...

2018-10-07 01:12:17 804

原创 LintCode 83: Single Number II (模3运算,经典位运算题)

经典位运算题。给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。在线评测地址: http://www.lintcode.com/problem/single-number-ii/解法参考九章,基于模3运算。其中ONES 和 TWOS 表示:如果TWOS里面ith是1,则ith当前为止出现1的次数模3的结果是2如果ONES里面ith是1,则ith目前为止...

2018-10-30 17:19:32 896

原创 LintCode 179: Update Bits (位运算题)

经典位运算题。记得尽量给位运算加括号,因为其优先级非常低。class Solution {public: /** * @param n: An integer * @param m: An integer * @param i: A bit position * @param j: A bit position * @return: ...

2018-10-28 13:27:24 221

原创 C++位运算常见操作

一些位运算的常见操作,整理如下:注意:1)位运算操作符的优先级都非常低,尽量记得加括号。int set_bit(int x, int n){ return x |= (1 << n);}

2018-10-28 13:17:48 2063 1

原创 LintCode 800: Backpack IX (经典01背包问题,DP)

注意,该题是01背包问题的变种,不是完全背包问题!因为每个学校只能申请一次!class Solution {public: /** * @param n: Your money * @param prices: Cost of each university application * @param probability: Probability of ...

2018-10-26 13:08:43 448

原创 LintCode 563: Backpack V(经典01背包问题)

经典01背包问题。注意j循环必须是从大到小。class Solution {public: /** * @param nums: an integer array and all positive numbers * @param target: An integer * @return: An integer */ int backPa...

2018-10-20 12:10:47 622

原创 LintCode 92: Backpack (经典01背包问题)

经典01背包问题。DP solution。Time complexity O(nm),n is the # of items, m is the packet size。Space complexity O(m)。注意:k循环必须从大到小 (对01背包和重复背包)。而如果是完全背包的话,可以是从小到大,从而实现优化。class Solution {public: /** ...

2018-10-19 11:31:33 923

原创 LintCode 440: Backpack III (完全背包问题,DP经典)

解法1:将其视为多重背包变形,每种物品取的上限是m/A[i]。时间复杂度: O(sum(m/A[i]) * backpackSize)。空间复杂度: O(m)。class Solution {public: /** * @param A: an integer array // size array * @param V: an integer array...

2018-10-17 20:25:45 1033

原创 LintCode 8: Rotate String (三步反转法)

经典的三步反转法。class Solution {public: /** * @param str: An array of char * @param offset: An integer * @return: nothing */ void rotateString(string &str, int offset) { ...

2018-10-14 15:01:40 242

原创 LintCode 39: Recover Rotated Sorted Array (三步反转法)

经典的三部反转法。Time complexity O(n).Space complexity O(1).一个小优化是在找pivot的时候可以用binary search。但总的时间复杂度还是O(n)。class Solution {public: /** * @param nums: An integer array * @return: nothing ...

2018-10-14 14:56:22 152

原创 LintCode 38: Search a 2D Matrix II

最优的做法就是从右上角开始。如果当前点大于target, colIndex–;如果当前点小于target, rowIndex++;否则 colIndex–; rowIndex++; count++;该方法也可以从左下角开始。Time complexity O(m+n).Space complexity O(1)....

2018-10-14 14:48:09 354

原创 LintCode 39: Recover Rotated Sorted Array (三步翻转法)

三步翻转法。时间复杂度O(n)。空间复杂度O(1)。一个小优化是找rotate的位置的时候可以用binary search,但总的时间复杂度还是O(n)。class Solution {public: /** * @param nums: An integer array * @return: nothing */ void recoverR...

2018-10-12 21:53:18 434

原创 LintCode 1380: Log Sorting

这道题目可以重温一下c++ stl里面的string::substr()string::find_first_of()sort()和opertor < 操作符重载enum Type { LETTERS, DIGITS,};struct Entry { string id; string content; int index; ...

2018-10-12 13:17:51 547

原创 LintCode 1479: Can Reach The Endpoint (BFS 典型题)

Solution 1: BFS searching, 类似骑士遍历题。struct Coord { int x; int y; Coord() : x(0), y(0) {} Coord(int a, int b) : x(a), y(b){}};class Solution {public: /** * @param map: the map ...

2018-10-10 16:04:58 566

原创 LintCode 1564: Interval Search (Binary Search 经典题)

我的做法是对每个interval的start和end计数,如果是start, count++, end, count–。这里要注意点有重叠的情况。用一个map来关联point和count。用binary search来找小于或等于target的第一个数,如果对应的count==0, 说明target不在某interval,否则说明target在某interval。代码如下:class So...

2018-10-09 16:26:12 436

原创 LintCode 1565: Modern Ludo I (DP典型题)

DP典型题。需要注意:起始点从1算起。点2-7的DP值为1.从点8到最后,所有点的DP值需要考虑两个因素:shortCut和骰子。class Solution {public: /** * @param length: the length of board * @param connections: the connections of the pos...

2018-10-09 13:33:59 675

原创 LintCode 667: Longest Palindromic Subsequence (DP 经典题)

经典DP题。我们最后需要返回dp[0][len-1],所以两个for loop是下面的代码: for (int i = len - 1; i >= 0; --i) { cout<<"i="<<i<<endl; for (int j = i + 1; j < len; ++j) {对于...

2018-10-08 03:27:06 323

原创 LintCode 109: Triangle (DP 经典题)

典型的DP题。我的解法的时间复杂度O(n^2)。不知道有没有O(n)的解法?class Solution {public: /** * @param triangle: a list of lists of integers * @return: An integer, minimum path sum */ int minimumTotal(...

2018-10-05 01:03:39 158

原创 LintCode 116: Jump Game (DP 典型题)

两种方法:解法1: DP. 时间复杂度O(n^2)。代码如下:class Solution {public: /** * @param A: A list of integers * @return: A boolean */ bool canJump(vector<int> &A) { int len =...

2018-10-05 00:02:19 221

原创 LintCode 513: Perfect Squares (DP 典型题)

典型的DP问题。注意isSquareNum() 用到了floor()。class Solution {public: /** * @param n: a positive integer * @return: An integer */ int numSquares(int n) { vector<int> DP(n ...

2018-10-04 23:16:36 194

原创 LintCode 65: Median of two Sorted Arrays (经典题!!!)

这题解法很多。解法1: 二分法。找两个排序数组中第K小的数。If startA exceeds A.size(), then the Kth smallest elements should be B[startB + K -1];If startA exceeds A.size(), then the Kth smallest elements should be B[startB ...

2018-10-03 17:36:26 390

原创 LintCode 486: Merge K Sorted Arrays (经典题)

解法1:利用priority queue + tuple。记得我们需要最小堆,因为sorting是从小到大排序。而priority queue缺省是用最大堆,所以需要define class cmp 和 bool operator()。tuple<i,j,k> 记录了k=arrays[i][j]。时间复杂度应该是 O(N log k).N is the total numbe...

2018-10-03 10:19:06 1070 1

原创 LintCode 577: Merge K Sorted Interval Lists (经典题中的经典题)

这题有很多种解法:解法1:利用priority queue和tupletuple<i, j, interval>是存储intervals[][]中i行j列对应的interval。第一步,先把每个vector的head pushd到priority_queue中,然后再将pq的top push_back到result vector中,同时对于的vector的j指针往右移位。这样,最...

2018-10-02 01:27:29 570

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除