Skip to content

Instantly share code, notes, and snippets.

View vineethm1627's full-sized avatar
🎯
Focusing

M Vineeth vineethm1627

🎯
Focusing
View GitHub Profile
@vineethm1627
vineethm1627 / max_area_islands_graph.cpp
Created July 2, 2021 17:03
Graph DFS Solution in C++
class Solution {
// denotes the 8 neighbors of a grid cell
int row_dirs[8] = {0, 0, -1, 1, -1, -1, 1, 1};
int col_dirs[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
int rows, cols;
public:
void dfs_helper(int r, int c, vector<vector<int>> &grid, int &cur_area) {
// base case: corner cases of the matrix
// when you encounter 0: there won't be adjacent neighbor calls
if(r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == 0)
@vineethm1627
vineethm1627 / longest_increasing_subseq.cpp
Created July 2, 2021 12:58
Efficient O(nlogn) solution using Binary Search and DP
//Function to find length of longest increasing subsequence.
// O(nlogn) solution using Binary Search + DP
int longestSubsequence(int n, int arr[]) {
// your code here
/*
Notation:
1) ith index means length of increasing subseq = i
2) ith index will contain the smallest ending element of the LIS of length i
*/
vector<int> dp(n + 1, INT_MAX);
@vineethm1627
vineethm1627 / max_non_adjacent_sum.cpp
Created July 2, 2021 11:52
O(1) Space efficient solution
//Function to return the maximum sum without adding adjacent elements.
// include/ exclude DP
long long maximumSum(int arr[], int sizeOfArray) {
//Your code here
// edge case: if all the elements are negative
bool flag = true;
int max_val = INT_MIN;
for(int i = 0; i < sizeOfArray; i++) {
if(arr[i] > 0) {
flag = false;
@vineethm1627
vineethm1627 / renaming_cities.cpp
Created July 2, 2021 08:11
Trie Efficient Solution
#include <bits/stdc++.h>
using namespace std;
struct Node{
bool isEndOfWord;
int count1;
map<char, Node *> mp;
};
Node *newNode(){
#include <stdio.h>
int main()
{
char info[100];
char dept[ ] = "HR";
int emp = 75;
sprintf(info, "The %s dept has %d employees.", dept, emp);
printf("%s\n", info);
return 0;
#include <stdio.h>
int main()
{
char info[ ] = "Snoqualmie WA 13190";
char city[50];
char state[50];
int population;
sscanf(info, "%s %s %d", city, state, &population);
printf("%d people live in %s, %s.", population, city, state);
#include <iostream>
using namespace std;
class SocialWebsite {
private:
protected:
public:
virtual void secret() = 0;
};
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile;
// open the file
MyFile.open("test.txt");
MyFile << "Some text. \n";
#include <iostream>
using namespace std;
int main() {
try {
double num1;
cout <<"Enter the first number:";
cin >> num1;
double num2;
/********************************************/
/************* HOW TO USE TYPES *************/
/********************************************/
#include <iostream>
using namespace std;
/* This can be used indifferently for any type */
template<typename T>
void print_data(T output){
cout << output << endl;