Skip to content

Completed s30 Backtracking-1 - #1147

Open
prenastro wants to merge 1 commit into
super30admin:masterfrom
prenastro:master
Open

Completed s30 Backtracking-1#1147
prenastro wants to merge 1 commit into
super30admin:masterfrom
prenastro:master

Conversation

@prenastro

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Combination Sum (combinationSum.py)

EACTLY MATCHING THE OUTPUT FORMAT. EXACTLY MATCHING THE SPACE AND NEWLINE PATTERNS. EXcombinationSum
Medium
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen number is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the backtracking solution to fit in the time limit.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the backtracking solution to not exceed the time limit.

The test should be generated such that the number of unique combinations that sum up to target is less than 150 combinations for the backing solution to fit in the time limit.

The test cases are generated such that the number of unique combinations that sum up to target is PASS or NEEDS_IMPROVEMENT

VERDICT: NEEDS_IMPROVEMENT


Expression Add Operators (expressionAddOperators.py)

VERDICT_REASON: [Brief reason for verdict]

Let me trace through the student's solution carefully.

The student's approach is interesting - they use a different parameterization. Let me trace through:

  • i: current index in num
  • curr: the current number being built (accumulator for consecutive digits)
  • calc: the running calculation
  • tail: the tail value (for multiplication precedence)
  • path: the expression string

Let me trace with num = "123", target = 6:

Initial call: helper(0, 0, 0, 0, "")

Step 1: i=0, curr = 0*10 + 1 = 1

  • curr > 0, so call helper(1, 1, 0, 0, "") (continue building curr)
    • i=1, curr = 1*10 + 2 = 12
    • curr > 0, so call helper(2, 12, 0, 0, "") (continue building curr)
      • i=2, curr = 12*10 + 3 = 123
      • curr > 0, so call helper(3, 123, 0, 0, "") (continue building curr)
        • i=3 == len(num), calc=0 != target=6, return
      • path is empty, so call helper(3, 0, 123, 123, "123")
        • i=3 == len(num), calc=123 != target=6, return
    • path is empty, so call helper(2, 0, 12, 12, "12")
      • i=2, curr = 12*10 + 3 = 123
      • curr > 0, so call helper(3, 123, 12, 12, "12")
        • i=3 == len(num), calc=12 != target=6, return
      • path is "12", not empty, so:
        • helper(3, 0, 12+123, 123, "12+123") - calc=135, not 6
        • helper(3, 0, 12-123, -123, "12-123") - calc=-111, not 6
        • helper(3, 0, 12-12+(12*123), 12*123, "12*123") - calc=1476, not 6
  • path is empty, so call helper(1, 0, 1, 1, "1")
    • i=1, curr = 0*10 + 2 = 2
    • curr > 0, so call helper(2, 2, 1, 1, "1") (continue building curr)
      • i=2, curr = 2*10 + 3 = 23
      • curr > 0, so call helper(3, 23, 1, 1, "1")
        • i=3 == len(num), calc=1 != target=6, return
      • path is "1", not empty, so:
        • helper(3, 0, 1+23, 23, "1+23") - calc=24, not 6
        • helper(3, 0, 1-23, -23, "1-23") - calc=-22, not 6
        • helper(3, 0, 1-1+(1*23), 1*23, "1*23") - calc=23, not 6
    • path is "1", not empty, so:
      • helper(2, 0, 1+2, 2, "1+2") - calc=3
        • i=2, curr = 0*10 + 3 = 3
        • curr > 0, so call helper(3, 3, 3, 2, "1+2")
          • i=3 == len(num), calc=3 != target=6, return
        • path is "1+2", not empty, so:
          • helper(3, 0, 3+3, 3, "1+2+3") - calc=6 == target! ✓
          • helper(3, 0, 3-3, -3, "1+2-3") - calc=0, not 6
          • helper(3, 0, 3-2+(2*3), 2*3, "1+2*3") - calc=7, not 6
      • helper(2, 0, 1-2, -2, "1-2") - calc=-1
        • i=2, curr = 0*10 + 3 = 3
        • curr > 0, so call helper(3, 3, -1, -2, "1-2")
          • i=3 == len(num), calc=-1 != target=6, return
        • path is "1-2", not empty, so:
          • helper(3, 0, -1+3, 3, "1-2+3") - calc=2, not 6
          • helper(3, 0, -1-3, -3, "1-2-3") - calc=-4, not 6
          • helper(3, 0, -1-(-2)+(-2*3), -2*3, "1-2*3") - calc=-5, not 6
      • helper(2, 0, 1-1+(1*2), 1*2, "1*2") - calc=2
        • i=2, curr = 0*10 + 3 = 3
        • curr > 0, so call helper(3, 3, 2, 2, "1*2")
          • i=3 == len(num), calc=2 != target=6, return
        • path is "1*2", not empty, so:
          • helper(3, 0, 2+3, 3, "1*2+3") - calc=5, not 6
          • helper(3, 0, 2-3, -3, "1*2-3") - calc=-1, not 6
          • helper(3, 0, 2-2+(2*3), 2*3, "1*2*3") - calc=6 == target! ✓

So we get ["1+2+3", "1*2*3"] which matches the expected output.

Wait, let me check the order. The expected output is ["1*2*3","1+2+3"]. The student's solution would produce them in the order I traced: first "1+2+3" then "123". The order doesn't matter for correctness.

Now let me check the leading zero handling. The condition if curr > 0 is used to decide whether to continue building the current number. But this is wrong for handling leading zeros!

Wait, let me think again. If num = "105", then:

  • i=0, curr = 1
  • curr > 0, so call helper(1, 1, 0, 0, "") (continue building curr)
    • i=1, curr = 1*10 + 0 = 10
    • curr > 0, so call helper(2, 10, 0, 0, "") (continue building curr)
      • i=2, curr = 10*10 + 5 = 105
      • curr > 0, so call helper(3, 105, 0, 0, "")
        • i=3 == len(num), calc=0 != target, return
      • path is empty, so call helper(3, 0, 105, 105, "105")
        • i=3 == len(num), calc=105 != target, return
    • path is empty, so call helper(2, 0, 10, 10, "10")
      • i=2, curr = 0*10 + 5 = 5
      • curr > 0, so call helper(3, 5, 10, 10, "10")
        • i=3 == len(num), calc=10 != target, return
      • path is "10", not empty, so:
        • helper(3, 0, 10+5, 5, "10+5") - calc=15

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants