- Binary Search Problems
- Sliding Window Problems
- Stack Problems
- Heap Problems
- Linked List
- Recursion Problems
- Backtracking Problems
- Dynamic Porblems
- Tree Problems
- Graph Problems
- Math And Geometry Porblems
- Array and Strings
- Intervals Problems
- Substring -> set should be continuous
- Subsequence -> Should be in order not need to be continuous
- Subset -> all posible combinations
- When trying to apply a greedy approch always try to find a counter example
def number_of_digits(x):
count = 0
while (x > 0):
x = x // 10
count+=1
return countdef findMaxConsecutiveOnes(self, nums: List[int]) -> int:
gcount = 0
count = 0
for x in nums:
if x == 1:
count +=1
if count > gcount:
gcount = count
elif x == 0:
count = 0
else:
pass
return gcountmake n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
int sum=0;
sort(nums.begin(),nums.end());
for(int i=0; i<nums.size(); i++){
if(i%2=0){
sum=sum+nums[i];
}
}
return sum; vector<int> separateDigits(vector<int>& nums) {
vector<int> answer;
int n= nums.size();
for(int i=0; i<n; i++){
vector<int> temp;
while(nums[i]>0){
temp.push_back(nums[i]%10);
nums[i]=nums[i]/10;
}
reverse(temp.begin(),temp.end());
for(int x=0;x<temp.size();x++){
answer.push_back(temp[x]);
}
}
return answer;
} ListNode* temp = head;
while(temp!= NULL){
cout<<temp->val<<" ";
temp= temp->next;
}int array_to_int(vector<int> v, int rflag) {
int val = 0;
if (rflag) {
/* generating number in reverse*/
for (int i = v.size() - 1; i >= 0; i--) {
val = val *10 + v[i];
}
}else {
/* generating number */
for (int i = 0; i < v.size(); i++) {
val = val *10 + v[i];
}
}
return val;
}
int main() {
std::cout << "Hello World!\n";
int key = 1234;
int tmp = key;
int d = 0;
int rkey = 0;
while(tmp) {
/*number to digits*/
d = tmp %10;
tmp /= 10;
/*digits to number*/
rkey = rkey* 10 + d;
}
std::cout<< key <<" " << rkey<<std::endl;
std::vector<int> v = {1, 2, 3, 4};
std::cout<<"normal int : "<< array_to_int(v, 0) <<std::endl;
std::cout<<"reverse int : "<< array_to_int(v, 1) <<std::endl;
}string data = "hello world how are you";
vector<int> letter_count(26, 0);
for(int i = 0; i < data.size(); i++) {
if(data[i] == ' ') continue;
int index = data[i] - 97;
letter_count[index] = letter_count[index] + 1;
}
for(int i = 0; i < data.size(); i++) {
cout << letter_count[i] <<endl;
} unordered_map<char, int> hash_map;
for(int i = 0; i < data.size(); i++) {
if(data[i] == ' ') continue;
hash_map[data[i]] = hash_map[data[i]] + 1;
}
for (auto i: hash_map)
cout << i.first << ":" << i.second <<endl;struct
{
bool operator()(int a, int b) const { return a < b; }
}customless;
sort(a.begin(), a.end(), customless);bool search(int arr[][3], int target){
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
if(arr[i][j] == target)
return 1;
}
}
return 0;
}void sumrowwise(int arr[][3]){
for(int i=0; i<3; i++){
int sum=0;
for(int j=0; j<3; j++){
sum += arr[i][j];
}
cout<<sum<<" ";
}
cout<<endl;
}
void sumcolwise(int arr[][3]){
for(int i=0; i<3; i++){
int sum=0;
for(int j=0; j<3; j++){
sum += arr[j][i];
}
cout<<sum<<" ";
}
cout<<endl;
}int largestrowsum(int arr[][3]){
int max = INT_MIN;
int rowindex=-1;
for(int i=0; i<3; i++){
int sum=0;
for(int j=0; j<3; j++){
sum += arr[i][j];
}
if(sum > max){
max = sum;
rowindex = i;
}
}
cout<< "rowindex of row having maximum sum is" << rowindex<<endl;;
return max;
}int largestcolsum(int arr[][3]){
int max = INT_MIN;
int colindex=-1;
for(int i=0; i<3; i++){
int sum=0;
for(int j=0; j<3; j++){
sum += arr[j][i];
}
if(sum > max){
max = sum;
colindex = i;
}
}
cout<< "rowindex of col having maximum sum is" << colindex<<endl;;
return max;
}Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
You may return the answer in any order.
Note: Intervals are non-overlapping if they have no common point. For example, [1, 2] and [3, 4] are non-overlapping, but [1, 2] and [2, 3] are overlapping.
Example 1:
Input: intervals = [[1,3],[1,5],[6,7]]
Output: [[1,5],[6,7]]
Example 2:
Input: intervals = [[1,2],[2,3]]
Output: [[1,3]]Apporach:
- sort the intervals list
- push the first node to output list
- check if cur node is overlapping with previous node
- if yes change the last node to max of both
- else push the cur
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
vector<vector<int>> output;
output.push_back(intervals[0]);
for (auto& interval : intervals) {
int start = interval[0];
int end = interval[1];
int lastEnd = output.back()[1];
if (start <= lastEnd) {
output.back()[1] = max(lastEnd, end);
} else {
output.push_back({start, end});
}
}
return output;
}
};