摘要:One pass in-place solution: all swaps.class Solution {public: /** * @param nums: a vector of integers * @return: nothing */ void partit...
阅读全文
08 2015 档案
摘要:A variation to a classical DP: LCS.class Solution {public: /** * @param A an integer array * @return A list of integers includes the index o...
阅读全文
摘要:It can be solved based on the code from "Strobogrammatic Number II". The idea is pretty straight forward - binary search the boundaries.class Solution...
阅读全文
摘要:This is abouthttps://en.wikipedia.org/wiki/Run-length_encoding. The trick is, for a valid char, we only compress up to 254 occurences - count 255 mean...
阅读全文
摘要:Key: each of 2\3\5 is trying to multiply with the least number it has not been multiplied.class Solution {public: int nthUglyNumber(int n) { ...
阅读全文
摘要:Another topological sorting problem. Note: the DFS one is like a 'post-order' traversal.class Solution { unordered_map> g; unordered_set visited...
阅读全文
摘要:A variation of 3Sum. And the trick is, (i& nums, int target) { size_t len = nums.size(); if(len = target) k--; ...
阅读全文
摘要:Use constructive strategy: we compose each of the 26 variations of one word.class Solution {public: vector> groupStrings(vector& strings) { ...
阅读全文
摘要:I learnt a clean idea: all read is from the 4-char buffer.int read4(char *buf);class Solution { int offset; int validLen; char _buf[4]; ...
阅读全文
摘要:An interesting DFS with a lot of details to take care of.class Solution { vector> ret; void go(int n, vector sofar, int minF) { int st...
阅读全文
摘要:Classic DP! For house[i] to pick a min-color from dp[i-1], we only need to check if color j is the min cost color index of dp[i - 1]; if yes, then we ...
阅读全文
摘要:Simply store indices in ctor.class WordDistance { unordered_map> hm;public: WordDistance(vector& words) { for (int i = 0; i &vec1 = hm[w...
阅读全文
摘要:Just take care of corner cases..class Vector2D { vector> &r; int i, i0;public: Vector2D(vector>& vec2d) : r(vec2d), i(0), i0(0) { i...
阅读全文
摘要:Linear scan after sorting. Please note sorting detail.typedef std::pair Rec;class Solution {public: int minMeetingRooms(vector& intervals) { ...
阅读全文
摘要:A typical DPclass Solution {public: int minCost(vector>& costs) { size_t len = costs.size(); if(len == 0) return 0; ve...
阅读全文
摘要:A typical sliding window solution.class Solution { int rec[26]; // for a-z int rec1[26]; // for A-Z int cnt; int &getCnt(char c) ...
阅读全文
摘要:DFS to check cyclic. Please note some details.class Solution{ unordered_map> g; unordered_set visited; bool go(int i, int p) // true - cy...
阅读全文
摘要:A variation of linear scan..typedef pair Rec;class Solution {public: int shortestDistance(vector& words, string word1, string word2) { v...
阅读全文
摘要:A typical recursion one.class Solution { bool _verify(vector &in, int i, int j, int low, int high) { if (i > j) return tr...
阅读全文
摘要:Recursion, a natural thought:class Solution { // Top : BottomRight std::pair rotate(TreeNode *p) { if (!p->left && !p->right) ...
阅读全文
摘要:It should be categorized as 'Advanced' I think ... Anyway, Fenwick tree is the key.Editorial:https://www.hackerrank.com/challenges/candles-2/editorial...
阅读全文
摘要:A trickier Dijkstra.#include #include #include #include #include #include #include #include #include using namespace std;const int INF = std::numeric_...
阅读全文
摘要:Typical Floyd-Walshall Algorithm.#include #include #include #include #include #include #include #include #include using namespace std;const long DIST_...
阅读全文
摘要:No big difference with "Breadth First Search: Shortest Reach", but this statement is crucial:If there are edges between the same pair of nodes with di...
阅读全文
摘要:Typical Dijstra algorithm impl. one.#include #include #include #include #include #include #include #include #include using namespace std;const long DI...
阅读全文
摘要:A combination of different DP passes.https://www.hackerrank.com/challenges/lego-blocks/editorial#include #include #include using namespace std;typedef...
阅读全文
摘要:My initial thought was recursive (a op b) evaluation, which doesn't work quite well.The correct way is to split by op. And, we can cache calculaton re...
阅读全文