diff --git a/solving/minimum-operations-to-sort-a-string/.url b/solving/minimum-operations-to-sort-a-string/.url new file mode 100644 index 0000000..d7ebd75 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/.url @@ -0,0 +1 @@ +https://leetcode.com/problems/minimum-operations-to-sort-a-string/ diff --git a/solving/minimum-operations-to-sort-a-string/driver.h b/solving/minimum-operations-to-sort-a-string/driver.h new file mode 100644 index 0000000..781b193 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/driver.h @@ -0,0 +1,13 @@ +// driver.h - generated by 'usaco new' from LeetCode's metadata (edit freely). +// Reads minimum-operations-to-sort-a-string.in in LeetCode's testcase format, cases back to back: +// one argument per line +// and writes one answer per case to minimum-operations-to-sort-a-string.out, formatted the way LeetCode +// shows its output. Parsing/printing helpers: leetcode.h. +int main() { + freopen("minimum-operations-to-sort-a-string.in", "r", stdin); + freopen("minimum-operations-to-sort-a-string.out", "w", stdout); + for (vector in; lc::read_case(in, 1);) { + auto s = lc::parse(in[0]); + cout << lc::show(Solution().minOperations(s)) << '\n'; + } +} diff --git a/solving/minimum-operations-to-sort-a-string/main.cpp b/solving/minimum-operations-to-sort-a-string/main.cpp new file mode 100644 index 0000000..c87f712 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/main.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +#ifdef LOCAL // set by ./usaco builds only, so this is safe to submit as-is +#include "debugging.h" +#include "leetcode.h" // ListNode, TreeNode +#endif + + +// ----- TYPES ----- +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// ----- FILE IO ----- +#define STDIN(filename) freopen(filename, "r", stdin) +#define STDOUT(filename) freopen(filename, "w", stdout) +#define FASTIO cin.tie(0)->sync_with_stdio(0) +#define STDIO(filename) \ + FASTIO; \ + string filenamee(filename); \ + STDIN((filenamee + ".in").c_str()); \ + STDOUT((filenamee + ".out").c_str()) +#define NL '\n' +#define CM ", " +#define SP " " + +// ----- FOR LOOPS ----- +#define F(i, L, R) for (int i = L; i < R; i++) +#define FE(i, L, R) for (int i = L; i <= R; i++) +#define FD(i, L, R) for (int i = L; i > R; i--) +#define FDE(i, L, R) for (int i = L; i >= R; i--) + +// ----- WEZ ----- +#define takeI(n) \ + int n; \ + cin >> n +#define takeII(n, m) \ + int n, m; \ + cin >> n >> m +#define takeIII(n, m, k) \ + int n, m, k; \ + cin >> n >> m >> k +#define takeL(n) \ + ll n; \ + cin >> n +#define takeS(s) \ + string s; \ + cin >> s +#define takeV(v, n) \ + vi v(n); \ + for (auto &x : v) \ + cin >> x +#define takeVL(v, n) \ + vector v(n); \ + for (auto &x : v) \ + cin >> x +#define whileZ \ + takeI(T); \ + while (T--) +// +// ----- MAX MIN ----- +#define REMAX(a, b) (a) = max((a), (b)) +#define REMIN(a, b) (a) = min((a), (b)) + +// ----- PAIRS ----- +#define fi first +#define se second + +// ----- OUTPUT ----- +#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL + +// ----- DEBUG ----- (compiled out on the judge) +#ifdef LOCAL +#define DBG(vari) cerr << #vari << " = " << (vari) << endl +#define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl +#define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ') +#define printV(a) printA(a, 0, SZ(a) - 1) +#else +#define DBG(vari) +#define DBG2(a, b) +#define printA(a, L, R) +#define printV(a) +#endif + +// ----- STL DS +#define ALL(c) (c).begin(), (c).end() +#define SZ(c) ((int)(c).size()) +#define SORT(c) sort(ALL(c)) +#define RSORT(c) sort(ALL(c), greater<>()) +#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) +#define pb push_back + +class Solution { +public: + int minOperations(string s) { + + } +}; + +#ifdef LOCAL // runs the examples; see driver.h +#include "driver.h" +#endif diff --git a/solving/minimum-operations-to-sort-a-string/minimum-operations-to-sort-a-string.in b/solving/minimum-operations-to-sort-a-string/minimum-operations-to-sort-a-string.in new file mode 100644 index 0000000..88b63a5 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/minimum-operations-to-sort-a-string.in @@ -0,0 +1,3 @@ +"dog" +"card" +"gf" diff --git a/solving/minimum-operations-to-sort-a-string/problem.md b/solving/minimum-operations-to-sort-a-string/problem.md new file mode 100644 index 0000000..2d8a4b5 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/problem.md @@ -0,0 +1,44 @@ +# 3863. Minimum Operations to Sort a String + +You are given a string `s` consisting of lowercase English letters. + +In one operation, you can select any **substring** of `s` that is **not** the entire string and **sort** it in **non-descending alphabetical** order. + +Return the **minimum** number of operations required to make `s` sorted in **non-descending** order. If it is not possible, return -1. + +**Example 1:** + +**Input:** s = "dog" + +**Output:** 1 + +**Explanation:**​​​​​​​ + +- Sort substring `"og"` to `"go"`. +- Now, `s = "dgo"`, which is sorted in ascending order. Thus, the answer is 1. + +**Example 2:** + +**Input:** s = "card" + +**Output:** 2 + +**Explanation:** + +- Sort substring `"car"` to `"acr"`, so `s = "acrd"`. +- Sort substring `"rd"` to `"dr"`, making `s = "acdr"`, which is sorted in ascending order. Thus, the answer is 2. + +**Example 3:** + +**Input:** s = "gf" + +**Output:** -1 + +**Explanation:** + +- It is impossible to sort `s` under the given constraints. Thus, the answer is -1. + +**Constraints:** + +- `1 <= s.length <= 10^5` +- `s` consists of only lowercase English letters. diff --git a/solving/minimum-operations-to-sort-a-string/tests/1.in b/solving/minimum-operations-to-sort-a-string/tests/1.in new file mode 100644 index 0000000..0cd19ab --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/1.in @@ -0,0 +1 @@ +"dog" diff --git a/solving/minimum-operations-to-sort-a-string/tests/1.out b/solving/minimum-operations-to-sort-a-string/tests/1.out new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/1.out @@ -0,0 +1 @@ +1 diff --git a/solving/minimum-operations-to-sort-a-string/tests/2.in b/solving/minimum-operations-to-sort-a-string/tests/2.in new file mode 100644 index 0000000..3cbf339 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/2.in @@ -0,0 +1 @@ +"card" diff --git a/solving/minimum-operations-to-sort-a-string/tests/2.out b/solving/minimum-operations-to-sort-a-string/tests/2.out new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/2.out @@ -0,0 +1 @@ +2 diff --git a/solving/minimum-operations-to-sort-a-string/tests/3.in b/solving/minimum-operations-to-sort-a-string/tests/3.in new file mode 100644 index 0000000..ae7a4eb --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/3.in @@ -0,0 +1 @@ +"gf" diff --git a/solving/minimum-operations-to-sort-a-string/tests/3.out b/solving/minimum-operations-to-sort-a-string/tests/3.out new file mode 100644 index 0000000..3a2e3f4 --- /dev/null +++ b/solving/minimum-operations-to-sort-a-string/tests/3.out @@ -0,0 +1 @@ +-1