Optomized solutions for Day1
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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];
|
||||||
|
|||||||
Reference in New Issue
Block a user