传送门 Sherlock and MiniMax Authored by darkshadows on May 07 2014 Problem Statement Watson gives Sherlock an array $A_1,A_2\cdots A_N$. He asks him to find an integer $M$ between $P$ and $Q$ (both inclusive), such that,$\min \{|A_i-M|, 1\le i\le N\}$ i…
题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For…
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the solution from this link:https://github.com/wangyongliang/Algorithm/blob/master/hackerrank/Strings/Square%20Subsequences/main.cc And here is his code with my…
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial gives a very neat BFS based idea which costs much less memory. https://www.hackerrank.com/challenges/beautiful-path/editorial…
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/CharlesOfria), it looks pretty much like a Brutal-Force (or simulation) based solution, mixed with some greedy strategies. To me the other NPC "Queens Revi…
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchanana on Jun 16 2015 Problem Statement You are given an integer N. Print the factorial of this number. N!=N×(N−1)×(N−2)×⋯×3×2×1 Note: Factorials of N>20…
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hackers/turuthok/download_solution Basically it is memorized search application. And we follow a discrete strategy: split it into digits and go digit by di…
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah! So the idea is straight forward: 1. sort the input array and calculate partial_sum()2. find the negative\positive boundary with the accumulated give…
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/tree/master/hackerrank/algorithms/the-indian-jobLesson learnt: The Italian\Indian job is two-way 01 Knapsack. And some complext problem can be convert…
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here: https://codepair.hackerrank.com/paper/5fIoGg74?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6IkJsdWVCaXJkMjI0IiwiZW1haWwiOiJoZWFsdGh5dG9ueUBnbWFpbC5jb20ifQ%…
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreimaximov/hacker-rank/blob/master/data-structures/tree/self-balancing-tree/main.cpp node *create_node(int val) { node *pNew = new node; pNew->val = val; p…
https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam 学习了线段相交的判断法.首先是叉乘,叉乘的几何意义是有向的平行四边形的面积(除以2就是三角形的面积).如果ABD和ABC正负相反,说明C和D在AB两侧,同样的,再判断A和B是否在CD两侧即可.当某三角形面积为0时,需要判断是否在线段上. #include <iostream> using namespace std; typedef long long…
https://www.hackerrank.com/challenges/service-lane 用RMQ做的,其实暴力也能过~ #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { int n, t; cin >> n >> t; vector<int> vec(n); for (int i = 0; i…
https://www.hackerrank.com/contests/w1/challenges/maximizing-xor/ 找了半天规律,答案竟然是暴力,伤感.我找到的方法是利用规律2^x XOR 2^x - 1会最大,感觉稍微效率高点. int maxXor(int l, int r) { if (l == r) return 0; int p = 1; for (int i = 0; i <= 10; i++) { if (p * 2 > r) break; p *= 2; } i…
https://www.hackerrank.com/contests/w2/challenges/manasa-and-stones 简单题. #include<iostream> using namespace std; int main() { int T; cin >> T; while (T--) { int n, a, b; cin >> n >> a >> b; // a < b if ( a > b) { int tm…
https://www.hackerrank.com/contests/w4/challenges/crush 第一眼觉得要用线段树,但据说会超时.其实这个可以通过生成pair排序来做. #include <vector> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; int main() { int N, M; cin >> N…
https://www.hackerrank.com/contests/w5/challenges/closest-number 简单题. #include <iostream> #include <cmath> using namespace std; int main() { int T; cin >> T; while (T--) { int a, b, x; cin >> a >> b >> x; if (b < 0 &…
https://www.hackerrank.com/contests/w7/challenges/die-hard-3 首先,发现c <= max(a, b) 而且 c = aX + bY时有解.然后根据扩展欧几里得算法知道c % gcd(a, b) == 0时有解. #include <vector> #include <iostream> using namespace std; int gcd(int a, int b) { if (a % b == 0) { ret…
https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list 简单题,GCD和LCM. #include <vector> #include <iostream> using namespace std; int gcd(int a, int b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } int lcm(int a, i…