Skip to content

Commit f6e10ae

Browse files
committed
practise 22-May-2020
1 parent fcba2a8 commit f6e10ae

4 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Given a valid (IPv4) IP address, return a defanged version of that IP address.
2+
3+
A defanged IP address replaces every period "." with "[.]".
4+
5+
6+
7+
Example 1:
8+
9+
Input: address = "1.1.1.1"
10+
Output: "1[.]1[.]1[.]1"
11+
Example 2:
12+
13+
Input: address = "255.100.50.0"
14+
Output: "255[.]100[.]50[.]0"
15+
16+
17+
Constraints:
18+
19+
The given address is a valid IPv4 address.
20+
21+
22+
class Solution {
23+
public:
24+
string defangIPaddr(string address) {
25+
string res;
26+
string tmp="[.]";
27+
int n=address.length();
28+
for(int i=0;i<n;i++){
29+
if(address[i]=='.'){
30+
res+=tmp;
31+
} else res+=address[i];
32+
}
33+
return res;
34+
}
35+
};
36+
37+
38+
39+
class Solution {
40+
public:
41+
string defangIPaddr(string address) {
42+
int n=address.length();
43+
int cnt=0;
44+
for(int i=0;i<n;i++){
45+
if(address[i]=='.') cnt++;
46+
}
47+
int last=n+(cnt*2);
48+
while(cnt--){
49+
address+=" ";
50+
}
51+
for(int i=n-1,j=last-1; i>=0;i--){
52+
if(address[i]=='.'){
53+
address[j]=']';
54+
address[j-1]='.';
55+
address[j-2]='[';
56+
j-=3;
57+
} else {
58+
address[j]=address[i];
59+
j-=1;
60+
}
61+
}
62+
return address;
63+
}
64+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
We are given a list nums of integers representing a list compressed with run-length encoding.
2+
3+
Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
4+
5+
Return the decompressed list.
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [1,2,3,4]
12+
Output: [2,4,4,4]
13+
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
14+
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
15+
At the end the concatenation [2] + [4,4,4] is [2,4,4,4].
16+
Example 2:
17+
18+
Input: nums = [1,1,2,3]
19+
Output: [1,3,3]
20+
21+
22+
Constraints:
23+
24+
2 <= nums.length <= 100
25+
nums.length % 2 == 0
26+
1 <= nums[i] <= 100
27+
28+
29+
30+
class Solution {
31+
public:
32+
vector<int> decompressRLElist(vector<int>& nums) {
33+
int n=nums.size();
34+
vector<int> v;
35+
for(int i=0;i<n;i+=2){
36+
int freq=nums[i];
37+
int val=nums[i+1];
38+
while(freq--){
39+
v.push_back(val);
40+
}
41+
}
42+
return v;
43+
}
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.
2+
3+
For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
4+
5+
6+
7+
Example 1:
8+
9+
Input: candies = [2,3,5,1,3], extraCandies = 3
10+
Output: [true,true,true,false,true]
11+
Explanation:
12+
Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
13+
Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
14+
Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
15+
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
16+
Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
17+
Example 2:
18+
19+
Input: candies = [4,2,1,1,2], extraCandies = 1
20+
Output: [true,false,false,false,false]
21+
Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
22+
Example 3:
23+
24+
Input: candies = [12,1,12], extraCandies = 10
25+
Output: [true,false,true]
26+
27+
28+
Constraints:
29+
30+
2 <= candies.length <= 100
31+
1 <= candies[i] <= 100
32+
1 <= extraCandies <= 50
33+
34+
35+
class Solution {
36+
public:
37+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
38+
int maxi=INT_MIN;
39+
vector<bool> res;
40+
for(auto x: candies){
41+
if(x>maxi) maxi=x;
42+
}
43+
for(auto x: candies){
44+
if(x+extraCandies >= maxi){
45+
res.push_back(true);
46+
} else res.push_back(false);
47+
}
48+
return res;
49+
}
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Given a binary tree, determine if it is a valid binary search tree (BST).
2+
3+
Assume a BST is defined as follows:
4+
5+
The left subtree of a node contains only nodes with keys less than the node's key.
6+
The right subtree of a node contains only nodes with keys greater than the node's key.
7+
Both the left and right subtrees must also be binary search trees.
8+
9+
10+
Example 1:
11+
12+
2
13+
/ \
14+
1 3
15+
16+
Input: [2,1,3]
17+
Output: true
18+
Example 2:
19+
20+
5
21+
/ \
22+
1 4
23+
/ \
24+
3 6
25+
26+
Input: [5,1,4,null,null,3,6]
27+
Output: false
28+
Explanation: The root node's value is 5 but its right child's value is 4.
29+
30+
31+
32+
/**
33+
* Definition for a binary tree node.
34+
* struct TreeNode {
35+
* int val;
36+
* TreeNode *left;
37+
* TreeNode *right;
38+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
39+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
40+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
41+
* };
42+
*/
43+
44+
bool tmpIsValidBST(TreeNode *root, long long mini, long long maxi){
45+
if(!root) return true;
46+
if(root->val<=mini || root->val>=maxi) return false;
47+
return tmpIsValidBST(root->left, mini, root->val) && tmpIsValidBST(root->right, root->val, maxi);
48+
49+
}
50+
51+
class Solution {
52+
public:
53+
bool isValidBST(TreeNode* root) {
54+
return tmpIsValidBST(root, (long long)INT_MIN-1, (long long)INT_MAX+1);
55+
}
56+
};

0 commit comments

Comments
 (0)