Struggled to complete 3 sum closest
This commit is contained in:
parent
8335d9c95f
commit
82a3a5fbd1
@ -127,11 +127,13 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\leet.cpp" />
|
||||
<ClCompile Include="src\3sum.cpp" />
|
||||
<ClCompile Include="src\3sumclosest.cpp" />
|
||||
<ClCompile Include="src\main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\leet.h" />
|
||||
<ClInclude Include="src\3sum.h" />
|
||||
<ClInclude Include="src\3sumclosest.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@ -18,12 +18,18 @@
|
||||
<ClCompile Include="src\main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\leet.cpp">
|
||||
<ClCompile Include="src\3sum.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\3sumclosest.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\leet.h">
|
||||
<ClInclude Include="src\3sum.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\3sumclosest.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "leet.h"
|
||||
#include "3sum.h"
|
||||
|
||||
std::vector<std::vector<int>> Solution::threeSum(std::vector<int>& nums) {
|
||||
std::vector<std::vector<int>> ans;
|
40
src/3sumclosest.cpp
Normal file
40
src/3sumclosest.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "3sumclosest.h"
|
||||
|
||||
int Solution::threeSumClosest(std::vector<int>& nums, int target) {
|
||||
std::unordered_map<int, int> map;
|
||||
|
||||
int ans = INT_MAX;
|
||||
|
||||
std::sort(nums.begin(), nums.end());
|
||||
|
||||
for (int i = 0; i < nums.size(); i++)
|
||||
map[nums[i]] = i;
|
||||
|
||||
for (int i = 0; i < nums.size(); i++)
|
||||
{
|
||||
for (int j = i + 1; j < nums.size(); j++)
|
||||
{
|
||||
int required = (target - (nums[i] + nums[j])) * -1;
|
||||
|
||||
if (map.count(required))
|
||||
return nums[i] + nums[j] + required;
|
||||
|
||||
//Find closest alternative
|
||||
int index = j+1;
|
||||
int current = nums[j+1];
|
||||
while (gettingCloser(required, nums[++index], current)) {
|
||||
current = nums[index];
|
||||
}
|
||||
|
||||
//See if closest alternative is closer
|
||||
if (std::abs(required - current) < std::abs(target - ans))
|
||||
ans = current + nums[i] + nums[j];
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
bool Solution::gettingCloser(int target, int newVal, int current) {
|
||||
return std::abs(target - newVal) < std::abs(target - current);
|
||||
}
|
10
src/3sumclosest.h
Normal file
10
src/3sumclosest.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int threeSumClosest(std::vector<int>& nums, int target);
|
||||
bool gettingCloser(int target, int newVal, int current);
|
||||
};
|
13
src/main.cpp
13
src/main.cpp
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user