Skip to content

Commit c4b1647

Browse files
committed
🚀 07-Oct-2020
1 parent 0b6e224 commit c4b1647

14 files changed

+977
-0
lines changed

competitive programming/leetcode/1009. Complement of Base 10 Integer.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,47 @@ class Solution {
4747
return N ^ tmp;
4848
}
4949
};
50+
51+
52+
53+
54+
// num + comlement = all onesa
55+
56+
class Solution {
57+
public:
58+
int bitwiseComplement(int n) {
59+
if(n==0) return 1;
60+
int digits=log2(n)+1;
61+
return pow(2, digits)-1-n;
62+
}
63+
};
64+
65+
66+
67+
68+
69+
class Solution {
70+
public:
71+
int bitwiseComplement(int n) {
72+
if(n==0) return 1;
73+
string binary;
74+
while(n){
75+
binary+=to_string(n%2);
76+
n/=2;
77+
}
78+
int len=binary.length();
79+
80+
for(int i=0;i<len;i++){
81+
if(binary[i]=='0') binary[i]='1';
82+
else binary[i]='0';
83+
}
84+
85+
int res=0;
86+
87+
for(int i=0;i<len;i++){
88+
res+=pow(2, i)*(binary[i]-'0');
89+
}
90+
91+
return res;
92+
}
93+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Let's define a function f(s) over a non-empty string s, which calculates the frequency of the smallest character in s. For example, if s = "dcce" then f(s) = 2 because the smallest character is "c" and its frequency is 2.
2+
3+
Now, given string arrays queries and words, return an integer array answer, where each answer[i] is the number of words such that f(queries[i]) < f(W), where W is a word in words.
4+
5+
6+
7+
Example 1:
8+
9+
Input: queries = ["cbd"], words = ["zaaaz"]
10+
Output: [1]
11+
Explanation: On the first query we have f("cbd") = 1, f("zaaaz") = 3 so f("cbd") < f("zaaaz").
12+
Example 2:
13+
14+
Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
15+
Output: [1,2]
16+
Explanation: On the first query only f("bbb") < f("aaaa"). On the second query both f("aaa") and f("aaaa") are both > f("cc").
17+
18+
19+
Constraints:
20+
21+
1 <= queries.length <= 2000
22+
1 <= words.length <= 2000
23+
1 <= queries[i].length, words[i].length <= 10
24+
queries[i][j], words[i][j] are English lowercase letters.
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
39+
vector<int> res;
40+
41+
vector<int> wordFreq;
42+
43+
for(auto word: words){
44+
vector<int> freq(26, 0);
45+
for(int i=0;i<word.length();i++){
46+
freq[word[i]-'a']++;
47+
}
48+
for(int i=0;i<26;i++){
49+
if(freq[i]>0){
50+
wordFreq.push_back(freq[i]);
51+
break;
52+
}
53+
}
54+
}
55+
56+
for(auto query: queries){
57+
vector<int> freq(26, 0);
58+
for(int i=0;i<query.length();i++){
59+
freq[query[i]-'a']++;
60+
}
61+
int tmp=0, mini=0;
62+
for(int i=0;i<26;i++){
63+
if(freq[i]>0){
64+
mini=freq[i];
65+
break;
66+
}
67+
}
68+
int cnt=0;
69+
70+
for(int i=0;i<wordFreq.size();i++){
71+
if(mini<wordFreq[i])
72+
cnt++;
73+
}
74+
75+
res.push_back(cnt);
76+
77+
}
78+
79+
return res;
80+
}
81+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
2+
3+
You can use each character in text at most once. Return the maximum number of instances that can be formed.
4+
5+
6+
7+
Example 1:
8+
9+
10+
11+
Input: text = "nlaebolko"
12+
Output: 1
13+
Example 2:
14+
15+
16+
17+
Input: text = "loonbalxballpoon"
18+
Output: 2
19+
Example 3:
20+
21+
Input: text = "leetcode"
22+
Output: 0
23+
24+
25+
Constraints:
26+
27+
1 <= text.length <= 10^4
28+
text consists of lower case English letters only.
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
int maxNumberOfBalloons(string text) {
40+
if(text.length()==0) return 0;
41+
42+
unordered_map<char, int> mp;
43+
44+
for(auto ch: text)
45+
mp[ch]++;
46+
47+
string tmp="balloon";
48+
int n=tmp.length();
49+
50+
int res=0;
51+
52+
while(1){
53+
for(int i=0;i<n;i++){
54+
if(mp[tmp[i]]>0){
55+
mp[tmp[i]]--;
56+
} else return res;
57+
}
58+
res++;
59+
}
60+
return res;
61+
}
62+
};
63+
64+
65+
66+
67+
68+
69+
class Solution {
70+
public:
71+
int maxNumberOfBalloons(string text) {
72+
if(text.length()==0) return 0;
73+
74+
unordered_map<char, int> mp;
75+
76+
for(auto ch: text)
77+
mp[ch]++;
78+
79+
int res=INT_MAX;
80+
81+
res=min(res,mp['b']);
82+
res=min(res,mp['a']);
83+
res=min(res,mp['l']/2);
84+
res=min(res,mp['o']/2);
85+
res=min(res,mp['n']);
86+
87+
return res;
88+
}
89+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Given a list of intervals, remove all intervals that are covered by another interval in the list.
2+
3+
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
4+
5+
After doing so, return the number of remaining intervals.
6+
7+
8+
9+
Example 1:
10+
11+
Input: intervals = [[1,4],[3,6],[2,8]]
12+
Output: 2
13+
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
14+
Example 2:
15+
16+
Input: intervals = [[1,4],[2,3]]
17+
Output: 1
18+
Example 3:
19+
20+
Input: intervals = [[0,10],[5,12]]
21+
Output: 2
22+
Example 4:
23+
24+
Input: intervals = [[3,10],[4,10],[5,11]]
25+
Output: 2
26+
Example 5:
27+
28+
Input: intervals = [[1,2],[1,4],[3,4]]
29+
Output: 1
30+
31+
32+
Constraints:
33+
34+
1 <= intervals.length <= 1000
35+
intervals[i].length == 2
36+
0 <= intervals[i][0] < intervals[i][1] <= 10^5
37+
All the intervals are unique.
38+
Show Hint #1
39+
Hide Hint #2
40+
Compare each interval to all others and check if it is covered by any interval.
41+
42+
43+
44+
45+
46+
47+
48+
class Solution {
49+
public:
50+
51+
static bool comp(vector<int> a, vector<int> b){
52+
if(a[0]==b[0])
53+
return a[1]>b[1];
54+
return a[0]<b[0];
55+
}
56+
57+
int removeCoveredIntervals(vector<vector<int>>& intervals) {
58+
sort(intervals.begin(), intervals.end(), comp);
59+
60+
int n=intervals.size();
61+
62+
int cnt=0;
63+
64+
int start=intervals[0][0], end=intervals[0][1];
65+
66+
for(int i=1;i<n;i++){
67+
if(intervals[i][0]>=start && intervals[i][1]<=end){
68+
cnt++;
69+
start=min(start, intervals[i][0]);
70+
end=max(end, intervals[i][1]);
71+
} else {
72+
start=intervals[i][0];
73+
end=intervals[i][1];
74+
}
75+
76+
}
77+
78+
return n-cnt;
79+
}
80+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.
2+
3+
Return the minimum number of steps to make the given string empty.
4+
5+
A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.
6+
7+
A string is called palindrome if is one that reads the same backward as well as forward.
8+
9+
10+
11+
Example 1:
12+
13+
Input: s = "ababa"
14+
Output: 1
15+
Explanation: String is already palindrome
16+
Example 2:
17+
18+
Input: s = "abb"
19+
Output: 2
20+
Explanation: "abb" -> "bb" -> "".
21+
Remove palindromic subsequence "a" then "bb".
22+
Example 3:
23+
24+
Input: s = "baabb"
25+
Output: 2
26+
Explanation: "baabb" -> "b" -> "".
27+
Remove palindromic subsequence "baab" then "b".
28+
Example 4:
29+
30+
Input: s = ""
31+
Output: 0
32+
33+
34+
Constraints:
35+
36+
0 <= s.length <= 1000
37+
s only consists of letters 'a' and 'b'
38+
39+
40+
41+
42+
43+
44+
45+
class Solution {
46+
public:
47+
48+
bool isPalindrome(string s){
49+
int start=0, end=s.length()-1;
50+
while(start<end){
51+
if(s[start]!=s[end])
52+
return false;
53+
start++;
54+
end--;
55+
}
56+
return true;
57+
}
58+
59+
int removePalindromeSub(string s) {
60+
if(s.length()==0) return 0;
61+
if(isPalindrome(s)) return 1;
62+
return 2;
63+
}
64+
};
65+
66+
/*
67+
If it's empty sting, return 0;
68+
If it's palindrome, return 1;
69+
Otherwise, we need at least 2 operation.
70+
71+
We can delete all characters 'a' in the 1st operation,
72+
and then all characters 'b' in the 2nd operation.
73+
So return 2 in this case
74+
*/

0 commit comments

Comments
 (0)