From e1019ff4ebcf454d54ea693e255f6c98820a80ce Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 21 Sep 2024 21:49:29 +0530 Subject: [PATCH] Create 386. Lexicographical Numbers --- 386. Lexicographical Numbers | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 386. Lexicographical Numbers diff --git a/386. Lexicographical Numbers b/386. Lexicographical Numbers new file mode 100644 index 0000000..1166383 --- /dev/null +++ b/386. Lexicographical Numbers @@ -0,0 +1,21 @@ +class Solution { +public: + vector lexicalOrder(int n) { + priority_queue, greater> heap; // Min-heap for lexicographical order + + // Push all numbers as strings into the priority queue + for (int i = 1; i <= n; i++) { + heap.push(to_string(i)); + } + + vector ans; + + // Pop elements from the heap, convert them back to integers, and store them in the answer + while (!heap.empty()) { + ans.push_back(stoi(heap.top())); // Convert string back to integer + heap.pop(); + } + + return ans; + } +};