-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #859 from taewanseoul/main
[Wan] Week 5
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 121. Best Time to Buy and Sell Stock | ||
* You are given an array prices where prices[i] is the price of a given stock on the ith day. | ||
* 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. | ||
* Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. | ||
* | ||
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ | ||
*/ | ||
|
||
// O(n) time | ||
// O(1) space | ||
function maxProfit(prices: number[]): number { | ||
let highestProfit = 0; | ||
let left = 0; | ||
let right = 1; | ||
|
||
while (right < prices.length) { | ||
if (prices[left] < prices[right]) { | ||
let profit = prices[right] - prices[left]; | ||
highestProfit = Math.max(highestProfit, profit); | ||
} else { | ||
left = right; | ||
} | ||
|
||
right++; | ||
} | ||
|
||
return highestProfit; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 659 · Encode and Decode Strings | ||
* 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. | ||
* Please implement encode and decode | ||
* | ||
* https://leetcode.com/problems/encode-and-decode-strings/description/ | ||
* https://www.lintcode.com/problem/659/ | ||
* | ||
*/ | ||
|
||
// O(n) time | ||
// O(1) space | ||
function encode(strs: string[]): string { | ||
let result = ""; | ||
|
||
for (const str of strs) { | ||
result += `${str.length}#${str}`; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// O(n) time | ||
// O(n) space | ||
function decode(str: string) { | ||
const result: string[] = []; | ||
|
||
let i = 0; | ||
while (i < str.length) { | ||
let pos = str.indexOf("#", i); | ||
const len = Number(str.slice(i, pos)); | ||
const word = str.slice(pos + 1, pos + 1 + len); | ||
result.push(word); | ||
i = pos + 1 + len; | ||
} | ||
|
||
return result; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* 49. Group Anagrams | ||
* Given an array of strings strs, group the anagrams together. You can return the answer in any order. | ||
* | ||
* https://leetcode.com/problems/group-anagrams/description/ | ||
*/ | ||
|
||
// O(n * mlog(m)) time | ||
// O(n) space | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const result: string[][] = []; | ||
const map = new Map<string, string[]>(); | ||
|
||
for (let i = 0; i < strs.length; i++) { | ||
const word = strs[i]; | ||
const sorted = word.split("").sort().join(""); | ||
|
||
if (map.has(sorted)) { | ||
map.get(sorted)!.push(word); | ||
} else { | ||
map.set(sorted, [word]); | ||
} | ||
} | ||
|
||
map.forEach((v) => result.push(v)); | ||
|
||
return result; | ||
} |