Skip to content

Commit c192444

Browse files
committed
🚀 25-Sep-2020
1 parent da64226 commit c192444

19 files changed

+1260
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
2+
3+
You may return the answer in any order.
4+
5+
6+
7+
Example 1:
8+
9+
Input: ["bella","label","roller"]
10+
Output: ["e","l","l"]
11+
Example 2:
12+
13+
Input: ["cool","lock","cook"]
14+
Output: ["c","o"]
15+
16+
17+
Note:
18+
19+
1 <= A.length <= 100
20+
1 <= A[i].length <= 100
21+
A[i][j] is a lowercase letter
22+
23+
24+
25+
26+
27+
28+
class Solution {
29+
public:
30+
vector<string> commonChars(vector<string>& A) {
31+
vector<string> res;
32+
33+
vector<int> freq(26, 0);
34+
35+
string first=A[0];
36+
37+
for(int i=0;i<first.length();i++)
38+
freq[first[i]-'a']++;
39+
40+
for(int i=1;i<A.size();i++){
41+
vector<int> ftmp(26, 0);
42+
string tmp=A[i];
43+
44+
for(int i=0;i<tmp.length();i++)
45+
ftmp[tmp[i]-'a']++;
46+
47+
for(int i=0;i<26;i++)
48+
freq[i]=min(freq[i], ftmp[i]);
49+
}
50+
51+
for(int i=0;i<26;i++){
52+
while(freq[i]>0){
53+
res.push_back(string(1, i+'a'));
54+
freq[i]--;
55+
}
56+
}
57+
58+
return res;
59+
}
60+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Students are asked to stand in non-decreasing order of heights for an annual photo.
2+
3+
Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.
4+
5+
Notice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students remain on their seats.
6+
7+
8+
9+
Example 1:
10+
11+
Input: heights = [1,1,4,2,1,3]
12+
Output: 3
13+
Explanation:
14+
Current array : [1,1,4,2,1,3]
15+
Target array : [1,1,1,2,3,4]
16+
On index 2 (0-based) we have 4 vs 1 so we have to move this student.
17+
On index 4 (0-based) we have 1 vs 3 so we have to move this student.
18+
On index 5 (0-based) we have 3 vs 4 so we have to move this student.
19+
Example 2:
20+
21+
Input: heights = [5,1,2,3,4]
22+
Output: 5
23+
Example 3:
24+
25+
Input: heights = [1,2,3,4,5]
26+
Output: 0
27+
28+
29+
Constraints:
30+
31+
1 <= heights.length <= 100
32+
1 <= heights[i] <= 100
33+
34+
35+
36+
37+
38+
39+
40+
class Solution {
41+
public:
42+
int heightChecker(vector<int>& heights) {
43+
int n=heights.size();
44+
vector<int> target(n);
45+
46+
for(int i=0;i<n;i++)
47+
target[i]=heights[i];
48+
49+
sort(target.begin(), target.end());
50+
51+
int cnt=0;
52+
53+
for(int i=0;i<n;i++){
54+
if(heights[i]!=target[i])
55+
cnt++;
56+
}
57+
return cnt;
58+
}
59+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
2+
3+
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
4+
5+
6+
7+
Example 1:
8+
9+
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
10+
Output: [2,2,2,1,4,3,3,9,6,7,19]
11+
12+
13+
Constraints:
14+
15+
arr1.length, arr2.length <= 1000
16+
0 <= arr1[i], arr2[i] <= 1000
17+
Each arr2[i] is distinct.
18+
Each arr2[i] is in arr1.
19+
20+
21+
22+
23+
24+
25+
26+
class Solution {
27+
public:
28+
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
29+
vector<int> res;
30+
map<int, int> m;
31+
32+
for(auto x: arr1)
33+
m[x]++;
34+
35+
for(auto x: arr2){
36+
while(m[x]>0){
37+
res.push_back(x);
38+
m[x]--;
39+
}
40+
}
41+
42+
for(auto [a, b]: m){
43+
while(b>0){
44+
res.push_back(a);
45+
b--;
46+
}
47+
}
48+
49+
return res;
50+
}
51+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
You are given an array of strings words and a string chars.
2+
3+
A string is good if it can be formed by characters from chars (each character can only be used once).
4+
5+
Return the sum of lengths of all good strings in words.
6+
7+
8+
9+
Example 1:
10+
11+
Input: words = ["cat","bt","hat","tree"], chars = "atach"
12+
Output: 6
13+
Explanation:
14+
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
15+
Example 2:
16+
17+
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
18+
Output: 10
19+
Explanation:
20+
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
21+
22+
23+
Note:
24+
25+
1 <= words.length <= 1000
26+
1 <= words[i].length, chars.length <= 100
27+
All strings contain lowercase English letters only.
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
int countCharacters(vector<string>& words, string chars) {
39+
vector<int> freq(26, 0);
40+
41+
int res=0;
42+
43+
for(auto x: chars) freq[x-'a']++;
44+
45+
for(auto word: words){
46+
vector<int> tmp=freq;
47+
bool flag=true;
48+
for(auto ch: word){
49+
if(tmp[ch-'a']<=0){ flag=false; break;}
50+
tmp[ch-'a']--;
51+
}
52+
if(flag) res+=word.length();
53+
}
54+
55+
return res;
56+
}
57+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
2+
3+
After doing so, return the array.
4+
5+
6+
7+
Example 1:
8+
9+
Input: arr = [17,18,5,4,6,1]
10+
Output: [18,6,6,6,1,-1]
11+
12+
13+
Constraints:
14+
15+
1 <= arr.length <= 10^4
16+
1 <= arr[i] <= 10^5
17+
18+
19+
20+
21+
22+
23+
24+
25+
class Solution {
26+
public:
27+
vector<int> replaceElements(vector<int>& arr) {
28+
int n=arr.size();
29+
30+
vector<int> res(n);
31+
32+
int maxi=arr[n-1];
33+
34+
res[n-1]=-1;
35+
36+
for(int i=n-2;i>=0;i--){
37+
res[i]=maxi;
38+
if(arr[i]>maxi) maxi=arr[i];
39+
}
40+
41+
return res;
42+
}
43+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Given an integer n, return any array containing n unique integers such that they add up to 0.
2+
3+
4+
5+
Example 1:
6+
7+
Input: n = 5
8+
Output: [-7,-1,1,3,4]
9+
Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
10+
Example 2:
11+
12+
Input: n = 3
13+
Output: [-1,0,1]
14+
Example 3:
15+
16+
Input: n = 1
17+
Output: [0]
18+
19+
20+
Constraints:
21+
22+
1 <= n <= 1000
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
vector<int> sumZero(int n) {
34+
vector<int> res;
35+
int i=1;
36+
37+
if(n%2!=0){
38+
res.push_back(0);
39+
n--;
40+
while(n){
41+
res.push_back(i);
42+
res.push_back(-i);
43+
i++;
44+
n-=2;
45+
}
46+
} else {
47+
while(n){
48+
res.push_back(i);
49+
res.push_back(-i);
50+
i++;
51+
n-=2;
52+
}
53+
}
54+
55+
return res;
56+
}
57+
};

0 commit comments

Comments
 (0)