leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 231c53e94f70be860ec574029d6b2b8c60230477
parent 45edb85c1501f4dcd49c729218729b542f1f263f
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 17 Feb 2026 14:02:53 -0600

Some leetcode problems

Diffstat:
Aswap-nodes-in-pairs/swap.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Atwo-best-non-overlapping-events/non-overlapping-v2.cpp | 22++++++++++++++++++++++
Atwo-best-non-overlapping-events/non-overlapping.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/swap-nodes-in-pairs/swap.cpp b/swap-nodes-in-pairs/swap.cpp @@ -0,0 +1,49 @@ +#include <iostream> +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +// two pointers +// first one stays on the prior (second pair) +// second one goes two ahead to swap +// swap +// increment + +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + + ListNode* original = head; + ListNode* prior = nullptr; + + bool first = true; + + while (head != nullptr && head->next != nullptr){ + ListNode* newFirst = swap(prior, head, head->next, head->next->next); + head = newFirst->next->next; + if (first == true){ + first = false; + original = newFirst; + } + prior = newFirst->next; + } + + return original; + } + ListNode* swap(ListNode* prior, ListNode* first, ListNode* second, ListNode* third){ + + second->next = first; + first->next = third; + if(prior != nullptr){ + prior->next = second; + } + return second; + } +}; diff --git a/two-best-non-overlapping-events/non-overlapping-v2.cpp b/two-best-non-overlapping-events/non-overlapping-v2.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int maxTwoEvents(vector<vector<int>>& events) { + int best = 0; + for(int i = 0; i < events.size(); ++i){ + vector<int> first = events[i]; + if(first[2] > best){ + best = first[2]; + } + + for(int x = 0; x < events.size() ; ++x){ + vector<int> second = events[x]; + if(second[0] > first[1]){ + if(second[2] + first[2] > best){ + best = second[2] + first[2]; + } + } + } + } + return best; + } +}; diff --git a/two-best-non-overlapping-events/non-overlapping.cpp b/two-best-non-overlapping-events/non-overlapping.cpp @@ -0,0 +1,49 @@ +#include <unordered_map> +#include <iostream> +#include <vector> +#include <algorithm> + +class Solution { +public: + int maxTwoEvents(vector<vector<int>>& events){ + + std::sort(events.begin(), events.end()); + vector<int> best; + + int max = 0; + for(int i = 0 ; i < events.size(); ++i){ + if(events[i][1] > max){ + max = events[i][1]; + } + } + + // iterate to max and pushback + for(int i = 0 ; i < max; ++i){ + best.push_back(0); + } + best.push_back(0); + best.push_back(0); + + int retScore = 0; + + for (int i = 0 ; i < events.size(); ++i){ + vector<int> currentEvent = events[i]; + int lowerBound = currentEvent[0]; + int currentVal = currentEvent[2]; + int bestAtLocation = best[lowerBound]; + + if (bestAtLocation + currentVal > retScore){ + retScore = bestAtLocation + currentVal; + } + if(currentVal > best[currentEvent[1] + 1]){ + for(int x = currentEvent[1] + 1; x < best.size(); ++x){ + if(currentVal > best[x]){ + best[x] = currentVal; + } + } + } + } + + return retScore; + } +};