Created
December 10, 2024 09:05
-
-
Save day253/c36e643d70e8783cef4f94d7fa822c2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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