commit 588fb8fa1a06bda90c374f5077b00fa56159d67c parent 631b49f2e2031626a19f7c9e74f1294ed84cb4bb Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Mon, 30 Sep 2024 09:34:46 +0200 1 Random Problem Diffstat:
A | Problems/0670.cpp | | | 20 | ++++++++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/Problems/0670.cpp b/Problems/0670.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int maximumSwap(int numi) const { + string num = to_string(numi); + const int n = size(num); + int last[10] = {0}; + + for (int i = 0; i < n; i++) + last[num[i] - '0'] = i; + for (int i = 0; i < n; i++) { + for (int d = 9; d > num[i] - '0'; d--) { + if (last[d] <= i) continue; + swap(num[i], num[last[d]]); + return stoi(num); + } + } + + return numi; + } +}; diff --git a/README.md b/README.md @@ -461,6 +461,7 @@ for solving problems. | 0664 | Hard | [Strange Printer](Problems/0664.cpp) | | 0667 | Medium | [Beautiful Arrangement II](Problems/0667.cpp) | | 0669 | Medium | [Trim a Binary Search Tree](Problems/0669.cpp) | +| 0670 | Medium | [Maximum Swap](Problems/0670.cpp) | | 0671 | Easy | [Second Minimum Node In a Binary Tree](Problems/0671.cpp) | | 0672 | Medium | [Bulb Switcher II](Problems/0672.cpp) | | 0673 | Medium | [Number of Longest Increasing Subsequence](Problems/0673.cpp) |