diff --git a/longest-repeating-character-replacement/sunjae95.js b/longest-repeating-character-replacement/sunjae95.js new file mode 100644 index 000000000..e3313c7db --- /dev/null +++ b/longest-repeating-character-replacement/sunjae95.js @@ -0,0 +1,66 @@ +/** + * @description + * brainstorming: + * 1. brute force -> time limited + * 2. recursion -> failed to implement + * 3. dynamic programming -> failed to implement + * 4. dale solution https://www.algodale.com/problems/longest-repeating-character-replacement/ + * n: length of list1 + list2 + * time complexity: O(n^3) + * space complexity: O(n) + */ + +/** + * brute force + * time complexity: O(n^3) + * space complexity: O(n) + */ +// var characterReplacement = function (s, k) { +// let answer = 0; +// const set = new Set(); +// for (let i = 0; i < s.length; i++) set.add(s[i]); +// for (let i = 0; i < s.length; i++) { +// for (const now of set) { +// let current = 0; +// let count = k; + +// for (let j = i; j < s.length; j++) { +// if (now === s[j]) current++; +// else if (count) { +// current++; +// count--; +// } +// break; +// } +// answer = Math.max(answer, current); +// } +// } + +// return answer; +// }; + +/** + * time complexity: O(n) + * space complexity: O(1) + */ +var characterReplacement = function (s, k) { + let start = 0; + let answer = 0; + + const map = new Map(); + for (let i = 0; i < s.length; i++) map.set(s[i], 0); + + for (let end = 0; end < s.length; end++) { + map.set(s[end], map.get(s[end]) + 1); + const maxLength = Math.max(...map.values()); + + while (end - start + 1 - maxLength > k) { + map.set(s[start], map.get(s[start]) - 1); + start++; + } + + answer = Math.max(end - start + 1, answer); + } + + return answer; +}; diff --git a/merge-two-sorted-lists/sunjae95.js b/merge-two-sorted-lists/sunjae95.js new file mode 100644 index 000000000..a4a451fab --- /dev/null +++ b/merge-two-sorted-lists/sunjae95.js @@ -0,0 +1,69 @@ +/** + * @description + * brainstorming: + * queue + * + * n: length of list1 + list2 + * time complexity: O(n) + * space complexity: O(n) + */ +var mergeTwoLists = function (list1, list2) { + ListNode.prototype.tail = null; + + ListNode.prototype.isLast = function () { + return this.val === 0 && this.next === null; + }; + + ListNode.prototype.pop = function () { + const value = this.val; + this.val = this.next ? this.next.val : 0; + this.next = this.next ? this.next.next : null; + return value; + }; + + ListNode.prototype.push = function (value) { + const node = new ListNode(value); + if (this.isLast()) { + this.val = value; + return; + } + + if (this.tail === null) { + this.next = node; + this.tail = node; + } else { + this.tail.next = node; + this.tail = node; + } + }; + + if (list1 === null && list2 === null) return null; + if (list1 === null || list2 === null) return list1 ? list1 : list2; + + const answer = new ListNode(); + + while (!(list1.isLast() && list2.isLast())) { + if (list1.isLast()) { + const value = list2.pop(); + answer.push(value); + continue; + } + if (list2.isLast()) { + const value = list1.pop(); + answer.push(value); + continue; + } + if (list1.val <= list2.val) { + const value = list1.pop(); + answer.push(value); + continue; + } + if (list2.val < list1.val) { + const value = list2.pop(); + answer.push(value); + continue; + } + } + + return answer; +};