Optomized solutions for Day1

This commit is contained in:
2020-12-03 12:10:28 -05:00
parent ddddf63071
commit 73c4fb9984
2 changed files with 21 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <algorithm>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
@@ -32,10 +33,16 @@ int main(){
//Start the timer //Start the timer
timer.start(); timer.start();
std::sort(numbersList.begin(), numbersList.end());
//Find the two numbers in the array that make the correct sum //Find the two numbers in the array that make the correct sum
for(int cnt1 = 0;(cnt1 < numbersList.size()) && (!found);++cnt1){ for(int cnt1 = 0;(cnt1 < numbersList.size()) && (!found);++cnt1){
for(int cnt2 = cnt1 + 1;(cnt2 < numbersList.size()) && (!found);++cnt2){ for(int cnt2 = cnt1 + 1;(cnt2 < numbersList.size()) && (!found);++cnt2){
if((numbersList[cnt1] + numbersList[cnt2]) == NEEDED_SUM){ int sum = numbersList[cnt1] + numbersList[cnt2];
if(sum > NEEDED_SUM){
break;
}
else if(sum == NEEDED_SUM){
firstNum = numbersList[cnt1]; firstNum = numbersList[cnt1];
secondNum = numbersList[cnt2]; secondNum = numbersList[cnt2];
found = true; found = true;

View File

@@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <algorithm>
#include "Stopwatch.hpp" #include "Stopwatch.hpp"
@@ -33,11 +34,17 @@ int main(){
//Start the timer //Start the timer
timer.start(); timer.start();
std::sort(numbersList.begin(), numbersList.end());
//Find the two numbers in the array that make the correct sum //Find the two numbers in the array that make the correct sum
for(int cnt1 = 0;(cnt1 < numbersList.size()) && (!found);++cnt1){ for(int cnt1 = 0;(cnt1 < numbersList.size()) && (!found);++cnt1){
for(int cnt2 = cnt1 + 1;(cnt2 < numbersList.size()) && (!found);++cnt2){ for(int cnt2 = cnt1 + 1;(cnt2 < numbersList.size()) && (!found);++cnt2){
for(int cnt3 = cnt2 + 1;(cnt3 < numbersList.size()) && (!found);++cnt3){ for(int cnt3 = cnt2 + 1;(cnt3 < numbersList.size()) && (!found);++cnt3){
if((numbersList[cnt1] + numbersList[cnt2] + numbersList[cnt3]) == NEEDED_SUM){ int sum = numbersList[cnt1] + numbersList[cnt2] + numbersList[cnt3];
if(sum > NEEDED_SUM){
break;
}
else if(sum == NEEDED_SUM){
firstNum = numbersList[cnt1]; firstNum = numbersList[cnt1];
secondNum = numbersList[cnt2]; secondNum = numbersList[cnt2];
thirdNum = numbersList[cnt3]; thirdNum = numbersList[cnt3];