
Algorithm
文章平均质量分 66
batilei
Temporal - Spatial Data
展开
-
Validate Binary Search Tree_Leetcode
一开始以为是题目SB,只是简单比较了父节点和子节点的值。结果发现原来是自己SB了,除了考虑父节点,还要考虑祖先节点的值,所以要用两个参数来记录每个节点valid的范围(根据父节点的值来不断调整)。public class Solution { public boolean isValidBST(TreeNode root) { if(root == null) ret原创 2015-01-24 11:23:14 · 712 阅读 · 0 评论 -
LeetCode - Sort List
就是用List来实现merge sort.import java.io.*;import java.util.*; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } s原创 2014-10-07 10:36:21 · 853 阅读 · 0 评论 -
Leetcode - Binary Tree Maximum Path Sum
解题的关键在于这条路径只能是先往上走,到达某个最高点,再往下走,换句话说,只能有一次转折的机会。所以递归这棵树,记录以某个子节点为转折点时的最大值。值得注意的是树节点的值有负值,所以如果某个子路径的和小于0,放弃它(设置和为0)。class Solution {public: int maxPathSum(TreeNode *root) { int maxSum = -1 <原创 2014-08-23 09:47:04 · 935 阅读 · 0 评论 -
Leetcode - Jump Game Two
class Solution {public: const int MAXVALUE = 1 << 30; int findMinStepToIndex(int maxNumbers[],int maxSteps,int index) { if (index == 0) return 0; int left = 1; int right = maxSteps;原创 2014-07-26 06:24:41 · 835 阅读 · 0 评论 -
Leetcode - candy
被第二个条件给坑了You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their ne原创 2014-08-23 10:58:10 · 975 阅读 · 0 评论 -
Leetcode 3Sum Closet
用了和3Sum差不多一样的思路,二分查找。关键要剪枝,但是却在剪枝那里犯了很多错误。然后原来有一个更加快的思路O(n^2).#include #include #include #include using namespace std;class Solution {public: int threeSumClosest(vector &num, int ta原创 2014-07-27 06:35:49 · 1116 阅读 · 0 评论 -
Leetcode - 3Sum
蛮常见一道题目。思路:1:排序,按顺序遍历两个数之和twoSum,2: 二分查找 (0 - twoSum)看是否存在这题最容易错的地方是must not contain duplicate triplets,所以遍历的这时候要用一个数字记录最后一个遍历的数字是,避免重复。#include#include #include using namespace s原创 2014-07-26 23:50:15 · 808 阅读 · 0 评论 -
LeetCode - Jump Game
一开始想DP一步步迭代更新,求出到跳到最后一个的最小步数,但是时间复杂度O(nk),会超时。再一想,发现该题只需要返回能否到达最后一个,不需要最小步数,所以迭代时候只需要保留当前能够走到的最远距离tmpMax,时间复杂度降到O(n)。class Solution {public: const int MAXVALUE = 1 << 30; bool canJump(int A[],原创 2014-07-26 04:01:11 · 796 阅读 · 0 评论 -
Leetcode - Reverse Words
比起POJ弱爆了一题,从后往前扫描一遍,O(n)时间,只要注意各种极端情况即可,不明白通过率为什么只有13%。#include#includeusing namespace std;class Solution {public: void reverseWords(string &s) { char* cstr = new char[s.size()+1];原创 2014-05-27 00:54:12 · 1066 阅读 · 0 评论 -
LeetCode Decode Ways
有点意思的题目。用动态规划可以O(n)求解出来:a[i]代表子字符串string(0,i)的可能解码方式,a[i] = {a[i-1] or a[i-1]+a[i-2]}. 意思是如果string(i)不为0,至少a[i] == a[i-1],即一种解码方法是string{0,.....(i-1)}+string(i); 然后如果string{i-1,i}是合法的(注意合法概念,比如原创 2014-05-27 03:53:56 · 1130 阅读 · 0 评论 -
poj 2559求柱形图中最大矩形
两种解法。我想到的是最大的矩形,中间一定有个最矮的某个单位矩形,所以用两个数组记录任何一个单位矩形histogram[i]左右两边第一个比它小的单位矩形的序号,这里找的时候用DP加速。#includeusing namespace std;//the histogram stored from left to rightlong histogram[100001]原创 2014-05-08 00:16:27 · 1628 阅读 · 0 评论 -
POJ2286 -IDA*
第一次IDA*。很有意思的思路。IDA*重点和A*不一样,A*总是看当前最优的估值函数f()=g()+h(),但IDA*不一样,只要深度在允许范围内,IDA*就不断地深搜,不管f()的大小。另外一个值得mark当要连续对不规则的数组进行有规律的操作时候,可以建立个映射数组。附代码,不过这个代码写的有点丑陋,另外值得参考的代码http://www.cnblogs.com/zhsl/archi原创 2014-05-13 05:31:26 · 957 阅读 · 0 评论 -
POJ3714 最近点对
变形了的最近点对,关键在于计算距离的时候,如果同类点的话,直接判定为无穷大即可。其他闲话:(1)因为一些原因,被迫暂时用回C++.(2)好久没刷题,忘记了数组一开始要开最大,多次new和delete,导致超时。(3) 感觉算法导论的最近点对没有考虑到有多个点都在一条vertical line上的情形。#include#include#include#include原创 2014-05-07 09:48:16 · 1659 阅读 · 0 评论 -
LeetCode - Symmetric Tree
A very interesting problem. At first if you have no idea how to do it, it will be very difficult. Once you got it, you will find out that you only need to traverse the tree twice. First is travelling原创 2014-02-10 07:03:54 · 987 阅读 · 0 评论 -
LeetCode的LastWord
其实是道水题,但是就是因为是水题,大意了,没有注意各种极端情况。。比如"", " ", "a b ","a b "吸取教训。。import java.util.*;public class LastWord { public int lengthOfLastWord(String s) {原创 2014-01-12 09:42:13 · 699 阅读 · 0 评论 -
LeetCode - Single Number
The kind of problem when you have no idea, it is very difficult. But once you know how to make it, it is very simple. Just use the XOR operation and go over the array. The numbers show up twice will d原创 2014-01-12 11:50:27 · 930 阅读 · 0 评论 -
LeetCode - Linked List Cycle
http://oj.leetcode.com/problems/linked-list-cycle/ A very classical interview question. The challenge exists in "without using extra space". So we use two pointers to go over the linked-list,原创 2014-01-12 10:41:01 · 1165 阅读 · 0 评论 -
LeetCode - WordBreak2
有意思的题目。。其实可以用动态规划,但是输出稍微麻烦。 动规公式,设置a[i][j]为字符串从i到j的组合数 初始化a[i][j] = 0;if(a[i][j] is a word) a[i][j] = 1; 推导for(int k=i;k a[i][j] = a[i][j] + a[i][k] * a[k+1][j] ;我这里用了递归,字符串st原创 2014-01-12 09:40:19 · 1172 阅读 · 0 评论 -
Leetcode - CopyWithRandomList
Algorithm: Iterate copy the original list first. For the random pointer, copy its original value(address) first. And during the iterate, use a map to store each node's original address and the corresp原创 2014-10-09 00:39:28 · 1132 阅读 · 0 评论 -
Leetcode - linked list circle 2
貌似国内面试经常问得题目,以前没遇到过。。思路:1: 两个指针,一个一次走两格,一个一次走一个。相遇了证明有环。2: 然后因为快的那个总是比慢的那个走快一步,当他们相遇后,再走k步(k是circle的结点数量)后,他们会再相遇,从而求出circle结点数量。3: 最后再设置两个指针(都是一次走一步),但一开始一个比另外一个领先k步,那么它们就会刚好在环的起点相遇。。但是原创 2014-10-08 23:28:09 · 1352 阅读 · 0 评论 -
Leetcode - Single Number II
The key is to use two constant space(32 bits) to store how many times 1 or 0 showed up in the bit i. If times of 1 in bit i is not the multiple of 3, then the unique value's bit i is 1. Otherwise the原创 2014-10-09 11:36:14 · 843 阅读 · 0 评论 -
Leetcode_Restor IP Address
简单的枚举,关键是corner case。 分成四段,不能多不能少,每段的数字只能从0-255,但是注意0开头的情况,"01","025"都是不行。import java.util.*;public class Solution { public List restoreIpAddresses(String s) { List ips = new LinkedLis原创 2015-01-24 10:37:17 · 747 阅读 · 0 评论 -
leetcode - Search in Rotated Sorted Array II
有duplicate的确会有影响,最坏情况下复杂度为O(n)。产生影响的例子(1)5,5,5,5,9,5(2)5,9,5,5,5,5这种情况下没有办法知道该如何二分。只能简单枚举了,两种枚举办法:(1)枚举最左端(l++) 或者最右端(r--)(2)我这里的策略比较暴力,当没法二分时候,我直接从左到右遍历一边,看看target在不在。public class S原创 2015-02-08 12:51:58 · 773 阅读 · 0 评论 -
Leetcode subsets2
第一次提交犯了个常见错误,调用下面for循环的时候,在for里面修改了subsets,这样子会抛异常。以后得注意,调用iteractor来遍历一个collection时候,不能在遍历里面改变这个collection的长度(增加,删除节点)。for(List subset : subsets){import java.util.*;public class Solution {原创 2014-12-30 13:56:32 · 915 阅读 · 0 评论 -
Cut the tree _ HackerRank
有意思的题目,但是题目描述不正确(貌似阿三哥出的题目),让我走了一些弯路,很tmd无语。。输入严格来说根本不是树,是图来的,因为边是无向边。。但是它的输入保证了是没有环路的图,所以某种程度上可以看做是树(任何一个节点都可以为根节点,然后递归地把每个节点的邻居看做子节点)。不过话说回来如果一开始告诉我这个图,我还真想不出来解法。。但是因为它“误导”了我,让我认为它是一颗树后,问题就简单了原创 2015-01-18 13:27:11 · 1332 阅读 · 0 评论 -
Similar Pair _ HackerRank
Hacker rank真的比leetcode 难了不止一个等级。。这题有点巧妙。。深度搜索每条路径,然后枚举,基本很多人都想的出来,但是关键在于这样肯定超时。巧妙之处在于要给每条路径建立一个线段树来加速查询,每次similar查询复杂度从O(h)变成O(lgh)。。犯了两个错误(1)要用long来存储线段树,已经可能的similar pairs。 (2)值减去T可能原创 2015-01-18 05:44:56 · 2576 阅读 · 0 评论 -
merge interval
常规做法,排序然后合并。import java.util.*;public class Solution { class IntervalComp implements Comparator{ @Override public int compare(Interval inte1, Interval inte2) { if(inte1原创 2014-11-22 12:24:10 · 894 阅读 · 0 评论 -
Leetcode Permutation2
去重的关键在于同样的数字,序号必须总是升序(或者 )原创 2014-11-24 10:40:50 · 980 阅读 · 0 评论 -
Leetcode - insert interval
这题其实出的不好,没有说清楚Limport java.util.*; public class Solution { int bSearch(List intervals, int val) { int n = intervals.size() ; if(val < intervals.get(0).start) { return -原创 2014-11-22 10:59:59 · 604 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
明白了上一题是求最大的连续子数组之和后,这题就更加简单了,遇到小于0的就不要加。public class Solution { public int maxProfit(int[] prices) { if(prices.length < 2) return 0; int n = prices.length;原创 2014-10-18 23:17:20 · 918 阅读 · 0 评论 -
Leetcode - Best Time to Buy and Sell Stock
知道是求连续最大子数组后就简单了。但是注意边界条件,如果最大子数组之和<0,那就不要交易了, 返回0.public class Solution { public int maxProfit(int[] prices) { if(prices.length < 2) return 0; int n = prices.原创 2014-10-18 23:09:05 · 937 阅读 · 0 评论 -
Leetcode- LRUCache
关键是搞懂题目(不知道LRUCache的只能自己google了)。然后用双向链表来模拟cache被get和set。但是naive implementation会exceed time limit。所以最大的关键是用一个HashMap来记录key在链表中的位置,这样子每次查询是O(1)的时间,否则O(n)。这个也是很经典的用Map来加速双向链表查询的思路(前提是key要唯一)。i原创 2014-10-18 12:19:27 · 1490 阅读 · 0 评论 -
WordladderII
import java.util.*;public class Solution { List> travelPath(String start, String cstr, Map> prevs_map){ List> paths = new LinkedList>(); if(start.equals(cstr)) { List path = new L原创 2014-11-01 10:54:07 · 927 阅读 · 0 评论 -
Leetcode - Evaluate Reverse Polish Notation
初看貌似有点复杂,但是搞懂了非常简单,就一个简单的栈应用,每次遇到计算符号"+", "-", "*", "/"就将栈顶端两个数字出栈,计算后再将结果压栈即可。。#include#include#includeusing namespace std;class Solution {public: int evalRPN(vector &tokens) { stack s原创 2014-10-03 12:22:01 · 803 阅读 · 0 评论 -
Leetcode - GasStation
An kind of interesting problem. Use an array arr[] to store how many gas are left if we travel from station i to station i+1, arr[i] = gas[i] - cost[i]. Then it becomes an variance of the maximal sub原创 2014-10-12 05:14:30 · 941 阅读 · 0 评论 -
Leetcode - Insertion Sort List
It is quite a basic problem. However I spent half an hour solving it and made two mistakes. Guess it's because I haven't written some fundamental data structures like linked list for a while and kind原创 2014-10-12 06:27:40 · 945 阅读 · 0 评论 -
DP Leetcode - Maximum Product Subarray
最近一直忙着写paper,很久没做题,一下子把题目搞复杂了。。思路理清楚了非常简单,每次只需更新2个值:最大乘积和最小乘积。最大乘积被更新有三种可能:当前A[i]>0,乘以前面最大的数(>0),得到新的最大乘积;当前A[i]0,(A[i-1]==0。最小乘积同理。。class Solution {public: int Max(int a, int b, int c) {原创 2014-10-02 00:55:11 · 1386 阅读 · 0 评论 -
Leetcode - WordBreak
试着写了个前缀树,352 ms。 其中最大的trick是要用一个数组 boolean[] searchFlag来记录从s[i]开始的子串(s[i...n-1])是否被搜索过(s[i...n-1]能否由dict的单词组成)。如果被搜索过那就不用再搜索了(因为搜索失败了,如果成功的话程序早已结束)。import java.util.*;public class Solutio原创 2014-10-12 04:13:22 · 1105 阅读 · 0 评论 -
LeetCode - Surrounded Regions
之前都在poj玩,第一次刷LeetCode OJ的题目,求Surrounded Regions。原题http://oj.leetcode.com/problems/surrounded-regions/ 。 不知道其他人什么解法。个人认为思路的关键是任何一个O如果它没有被X包围,那么它一定和最外面的边界的某个O是连通的。反过来,也就是可以从最外面那层所有的O开始用广度搜索所有没有被包围的O。原创 2013-11-03 10:47:39 · 1945 阅读 · 0 评论 -
求两个排好序的数组中的第k小数字
最近各种原因一直看论文做ppt,觉得还是得养成刷题的习惯,保持思维的活跃度,而且可以多手准备。 很多人推荐LeetCode,第一次玩,OJ的题目太少。去看了讨论版,随便选了道容易上手的,给定两个排好序的数组,找出第k小的数字。 O( (m+n)log(m+n))的算法应该没人会用吧。 O(k)的算法应该很多人都能想到。 O(log(k))的算法有点难想到。不过想通了发现思维原创 2013-10-28 09:54:20 · 1195 阅读 · 0 评论