Untitled
unknown
plain_text
5 months ago
1.1 kB
3
Indexable
function func(s) { const map = new Map(); const arr = new Array(s.length).fill(0); for (let i = 0; i < s.length; i++) { if (s[i] === '$') { arr[i] = -1; // Get the smallest key from the map const firstKey = [...map.keys()].sort()[0]; const queue = map.get(firstKey); const index = queue.shift(); // Remove the first (smallest) index arr[index] = -1; if (queue.length === 0) { map.delete(firstKey); // Remove the key if the queue is empty } } else { let queue = map.get(s[i]); if (!queue) { queue = []; } // Insert the index into the queue in descending order queue.push(i); queue.sort((a, b) => b - a); // Sort in descending order map.set(s[i], queue); } } let result = ''; for (let i = 0; i < s.length; i++) { if (arr[i] !== -1) { result += s[i]; } } return result; }
Editor is loading...
Leave a Comment