摘要:Maintain a hashset with size of (K + 1)class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { unordered_set hs; ...
阅读全文
05 2015 档案
摘要:Good one. My first reaction to this problem is the same idea as this answer:https://leetcode.com/discuss/37365/accepted-c-solution-o-nlogn-using-maxhe...
阅读全文
摘要:I guess the punch line of this one is Sieving for primes.#include #include #include #include #include #include #include #include using namespace std;v...
阅读全文
摘要:Apparently it is by a backwards derivation solution. Say energy at h[i] is e, the next energy at h[i+1] is 2*e - h[i+1] => e', so backwards, e = ceil(...
阅读全文
摘要:We match larger in array_a with smaller in array_b, by which we can have as even sum as possible, so we don't have too small sums.#include #include #i...
阅读全文
摘要:Apparently, more expensive flowers should be bought with lower coeff. (Greedy part), and any unbalanced assignment would cause extra cost.So the code ...
阅读全文
摘要:Typical recursion usage.class Solution { vector> ret; void dfs(vector curr, int currSum, int maxN, int n, int k) { // Ending ...
阅读全文
摘要:To have "shortest" expanded palindrome, we should know the longest "heading" palindrome in original string, so that we can reuse as much part as possi...
阅读全文
摘要:It is modulo version of "max sum of contiguous subarray". So similar pattern can be applied too, but with tricks regarding to modulo ops.I checked edi...
阅读全文
摘要:Good one to learn Binary Indexed Tree (Fenwick Tree). Simply grabbed the editorial's code but with comments.#include #include #include #include #inclu...
阅读全文
摘要:A high quality DP problem to work on. It is not an merely educational DP, it is subtle.Basic ideas: 1. Run original DP twice, 1st with num[0] selecte...
阅读全文
摘要:Trie, again.class TrieNode {public: // Initialize your data structure here. TrieNode() : prev(nullptr), c(0), bIsLast(false) { } TrieNo...
阅读全文
摘要:I'm glad to see that LeetCode has finally realized the importance of Trie.My C++ code can be further optimized..class TrieNode {public: // Initiali...
阅读全文
摘要:An ACM-level problem because it involves "advanced maths". It should not be marked as "Moderate". Other than that, it is a medium level DP one.*Math m...
阅读全文
摘要:This is my 1st 3D DP problem, and it should be an educational one. But, still tricky to me.Here is a good article:http://www.cnblogs.com/sunshineatnoo...
阅读全文
摘要:Basically speaking, I got higher resolution view into code now, in a 庖丁解牛 style. Previously, I treated one program as a whole, and sometimes I got con...
阅读全文
摘要:This one lets you print out one topological sorted sequence. Either BFS or DFS works. But there are tricky details.This is my reference:https://leetco...
阅读全文
摘要:O(n): Sliding window:class Solution {public: int minSubArrayLen(int s, vector& nums) { int len = nums.size(); if (len == 0) return...
阅读全文
摘要:Topological sorting. This is my DFS solution. Please note that I used _rec to avoid duplicate checking.class Solution {public: map rec; set _rec...
阅读全文
摘要:Implementation problem.class TrieNode {public: // Initialize your data structure here. TrieNode() { c = 0; bIsLast = false; } ...
阅读全文
摘要:Solution 1:https://www.hackerrank.com/challenges/number-list/editorialReversed thought: total no. of subsets - no. of subsets without arr[i] > kSoluti...
阅读全文
摘要:I treated it too difficult.. nothing special, just some optimization mentioned as:http://saraguru.weebly.com/how-i-solved/find-maximum-index-product-h...
阅读全文
摘要:It requires O(nlgn) solution. And actually I think the passing code is for "non-decreasing"..#include #include #include #include #include #include #in...
阅读全文
摘要:Really interesting problem. Naive solution would be O(n^3). But please note the pattern here: (i, j, k) -> (i + 1, j + 1, k) -> (i + 2, j + 2, k)... t...
阅读全文