Uncategorized

Unordered Swaps solution codeforces

  Unordered Swaps solution codeforces Alice had a permutation pp of numbers from 11 to nn. Alice can swap a pair (x,y)(x,y) which means swapping elements at positions xx and yy in pp (i.e. swap pxpx and pypy). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them …

Unordered Swaps solution codeforces Read More »

Circular Spanning Tree solution codeforces

  Circular Spanning Tree solution codeforces There are nn nodes arranged in a circle numbered from 11 to nn in the clockwise order. You are also given a binary string ss of length nn. Your task is to construct a tree on the given nn nodes satisfying the two conditions below or report that there such tree does not exist: For each node ii (1≤i≤n)(1≤i≤n), the degree of …

Circular Spanning Tree solution codeforces Read More »

AND Sorting solution codeforces

  AND Sorting solution codeforces You are given a permutation pp of integers from 00 to n−1n−1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1pi>pi+1 for at least one 1≤i≤n−11≤i≤n−1). The permutation is called XX-sortable for some non-negative integer XX if it is possible to sort the permutation by performing the operation below some finite number of times: Choose two indices ii and jj (1≤i<j≤n)(1≤i<j≤n) such that pi&pj=Xpi&pj=X. …

AND Sorting solution codeforces Read More »

LIS or Reverse LIS? solution codeforces

  LIS or Reverse LIS? solution codeforces You are given an array aa of nn positive integers. Let LIS(a)LIS(a) denote the length of longest strictly increasing subsequence of aa. For example, LIS([2,1–,1,3–])LIS([2,1_,1,3_]) = 22. LIS([3–,5–,10–––,20–––])LIS([3_,5_,10_,20_]) = 44. LIS([3,1–,2–,4–])LIS([3,1_,2_,4_]) = 33. We define array a′a′ as the array obtained after reversing the array aa i.e. a′=[an,an−1,…,a1]a′=[an,an−1,…,a1]. The beauty of array aa is defined as min(LIS(a),LIS(a′))min(LIS(a),LIS(a′)). Your task is to determine the maximum possible beauty of the array aa if you can …

LIS or Reverse LIS? solution codeforces Read More »

MCMF? solution codeforces

  MCMF? solution codeforces You are given two integer arrays aa and bb (bi≠0bi≠0 and |bi|≤109|bi|≤109). Array aa is sorted in non-decreasing order. The cost of a subarray a[l:r]a[l:r] is defined as follows: If ∑j=lrbj≠0∑j=lrbj≠0, then the cost is not defined. Otherwise: Construct a bipartite flow graph with r−l+1r−l+1 vertices, labeled from ll to rr, with all vertices having bi<0bi<0 on the left and those with bi>0bi>0 on right. For each i,ji,j such that l≤i,j≤rl≤i,j≤r, bi<0bi<0 and bj>0bj>0, draw an edge from ii to jj with infinite …

MCMF? solution codeforces Read More »

Palindromic Indices solution codeforces

  Palindromic Indices solution codeforces You are given a palindromic string ss of length nn. You have to count the number of indices ii (1≤i≤n)(1≤i≤n) such that the string after removing sisi from ss still remains a palindrome. For example, consider ss = “aba” If we rem ove s1s1 from ss, the string becomes “ba” which is not a palindrome. If we remove s2s2 from ss, the string becomes “aa” which is a palindrome. If …

Palindromic Indices solution codeforces Read More »

Check if There Is a Valid Parentheses String Path solution leetcode

Check if There Is a Valid Parentheses String Path solution leetcode A parentheses string is a non-empty string consisting only of ‘(‘ and ‘)’. It is valid if any of the following conditions is true: It is (). It can be written as AB (A concatenated with B), where A and B are valid parentheses strings. It can be written as (A), where A is a valid parentheses string. You are given an m x n matrix of parentheses grid. …

Check if There Is a Valid Parentheses String Path solution leetcode Read More »

Count Number of Texts solution leetcode

Count Number of Texts solution leetcode Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below. In order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the letter in the key. For example, to add the letter ‘s’, Alice has to press ‘7’ four times. …

Count Number of Texts solution leetcode Read More »

Count Nodes Equal to Average of Subtree solution leetcode

Count Nodes Equal to Average of Subtree solution leetcode Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer. A subtree of root is a tree consisting of root and all of its descendants. Count Nodes Equal …

Count Nodes Equal to Average of Subtree solution leetcode Read More »

Largest 3-Same-Digit Number in String solution leetcode

Largest 3-Same-Digit Number in String solution leetcode You are given a string num representing a large integer. An integer is good if it meets the following conditions: It is a substring of num with length 3. It consists of only one unique digit. Return the maximum good integer as a string or an empty string “” if no such integer exists. Note: A substring is a contiguous sequence of characters within a string. …

Largest 3-Same-Digit Number in String solution leetcode Read More »