File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 709
709
896|[ Monotonic Array] ( ./0896-monotonic-array.js ) |Easy|
710
710
897|[ Increasing Order Search Tree] ( ./0897-increasing-order-search-tree.js ) |Easy|
711
711
898|[ Bitwise ORs of Subarrays] ( ./0898-bitwise-ors-of-subarrays.js ) |Medium|
712
+ 899|[ Orderly Queue] ( ./0899-orderly-queue.js ) |Hard|
712
713
901|[ Online Stock Span] ( ./0901-online-stock-span.js ) |Medium|
713
714
905|[ Sort Array By Parity] ( ./0905-sort-array-by-parity.js ) |Easy|
714
715
909|[ Snakes and Ladders] ( ./0909-snakes-and-ladders.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments