-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_sum_closest.rs
More file actions
101 lines (75 loc) · 2.85 KB
/
three_sum_closest.rs
File metadata and controls
101 lines (75 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Given an integer array nums of length n and an integer target,
find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
*/
/*
Initial naive implementation:
Brute force approach to find the closest sum of the three integers
*/
pub fn three_sum_closest_naive(nums: Vec<i32>, target: i32) -> i32 {
// Instantiate the closest sum to a large value
let mut closest_sum = i32::MAX;
// Get the length of the input array
let n = nums.len();
// Iterate through all combinations of three numbers
for i in 0..n-2 {
for j in (i + 1)..n-1 {
for k in (j + 1)..n {
// Calculate the sum of the three numbers
let sum = nums[i] + nums[j] + nums[k];
// If the absolute difference between the sum and target
// is smaller than the current closest sum, update closest sum
if (sum - target).abs() < (closest_sum - target).abs() {
closest_sum = sum;
}
}
}
}
// Return the closest sum found
closest_sum
}
/*
Two pointer approach to find the closest sum of three integers:
Based on the solution to the 3Sum problem, we can adapt the two-pointer technique
to find the closest sum to the target.
*/
pub fn three_sum_closest_two_pointer(nums: Vec<i32>, target: i32) -> i32 {
// Sort the initial array
let mut nums = nums;
nums.sort();
// Initialize the closest sum to a large value
let mut closest_sum = i32::MAX;
// Loop over the nums array
for i in 0..nums.len() {
// Initialize two pointers
// (immediately after the current index)
let mut left: usize = i + 1;
// (end of the array)
let mut right: usize = nums.len() - 1;
// Transverse the array with the two pointers
while left < right {
// Calculate the sum of the three numbers
let sum: i32 = nums[i] + nums[left] + nums[right];
// If the sum is equal to the target, return it immediately
if sum == target {
return sum;
}
// If the absolute difference between the sum and target
// is smaller than the current closest sum, update closest sum
if (sum - target).abs() < (closest_sum - target).abs() {
closest_sum = sum;
}
// If the sum is less than the target, move the left pointer to the right
if sum < target {
left += 1;
// If the sum is greater than the target, move the right pointer to the left
} else {
right -= 1;
}
}
}
// Return the closest sum found
closest_sum
}