DEV Community

Mukilan Palanichamy
Mukilan Palanichamy

Posted on

My journey in competitive programming

1. Combination Sum:

This problem asks you to find all unique combinations of numbers from a list candidates that add up to a target sum. Each number can be used unlimited times.

Example:

Input:
candidates = [2, 3, 6, 7], target = 7
Output:
[[2, 2, 3], [7]]

How to Solve:
1.Use backtracking to get all possible combinations.
2.For each number, decide whether to include it in the current combination or skip it.
3.Stop moving further if the current sum more than target.
4.Use recursion to build the combination step by step.

Image description

2.Combination Sum II:

Example:

Input:
candidates = [10, 1, 2, 7, 6, 1, 5], target = 8
Output:
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]

How to Solve:

  1. Sort the candidates list to handle duplicates easily.
  2. Use backtracking similar to Combination Sum but: Skip duplicates; if the same number has already been considered for the current position.Move to the next index instead of reusing the same number.

Image description

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay