Yesterday for the first time to participate in a Hackerrank game, from 11 o'clock Noon to do the night 12 o'clock, finally too tired, at the end of the game one hour to go to bed. Completely passed the first two questions, the third question for a sentence has not been correctly understood, so did not pass the majority of test case, the final ranking in the contest in 23**/60**. Oneself is holding the mentality of study to go, although this achievemen
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 knapsack. And some complext problem can be converted to a simpler one.Code is amazingly simple:#include #include#include#include#includeusing namespacestd;intKnapsack (vectorint> a,intG) { intn =a.size (); Vectorint> dp (G +1); for(inti =1; I ) for(
Classic and challenging dp! And you need combine several tricks together with DP-make it 100% pass.My Main reference is here:https://github.com/havelessbemore/hackerrank/blob/master/algorithms/bit_manipulation/ String-transmission.javaBasically it is a counting problem.It's easy-to-possible count should be SUM (CR (n, 0), CR (n, 1),.. Cr (n, k)) without the limitation of "No Periodic strings". And then we can count how many "periodic" strings there is
second by second. the problem is that the protein rings can be very long (up to 1 million proteins in a single ring) and they want to know the state of the ring after upto 109 seconds. thus their software takes too long to report the results. they ask you for your help.
Input FormatInput contains 2 lines.First line has 2 integers n and k, n being the length of the protein ring and K the desired number of seconds.Second line contains a string of length N containing uppercase letters A, B, C or
Question Link
Ashton appeared for a job interview and is asked the following question. arrange all the distinct substrings of a given string in lexicographical order and concatenate them. print the kth character of the concatenated string. it is assured that given value of K will be valid I. e. there will be a kth character. can you help Ashton out with this?
Input FormatFirst line will contain a number t I. e. Number of test cases.First line of each test case will contain a string containing
Similar Pair _ HackerRank, pairhackerrank
Hacker rank is really more difficult than leetcode ..
This question is a bit clever .. Each path is searched in depth and then enumerated. Most people think about it, but the key is that this definitely times out. The trick is to create a line segment tree for each path to accelerate the query. The complexity of each similar query changes from O (h) to O (lgh )..
Two mistakes were made.
(1) Use long to store
Greedy beats DP this time...
I tried several DP solutions first, but all failed with re \ TLE. If you 'feel' the problem, greedy shocould be working:
(A solution from discussion)
Def getiterator (n): While n> 0: If n % 3 = 0: break; else: N-= 5 return n t = input () for I in xrange (t): n = int (input () rows = getrows (n) If rows
Hackerrank-Sherlock and the beast
Another fun greedy problem to work on:we simply go from first to second last person, as long someone is odd, we distribut E bread to she and her next.#include #includeusing namespacestd;intMain () {intN; CIN>>N; Vectorint>B (N); for(intB_i =0; B_i ) {cin>>B[b_i]; } intCNT =0; for(inti =0; I 1; i + +) { if(B[i]%2) {B[i]++; B[i+1]++; CNT+=2; } } if(B.back ()%2) cout"NO"Endl; ElsecoutEndl; return 0;}Hackerrank "Fair rations"
An intuitive Prim algorithm impl.#include #include#include#include#includeusing namespacestd;structedge{Edge (): s (0), T (0), D (0{} Edge (unsigned RS, unsigned rt, unsigned rd): s (rs), T (RT), D (RD) {} unsigned s; unsigned t; unsigned D; BOOL operator()(ConstEdge e1,ConstEdge E2) { returne1.d >e2.d; }};intMain () {LongN, M; CIN >> N >>m; //From -to-and (length, id)unordered_mapG; for(inti =0; I ) {Unsigned A, B, D; CIN>> a >> b >>D; G[A][B]= G[b][a] =D; } unsigned s; CIN>>s; Un
"How many inverted pairs"-so usually ends up with mergesort solution (of course there is other solutions out there)defmergesort (arr):ifLen (arr) = = 1: return0, arr mid= Len (arr)//2Cnt1, arr1=mergesort (Arr[:mid]) Cnt2, arr2=mergesort (arr[mid:])#RET =[] CNT=0 I=0 J=0 Inx=0 whileI andJ Len (arr2):ifArr1[i] Arr2[j]: Ret.append (arr1[i]) I+ = 1elifARR2[J] Arr1[i]: Ret.append (arr2[j]) CNT+ = Len (arr1)-I#arr2-arr1J + = 1 whileI Len (arr1): Ret.append (Arr1[i]) I+ = 1 whileJ Len (arr2): R
I caught the sparkle in my mind and got AC1! It is a great great experience!So the basic Idea:permute on 3 consecutive items doesn ' t change the parity of the No. of inversions. Each permutation can is only remove 0 or 2 inversions. So we say "YES" when No. of inversion% 2 = = 0. And we use MergeSort to count it in O (NLGN).RET =0defmerge (arr1, arr2):Globalretif notARR1:returnarr2if notARR2:returnarr1 forV2inchArr2:arr1.append (v2) I= Len (arr1)-1 while(I >0):ifArr1[i] ]: Arr1[i-1],arr1[i] =
This is the classic LCS problem. Since it requires you-to-print one longest common subsequence, just use the O (m*n)-space version here.My accepted code is as follows.1#include 2#include 3#include 4 5 using namespacestd;6 7vectorint> LCS (vectorint> A, vectorint>b) {8 intn = a.size (), M =b.size ();9vectorint> > DP (n +1, vectorint> (M +1,0));Ten for(inti =1; I ) { One for(intj =1; J ) { A if(A[i-1] = = B[j-1]) Dp[i][j] = dp[i-1][j-1] +1; - ElseDP[I][J]
attended by up to a total.If m=1, of course, according to the end time sort, can.Here, simply deform, set b[j] for the point where the J-person is currently located (initially 0), for the first meeting, if there are more than one person to meet the criteria, select B[j] the largest one.Attached code:1#include 2#include 3#include 4#include 5#include 6 using namespacestd;7 8typedef pairint,int>PII;9 #defineF FirstTen #defineS Second One A intMain () { - intN, P; -scanf"%d%d", n, P); thePII t
It requires O (NLGN) solution. And actually I think the passing code is for "Non-decreasing".#include #include#include#include#includestring>#include#includeusing namespacestd;intMain () {intT CIN >>T; Vectorint>arr (t); for(inti =0; I ) Cin>>Arr[i]; Vectorint> dp (t,1); //C[i]: Smallest last elem of a LIS seq with lenghth ivectorint> C (1, arr[0]); intRET =1; for(inti =1; I ) { if(Arr[i] 0]) {c[0] =Arr[i]; Dp[i]=1; } Else if(Arr[i] >=C.back ()) {C.push_back (arr[i]
must be a O (n) solution, and there is. What's better than a standard DP in cerntain cases? Greedy.#include #include#include#includeusing namespacestd;#defineREP (i, S, N) for (int i = s; i typedefLong LongLL; LL Calc (Vectorinch) {size_t len=inch. Size (); LL ret=0; Std::reverse (inch. Begin (),inch. End ()); LL Peak= -1; REP (i,0, Len) { if(inch[I] >Peak) {Peak=inch[i]; } Else{ret+ = peak-inch[i]; } } returnret;}intMain () {intT CIN >>T; while(t--) { intN CIN >>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.