Skip to content

Commit 41ed6ab

Browse files
committed
🚀 18-Dec-2020
1 parent a3b7930 commit 41ed6ab

11 files changed

+712
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
A string b is a subsequence of a string a if b can be obtained from a by deletion of several (possibly, zero or all) characters. For example, "xy" is a subsequence of "xzyw" and "xy", but not "yx".
3+
4+
You are given a string a. Your task is to reorder the characters of a so that "trygub" is not a subsequence of the resulting string.
5+
6+
In other words, you should find a string b which is a permutation of symbols of the string a and "trygub" is not a subsequence of b.
7+
8+
We have a truly marvelous proof that any string can be arranged not to contain "trygub" as a subsequence, but this problem statement is too short to contain it.
9+
10+
Input
11+
The first line contains a single integer t (1≤t≤100) — the number of test cases.
12+
13+
The first line of each test case contains a single integer n (1≤n≤200) — the length of a.
14+
15+
The next line contains the string a of length n, consisting of lowercase English letters.
16+
17+
Output
18+
For each test case, output a string b of length n which is a permutation of characters of the string a, and such that "trygub" is not a subsequence of it.
19+
20+
If there exist multiple possible strings b, you can print any.
21+
22+
Example
23+
inputCopy
24+
3
25+
11
26+
antontrygub
27+
15
28+
bestcoordinator
29+
19
30+
trywatchinggurabruh
31+
outputCopy
32+
bugyrtnotna
33+
bestcoordinator
34+
bruhtrywatchinggura
35+
Note
36+
In the first test case, "bugyrtnotna" does not contain "trygub" as a subsequence. It does contain the letters of "trygub", but not in the correct order, so it is not a subsequence.
37+
38+
In the second test case, we did not change the order of characters because it is not needed.
39+
40+
In the third test case, "bruhtrywatchinggura" does contain "trygu" as a subsequence, but not "trygub".
41+
*/
42+
43+
44+
#include<bits/stdc++.h>
45+
using namespace std;
46+
47+
int main(){
48+
ios_base::sync_with_stdio(false);
49+
cin.tie(NULL);
50+
51+
int t;
52+
cin>>t;
53+
while(t--){
54+
int n;
55+
cin>>n;
56+
string s;
57+
cin>>s;
58+
int cntb=0;
59+
for(auto &ch: s)
60+
if(ch=='b') cntb++;
61+
string res="";
62+
if(cntb==0) cout<<s<<endl;
63+
else {
64+
res.append(cntb, 'b');
65+
for(auto &ch: s){
66+
if(ch!='b') res+=ch;
67+
}
68+
cout<<res<<endl;
69+
}
70+
}
71+
72+
73+
return 0;
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
One fall day Joe got bored because he couldn't find himself something interesting to do. Marty suggested Joe to generate a string of length n to entertain him somehow. It didn't seem particularly difficult, but Joe's generated string had to follow these rules:
3+
4+
the string may only contain characters 'a', 'b', or 'c';
5+
the maximum length of a substring of this string that is a palindrome does not exceed k.
6+
7+
A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. For example, strings "a", "bc", "abc" are substrings of a string "abc", while strings "ac", "ba", "cba" are not.
8+
9+
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, strings "abccba", "abbba", "aba", "abacaba", "a", and "bacab" are palindromes, while strings "abcbba", "abb", and "ab" are not.
10+
11+
Now Joe wants to find any correct string. Help him! It can be proven that the answer always exists under the given constraints.
12+
13+
Input
14+
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤10).
15+
16+
The only line of each test case contains two integers n and k (1≤k≤n≤1000) — the required string length and the maximum length of a palindrome substring, respectively.
17+
18+
Output
19+
For each test case, print any string that satisfies the conditions from the problem statement. If there are multiple correct answers, you can print any one of them. It can be proven that the answer always exists under the given constraints.
20+
21+
Example
22+
inputCopy
23+
2
24+
3 2
25+
4 1
26+
outputCopy
27+
aab
28+
acba
29+
Note
30+
In the first test case of the example, the palindrome substring with the maximum length is "aa". Its length does not exceed 2, so it fits.
31+
32+
In the second test case all palindrome substrings have the length one.
33+
34+
*/
35+
36+
37+
#include<bits/stdc++.h>
38+
using namespace std;
39+
40+
int main(){
41+
ios_base::sync_with_stdio(false);
42+
cin.tie(NULL);
43+
44+
int t;
45+
cin>>t;
46+
while(t--){
47+
int n, k;
48+
cin>>n>>k;
49+
50+
string tmp="abc";
51+
string res="";
52+
int start=0;
53+
54+
for(int i=0;i<n;i++){
55+
int j=k;
56+
while(j-- && res.length()<n){
57+
res+=tmp[start%3];
58+
}
59+
start++;
60+
}
61+
62+
cout<<res<<endl;
63+
}
64+
65+
return 0;
66+
}
67+
68+
69+
70+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Find and Print All The Prime Numbers Between L and R (Both L and R Inclusive)
3+
4+
Input Format
5+
6+
Take Input Value of L and R
7+
8+
Output Format
9+
10+
Print All The Prime Number Between L and R (Both L and R inclusive)
11+
12+
Constraints
13+
14+
0 < L < 10001
15+
0 < R < 10001
16+
L <= R
17+
SAMPLE INPUT
18+
2 10
19+
SAMPLE OUTPUT
20+
2 3 5 7
21+
22+
*/
23+
24+
25+
26+
27+
28+
29+
#include<bits/stdc++.h>
30+
using namespace std;
31+
32+
void seiveErato(int start, int range, vector <int> &prime){
33+
vector <bool> seive(range + 1, true);
34+
seive[0] = false;
35+
seive[1] = false;
36+
for(int i = 2; i * i <= range; i++){
37+
if(seive[i]){
38+
for(int j = i * i; j <= range; j += i)
39+
seive[j] = false;
40+
}
41+
}
42+
43+
for(int i = start; i <= range; i++)
44+
if(seive[i])
45+
prime.push_back(i);
46+
}
47+
48+
int main(){
49+
ios_base::sync_with_stdio(false);
50+
cin.tie(NULL);
51+
52+
int l, r;
53+
cin >> l >> r;
54+
55+
vector <int> prime;
56+
seiveErato(l, r, prime);
57+
58+
for(auto &x: prime) cout << x << " ";
59+
60+
return 0;
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
2+
3+
Do not allocate extra space for another array; you must do this by modifying the input array in-place with O(1) extra memory.
4+
5+
Clarification:
6+
7+
Confused why the returned value is an integer, but your answer is an array?
8+
9+
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller.
10+
11+
Internally you can think of this:
12+
13+
// nums is passed in by reference. (i.e., without making a copy)
14+
int len = removeDuplicates(nums);
15+
16+
// any modification to nums in your function would be known by the caller.
17+
// using the length returned by your function, it prints the first len elements.
18+
for (int i = 0; i < len; i++) {
19+
print(nums[i]);
20+
}
21+
22+
23+
Example 1:
24+
25+
Input: nums = [1,1,1,2,2,3]
26+
Output: 5, nums = [1,1,2,2,3]
27+
Explanation: Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned length.
28+
Example 2:
29+
30+
Input: nums = [0,0,1,1,1,1,2,3,3]
31+
Output: 7, nums = [0,0,1,1,2,3,3]
32+
Explanation: Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length.
33+
34+
35+
Constraints:
36+
37+
0 <= nums.length <= 3 * 104
38+
-104 <= nums[i] <= 104
39+
nums is sorted in ascending order.
40+
41+
42+
43+
44+
45+
46+
class Solution {
47+
public:
48+
int removeDuplicates(vector<int>& nums) {
49+
int n=nums.size();
50+
if(n<=2) return nums.size();
51+
52+
int cnt=1, start=0;
53+
for(int i=0;i<n-1;i++){
54+
if(nums[i]==nums[i+1]){
55+
cnt++;
56+
} else {
57+
for(int j=0;j<cnt && j<2;j++)
58+
nums[start++]=nums[i];
59+
cnt=1;
60+
}
61+
}
62+
63+
for(int j=0;j<cnt && j<2;j++)
64+
nums[start++]=nums[n-1];
65+
66+
67+
nums.resize(start);
68+
69+
return nums.size();
70+
}
71+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Given the root of a binary tree, the depth of each node is the shortest distance to the root.
2+
3+
Return the smallest subtree such that it contains all the deepest nodes in the original tree.
4+
5+
A node is called the deepest if it has the largest depth possible among any node in the entire tree.
6+
7+
The subtree of a node is tree consisting of that node, plus the set of all descendants of that node.
8+
9+
Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
10+
11+
12+
13+
Example 1:
14+
15+
16+
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
17+
Output: [2,7,4]
18+
Explanation: We return the node with value 2, colored in yellow in the diagram.
19+
The nodes coloured in blue are the deepest nodes of the tree.
20+
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
21+
Example 2:
22+
23+
Input: root = [1]
24+
Output: [1]
25+
Explanation: The root is the deepest node in the tree.
26+
Example 3:
27+
28+
Input: root = [0,1,3,null,2]
29+
Output: [2]
30+
Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
31+
32+
33+
Constraints:
34+
35+
The number of nodes in the tree will be in the range [1, 500].
36+
0 <= Node.val <= 500
37+
The values of the nodes in the tree are unique.
38+
39+
40+
41+
42+
/**
43+
* Definition for a binary tree node.
44+
* struct TreeNode {
45+
* int val;
46+
* TreeNode *left;
47+
* TreeNode *right;
48+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
49+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
50+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
51+
* };
52+
*/
53+
class Solution {
54+
public:
55+
unordered_map<TreeNode*, int> mp;
56+
int max_depth;
57+
58+
void dfs(TreeNode* root, TreeNode* parent){
59+
if(root){
60+
mp.insert({root, mp[parent]+1});
61+
dfs(root->left, root);
62+
dfs(root->right, root);
63+
}
64+
}
65+
66+
TreeNode* answer(TreeNode* root){
67+
if(root==NULL || mp[root]==max_depth) return root;
68+
TreeNode* L=answer(root->left);
69+
TreeNode* R=answer(root->right);
70+
if(L && R) return root;
71+
if(L) return L;
72+
if(R) return R;
73+
return NULL;
74+
}
75+
76+
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
77+
mp.insert({NULL, -1});
78+
dfs(root, NULL);
79+
max_depth=-1;
80+
for(auto it=mp.begin();it!=mp.end();it++){
81+
max_depth=max(max_depth, it->second);
82+
}
83+
return answer(root);
84+
}
85+
};

0 commit comments

Comments
 (0)