Skip to content

Instantly share code, notes, and snippets.

View piusayowale's full-sized avatar

OGUNLEYE AYOWALE piusayowale

  • Lagos, Nigeria
View GitHub Profile
@piusayowale
piusayowale / pow.cpp
Created August 16, 2022 16:08
raised to power
double myPow(double x, int n) {
double ans=1;
long long tempn=n;
if(tempn <0)
tempn=-1*tempn;
while(tempn)
{
if(tempn%2 == 0)
{
@piusayowale
piusayowale / maxsumarray.cpp
Created August 16, 2022 13:49
maximum sub array sum (dynamic programming)
int maxSubsetSum(vector<int> arr) {
int N = arr.size();
int dp[N][2];
if (N == 1) {
return arr[0];
}
dp[0][0] = 0;
dp[0][1] = arr[0];
@piusayowale
piusayowale / add_numbers_leetcode.cpp
Created August 15, 2022 18:03
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
class Solution {
int cache = 0;
public:
ListNode * insert(ListNode *& list, int value){
ListNode * temp = nullptr;
ListNode * ptr = list;
if(ptr == nullptr){
temp = new ListNode(value);
@piusayowale
piusayowale / find_pairs.cpp
Created August 13, 2022 22:29
count pairs whose difference equal to a key
int pairs(int k, vector<int> arr) {
int count = 0;
multiset<int> s(arr.begin(), arr.end());
for(int number : arr)
{
count = count + s.count(number + k);
@piusayowale
piusayowale / iceCreampalour.cpp
Created August 11, 2022 20:41
hashtable solution for ice cream palour
void whatFlavors(vector<int> cost, int money) {
unordered_map<int, int> table;
vector<int> same_cache;
int index1 = 0;
int index2 = 0;
for(int i = 0; i < cost.size(); i++){
table[cost[i]] = i;
auto it = table.find(money - cost[i]);
pair<bool, tuple<int, int, int>> isTriangle(vector<int> sticks,int it){
/* triangle inequality theorem */
/* a + b > c; */
/* a + c > b */
/* b + c > a */
auto it_begin = sticks.begin() + it;
int a = *it_begin;
int b = *(it_begin + 1);
int c = *(it_begin + 2);
@piusayowale
piusayowale / maxmin.cpp
Created August 9, 2022 19:12
max - min problem
int maxMin(int k, vector<int> arr) {
sort(arr.begin(), arr.end());
int min = INT32_MAX;
for(int i = 0; i + k - 1 < arr.size(); i++){
int temp = arr[k + i - 1] - arr[i];
if(temp < min){
min = temp;
}
}
@piusayowale
piusayowale / greedyflourist.cpp
Created August 9, 2022 18:47
Greedy flourist
int getMinimumCost(int k, vector<int> c) {
if(k == c.size()){
return accumulate(c.begin(), c.end(), 0);
}
sort(c.begin(), c.end());
int cost = 0;
int purchased = 0;
int flowers = c.size() - 1;
long arrayManipulation(int n, vector<vector<int>> queries) {
if(n==0 || queries.empty()){
return -1;
}
vector<long> computation(n);
for (int i = 0; i < queries.size(); i++) {
int a = queries[i][0] - 1;
@piusayowale
piusayowale / grading.cpp
Created August 8, 2022 16:49
student grading
int round_num(int v){
if(v < 38){
return v;
}
if(v % 5 == 0){
return v;
}
int num = v;
int temp = num - (num % 5) + 5;
int rem = temp - num;