Picking Numbers

Sort by

recency

|

207 Discussions

|

  • + 0 comments

    I'm seeing a lot of dictionaries in the comments, but because the constraints declare the numbers are within 1 - 100, we can use a fast counting array, here's my C# solution.

        const int lim = 100;
        int[] count = new int[lim];
        for(int i = 0; i < a.Count; ++i) ++count[a[i]];
        int max = 0;
        for(int i  =1; i < lim; ++ i)
        {
            max = Math.Max(count[i]+count[i-1],max);
        }
        return max;
    
  • + 0 comments

    Problem: [1 ,2, 2, 3, 1, 2] Incorrect: Subsets [1,2,2,3] and [1,2] My output: 4 Expected output: 5

    Correct: because 3 doesn't fullfil condition to be in the same set as number 1 sorted: [1,1,2, 2, 2,3] Subsets [1,1,2,2,2] and [3] My output: 5 Expected output: 5

  • + 0 comments

    I interpreted this several other ways than what the problem statement meant before checking here lol...

  • + 1 comment

    question asks "longest subarray" that should be continuous elements from the original array, so not allowed to change order. Thats what the example shows. Then on input: 1 2 2 3 1 2 expected answer: 5.

    Thats for sure wrong. Question worded incorrectly. After sorting the inputarray my sollution passes. So the question means SUBSET: any combination of elements regardless of order or position

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn picking_numbers(a: &[i32]) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(n)
        //You don't need to check for value-1 cases, because that is already checked by the value
        //itself + 1
        let mut a_dict = std::collections::HashMap::new();
    
        for &value in a {
            match a_dict.get(&value) {
                Some(f) => a_dict.insert(value, f + 1),
                None => a_dict.insert(value, 1),
            };
        }
    
        let mut longest_subsequence_len = 0;
        let mut current_subsequence_len = 0;
        for (value, frequency) in a_dict.iter() {
            let plus_len = *a_dict.get(&(*value + 1)).unwrap_or(&0);
    
            current_subsequence_len = frequency + plus_len;
            if current_subsequence_len > longest_subsequence_len {
                longest_subsequence_len = current_subsequence_len
            }
        }
    
        longest_subsequence_len
    }