MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / logarithmicSort

Function logarithmicSort

javascript/0300-longest-increasing-subsequence.js:85–102  ·  view source on GitHub ↗
(nums, subsequence = [])

Source from the content-addressed store, hash-verified

83};
84
85var logarithmicSort = (nums, subsequence = []) => {
86 for (const num of nums) {
87 /* Time O(N) */
88 const max = subsequence[subsequence.length - 1];
89
90 const canAdd = max < num;
91 if (canAdd) {
92 subsequence.push(num);
93 continue;
94 } /* Space O(N) */
95
96 const index = binarySearch(num, subsequence); /* Time O(log(N)) */
97
98 subsequence[index] = num;
99 }
100
101 return subsequence;
102};
103
104const binarySearch = (num, subsequence) => {
105 let [left, right] = [0, subsequence.length - 1];

Callers 1

lengthOfLISFunction · 0.85

Calls 2

binarySearchFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected