Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code solutions differ from video. #424

Closed
Ahmad-A0 opened this issue Jul 9, 2022 · 3 comments
Closed

Code solutions differ from video. #424

Ahmad-A0 opened this issue Jul 9, 2022 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@Ahmad-A0
Copy link
Collaborator

Ahmad-A0 commented Jul 9, 2022

It seems some solutions differ from the video, for 268-Missing-Number this code is shown in the video and appears as the python solution on neetcode.io:

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        res = len(nums)
        
        for i in range(len(nums)):
            res += (i - nums[i])
        return res

However the Java, C++ and proposed Typescript solution (#422) use slightly different algorithms:

Java:

class Solution {
    public int missingNumber(int[] nums) {
        int sum = 0;
        int total = nums.length * (nums.length+1)/2;
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        return total - sum;
    }
}

C++:

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size();
        int result = n;
        for (int i = 0; i < n; i++) {
            result ^= i ^ nums[i];
        }
        return result;
    }
};

Typescript:

function missingNumber(nums: number[]): number {
  let sum: number = 0
  let total: number = (nums.length * (nums.length + 1)) / 2
  for (let i = 0; i < nums.length; i++) {
    sum += nums[i]
  }
  return total - sum
}
@Ahmad-A0
Copy link
Collaborator Author

Ahmad-A0 commented Jul 9, 2022

I'm not sure how much it matters in this case, but it could definitely be confusing for some of the harder problems.

@siphc
Copy link
Contributor

siphc commented Jul 11, 2022

Java and Typescript solutions are algorithmically the same to Python's. They calculate the sum of range [0, n] before iteration, as opposed to during iteration (as with Python's case).

Additionally, https://neetcode.io/ groups 268-Missing-Number as "Bit Manipulation", and the video does cover the bitwise xor approach to the problem, which is what the C++ solution implemented.

@Ahmad-A0
Copy link
Collaborator Author

Sorry, I should have chosen a better example. I'll edit my initial comment when I find one, but I wanted to raise the issue of how code is translated between the python solution and other languages to avoid confusion and refactors later on.

briannej added a commit to briannej/leetcode that referenced this issue Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants