C
C#13mo ago
aa battery

❔ Just got started with leetcode after a year of learning, I need some help...

So the title says it all I guess, I started with leetcode after around a year with learning and I started away with one of its problems. It took me two hours to finally get it right but my code felt so redundant and long for such a simple problem. Can I get some feedback on how to make it more efficient? Thanks So here is what I had after I was done (it was the two sum problem)
public class Solution {
public int[] TwoSum(int[] nums, int target) {
for(int i = 0; i <= nums.Length; i++) {
int e = i < nums.Length ? i + 1 : i;
var excludedNums= new int[nums.Length-e];
Array.Copy(nums, e, excludedNums, 0, excludedNums.Length);

foreach(int a in excludedNums) {
int test = nums[i] + a;
if (test == target) {
if (a == nums[i]) {
return new int[] { i, Array.IndexOf(nums, a, i+1) };
}
return new int[] { i, Array.IndexOf(nums, a) };
};
}
}
return new int[2];
}
}
public class Solution {
public int[] TwoSum(int[] nums, int target) {
for(int i = 0; i <= nums.Length; i++) {
int e = i < nums.Length ? i + 1 : i;
var excludedNums= new int[nums.Length-e];
Array.Copy(nums, e, excludedNums, 0, excludedNums.Length);

foreach(int a in excludedNums) {
int test = nums[i] + a;
if (test == target) {
if (a == nums[i]) {
return new int[] { i, Array.IndexOf(nums, a, i+1) };
}
return new int[] { i, Array.IndexOf(nums, a) };
};
}
}
return new int[2];
}
}
7 Replies
JakenVeina
JakenVeina13mo ago
what's the TwoSum problem? yeah, for starters, there is 0 need to do any allocations, except for the final result so, we're given that there is only one solution for each input and the numbers can't be reused and the output doesn't have to be ordered so, we want to examine each possible un-ordered pair of numbers in the input, until we find a match
public int[] TwoSum(int[] nums, int target)
{
for(var i = 0; i < (nums.Length - 1); ++i)
for(var j = i + 1; j < nums.Length; ++i)
if ((nums[i] + nums[j]) == target)
return new[] { i, j };

throw new ArgumentException("No solution found");
}
public int[] TwoSum(int[] nums, int target)
{
for(var i = 0; i < (nums.Length - 1); ++i)
for(var j = i + 1; j < nums.Length; ++i)
if ((nums[i] + nums[j]) == target)
return new[] { i, j };

throw new ArgumentException("No solution found");
}
two nested loops gets us the ability to iterate a pair of numbers I.E. we're going to iterate all posibilities for picking the first number, and then all possibilities for picking the second we can also optimize the first loop to not actually look at all of the input numbers since, by the time we reach the last one, we will have checked all the pairs that include the last one, with the last one being picked as the second number so, the outer loop only has to iterate to (nums.Length - 1) the inner loop is similar, in the opposite direction we don't need to iterate picks for the second number that we've already iterated for picks as the first number so, we start at i + 1 in the inner loop
cap5lut
cap5lut13mo ago
wasnt there a constraint that i and j have to be different?
JakenVeina
JakenVeina13mo ago
right that's accomplished by starting the inner loop at i + 1 my mistake I had it as i initially
aa battery
aa batteryOP13mo ago
oh i see i think i over complicated it by using a foreach which made me use indexof
JakenVeina
JakenVeina13mo ago
very much so
aa battery
aa batteryOP13mo ago
and ig the array copy made it even more complicated idk
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server