Skip to content

Commit 5aa53fb

Browse files
authored
Merge pull request #512 from Sunjae95/main
[선재] WEEK08 Solution
2 parents 1d55b0f + e156dd5 commit 5aa53fb

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. brute force -> time limited
5+
* 2. recursion -> failed to implement
6+
* 3. dynamic programming -> failed to implement
7+
* 4. dale solution https://www.algodale.com/problems/longest-repeating-character-replacement/
8+
* n: length of list1 + list2
9+
* time complexity: O(n^3)
10+
* space complexity: O(n)
11+
*/
12+
13+
/**
14+
* brute force
15+
* time complexity: O(n^3)
16+
* space complexity: O(n)
17+
*/
18+
// var characterReplacement = function (s, k) {
19+
// let answer = 0;
20+
// const set = new Set();
21+
// for (let i = 0; i < s.length; i++) set.add(s[i]);
22+
// for (let i = 0; i < s.length; i++) {
23+
// for (const now of set) {
24+
// let current = 0;
25+
// let count = k;
26+
27+
// for (let j = i; j < s.length; j++) {
28+
// if (now === s[j]) current++;
29+
// else if (count) {
30+
// current++;
31+
// count--;
32+
// }
33+
// break;
34+
// }
35+
// answer = Math.max(answer, current);
36+
// }
37+
// }
38+
39+
// return answer;
40+
// };
41+
42+
/**
43+
* time complexity: O(n)
44+
* space complexity: O(1)
45+
*/
46+
var characterReplacement = function (s, k) {
47+
let start = 0;
48+
let answer = 0;
49+
50+
const map = new Map();
51+
for (let i = 0; i < s.length; i++) map.set(s[i], 0);
52+
53+
for (let end = 0; end < s.length; end++) {
54+
map.set(s[end], map.get(s[end]) + 1);
55+
const maxLength = Math.max(...map.values());
56+
57+
while (end - start + 1 - maxLength > k) {
58+
map.set(s[start], map.get(s[start]) - 1);
59+
start++;
60+
}
61+
62+
answer = Math.max(end - start + 1, answer);
63+
}
64+
65+
return answer;
66+
};

merge-two-sorted-lists/sunjae95.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* queue
5+
*
6+
* n: length of list1 + list2
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var mergeTwoLists = function (list1, list2) {
11+
ListNode.prototype.tail = null;
12+
13+
ListNode.prototype.isLast = function () {
14+
return this.val === 0 && this.next === null;
15+
};
16+
17+
ListNode.prototype.pop = function () {
18+
const value = this.val;
19+
this.val = this.next ? this.next.val : 0;
20+
this.next = this.next ? this.next.next : null;
21+
return value;
22+
};
23+
24+
ListNode.prototype.push = function (value) {
25+
const node = new ListNode(value);
26+
if (this.isLast()) {
27+
this.val = value;
28+
return;
29+
}
30+
31+
if (this.tail === null) {
32+
this.next = node;
33+
this.tail = node;
34+
} else {
35+
this.tail.next = node;
36+
this.tail = node;
37+
}
38+
};
39+
40+
if (list1 === null && list2 === null) return null;
41+
if (list1 === null || list2 === null) return list1 ? list1 : list2;
42+
43+
const answer = new ListNode();
44+
45+
while (!(list1.isLast() && list2.isLast())) {
46+
if (list1.isLast()) {
47+
const value = list2.pop();
48+
answer.push(value);
49+
continue;
50+
}
51+
if (list2.isLast()) {
52+
const value = list1.pop();
53+
answer.push(value);
54+
continue;
55+
}
56+
if (list1.val <= list2.val) {
57+
const value = list1.pop();
58+
answer.push(value);
59+
continue;
60+
}
61+
if (list2.val < list1.val) {
62+
const value = list2.pop();
63+
answer.push(value);
64+
continue;
65+
}
66+
}
67+
68+
return answer;
69+
};

0 commit comments

Comments
 (0)