Skip to content

Instantly share code, notes, and snippets.

@day253
Created December 10, 2024 09:05
Show Gist options
  • Select an option

  • Save day253/c36e643d70e8783cef4f94d7fa822c2f to your computer and use it in GitHub Desktop.

Select an option

Save day253/c36e643d70e8783cef4f94d7fa822c2f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void dfs(int target, vector<int>& groups, int current, int& result) {
if (current >= target || current == 0) {
return;
}
result = max(current, result);
for (auto group : groups) {
dfs(target, groups, current * 10 + group, result);
}
}
int findN(int target, vector<int>& groups) {
int result = 0;
for (auto group : groups) {
dfs(target, groups, group, result);
}
return result;
}
};
int main() {
vector<int> groups{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int target = 23145;
int result = 22999;
Solution s;
cout << s.findN(target, groups) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment