Skip to content

Commit f95688d

Browse files
committed
Add solution #899
1 parent 7ca6ae2 commit f95688d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@
709709
896|[Monotonic Array](./0896-monotonic-array.js)|Easy|
710710
897|[Increasing Order Search Tree](./0897-increasing-order-search-tree.js)|Easy|
711711
898|[Bitwise ORs of Subarrays](./0898-bitwise-ors-of-subarrays.js)|Medium|
712+
899|[Orderly Queue](./0899-orderly-queue.js)|Hard|
712713
901|[Online Stock Span](./0901-online-stock-span.js)|Medium|
713714
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
714715
909|[Snakes and Ladders](./0909-snakes-and-ladders.js)|Medium|

solutions/0899-orderly-queue.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 899. Orderly Queue
3+
* https://leetcode.com/problems/orderly-queue/
4+
* Difficulty: Hard
5+
*
6+
* You are given a string s and an integer k. You can choose one of the first k letters of s
7+
* and append it at the end of the string.
8+
*
9+
* Return the lexicographically smallest string you could have after applying the mentioned
10+
* step any number of moves.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @param {number} k
16+
* @return {string}
17+
*/
18+
var orderlyQueue = function(s, k) {
19+
if (k === 1) {
20+
let smallest = s;
21+
for (let i = 0; i < s.length; i++) {
22+
const rotated = s.slice(i) + s.slice(0, i);
23+
if (rotated < smallest) smallest = rotated;
24+
}
25+
return smallest;
26+
}
27+
return [...s].sort().join('');
28+
};

0 commit comments

Comments
 (0)