This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} A | |
* @return {number[]} | |
*/ | |
// O(n**2) time. O(1) space. | |
// beats 95%! | |
var pancakeSort = function(A) { | |
const wc = []; | |
let end = A.length-1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The rand7() API is already defined for you. | |
* var rand7 = function() {} | |
* @return {number} a random integer in the range 1 to 7 | |
*/ | |
/* | |
IDEA: Make two calls to rand7 to get a value from this table: | |
(each value is the sum of the two calls to rand7()) | |
1. 2 3. 4. 5. 6. 7. | |
1. 2 3 4 5 6 7 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
IDEA: If we have a sorted list of starts and ends, | |
then scanning from left to right, every time we | |
see a start, that must be the closest interval to | |
the right of all the ends we've seen so far. | |
O(nlogn) // nlogn for sorting, + n for a single scan. | |
*/ | |
var findRightInterval = function(intervals) { | |
// 1. Break intervals into start and end events |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} A | |
* @return {number[]} | |
*/ | |
var sortArrayByParity = function(A) { | |
let [l,r] = [0,A.length-1] | |
while(l<r){ | |
if(A[l] %2){ | |
[A[l],A[r]] = [A[r],A[l]] | |
r-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// O(n) time, O(1) space | |
var reorderList = function(head) { | |
if(!head || !head.next) return; | |
// split in half | |
let [slow, fast, prev] = [head, head, null]; | |
while(fast && fast.next){ | |
prev = slow; | |
slow = slow.next; | |
fast = fast.next.next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} S | |
* @return {string} | |
*/ | |
const v = 'aeiouAEIOU'; | |
var toGoatLatin = function(S) { | |
return S.split(' ') | |
.map((w, i) => (v.indexOf(w[0]) >= 0 ? w : (w.slice(1) + w[0])) + 'ma' + 'a'.repeat(i+1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
IDEA: We can build up a valid solution from left to right. | |
- Start with every legal number (1-9) | |
- For each starting point | |
- append N+K, and keep building if legal | |
- append N-K, and keep building if legal | |
- If you get to K digits, this is an answer, so add it to your result | |
Time Complexity: | |
- This is a recursive function with a branching factor of 2 and a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func max(a, b int) int { | |
if a > b { | |
return a | |
} | |
return b | |
} | |
func min(a, b int) int { | |
if a < b { | |
return a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[][]} intervals | |
* @return {number} | |
*/ | |
var eraseOverlapIntervals = function(intervals) { | |
intervals.sort((a, b) => a[0] - b[0]); // asc by start time | |
let [result, slow, fast] = [0, 0, 1]; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[][]} intervals | |
* @return {number} | |
*/ | |
var eraseOverlapIntervals = function(intervals) { | |
intervals.sort((a, b) => a[0] - b[0]); // asc by start time | |
let [result, slow, fast] = [0, 0, 1]; | |