Skip to content

Commit 2c43c5b

Browse files
authored
Merge pull request #859 from taewanseoul/main
[Wan] Week 5
2 parents 7db045f + 12ec55e commit 2c43c5b

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 121. Best Time to Buy and Sell Stock
3+
* You are given an array prices where prices[i] is the price of a given stock on the ith day.
4+
* You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
5+
* Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
6+
*
7+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
8+
*/
9+
10+
// O(n) time
11+
// O(1) space
12+
function maxProfit(prices: number[]): number {
13+
let highestProfit = 0;
14+
let left = 0;
15+
let right = 1;
16+
17+
while (right < prices.length) {
18+
if (prices[left] < prices[right]) {
19+
let profit = prices[right] - prices[left];
20+
highestProfit = Math.max(highestProfit, profit);
21+
} else {
22+
left = right;
23+
}
24+
25+
right++;
26+
}
27+
28+
return highestProfit;
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 659 · Encode and Decode Strings
3+
* Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
4+
* Please implement encode and decode
5+
*
6+
* https://leetcode.com/problems/encode-and-decode-strings/description/
7+
* https://www.lintcode.com/problem/659/
8+
*
9+
*/
10+
11+
// O(n) time
12+
// O(1) space
13+
function encode(strs: string[]): string {
14+
let result = "";
15+
16+
for (const str of strs) {
17+
result += `${str.length}#${str}`;
18+
}
19+
20+
return result;
21+
}
22+
23+
// O(n) time
24+
// O(n) space
25+
function decode(str: string) {
26+
const result: string[] = [];
27+
28+
let i = 0;
29+
while (i < str.length) {
30+
let pos = str.indexOf("#", i);
31+
const len = Number(str.slice(i, pos));
32+
const word = str.slice(pos + 1, pos + 1 + len);
33+
result.push(word);
34+
i = pos + 1 + len;
35+
}
36+
37+
return result;
38+
}

group-anagrams/taewanseoul.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 49. Group Anagrams
3+
* Given an array of strings strs, group the anagrams together. You can return the answer in any order.
4+
*
5+
* https://leetcode.com/problems/group-anagrams/description/
6+
*/
7+
8+
// O(n * mlog(m)) time
9+
// O(n) space
10+
function groupAnagrams(strs: string[]): string[][] {
11+
const result: string[][] = [];
12+
const map = new Map<string, string[]>();
13+
14+
for (let i = 0; i < strs.length; i++) {
15+
const word = strs[i];
16+
const sorted = word.split("").sort().join("");
17+
18+
if (map.has(sorted)) {
19+
map.get(sorted)!.push(word);
20+
} else {
21+
map.set(sorted, [word]);
22+
}
23+
}
24+
25+
map.forEach((v) => result.push(v));
26+
27+
return result;
28+
}

0 commit comments

Comments
 (0)