Skip to content

Commit

Permalink
Merge pull request #439 from tolluset/main
Browse files Browse the repository at this point in the history
οΏ½[μ΄λ³‘ν˜„] Week 5
  • Loading branch information
tolluset authored Sep 14, 2024
2 parents b69d13e + b773d4e commit ec67f28
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 3sum/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* TC: O(n^2)
* SC: O(n)
* */
function threeSum(nums: number[]): number[][] {
const n = nums.length;
const res: number[][] = [];

nums.sort((a, b) => a - b);

for (let i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
}

let left = i + 1,
right = n - 1;

while (left < right) {
const sum = nums[i] + nums[left] + nums[right];

if (sum === 0) {
res.push([nums[i], nums[left], nums[right]]);

while (nums[left] === nums[left + 1]) {
left++;
}

while (nums[right] === nums[right - 1]) {
right++;
}

left++;
right--;

continue;
}

if (sum < 0) {
left++;

continue;
}

right--;
}
}

return res;
}

const tc1 = threeSum([-1, 0, 1, 2, -1, -4]); // [[-1,-1,2],[-1,0,1]]
console.info("πŸš€ : tolluset.ts:39: tc1=", tc1);

const tc2 = threeSum([0, 0, 0]); // [[0,0,0]]
console.info("πŸš€ : tolluset.ts:42: tc2=", tc2);
63 changes: 63 additions & 0 deletions best-time-to-buy-and-sell-stock/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* TC: O(n)
* SC: O(1)
* */
function maxProfitV2(prices: number[]): number {
const n = prices.length;

let min = Infinity,
max = 0;

for (let i = 0; i < n; i++) {
if (prices[i] < min) {
min = prices[i];
continue;
}

if (prices[i] - min > max) {
max = prices[i] - min;
continue;
}
}

return max;
}

const tc1V2 = maxProfitV2([7, 1, 5, 3, 6, 4]);
console.info("πŸš€ : tolluset.ts:27: tc1V2=", tc1V2); // 5

const tc2V2 = maxProfitV2([7, 6, 4, 3, 1]);
console.info("πŸš€ : tolluset.ts:30: tc2V2=", tc2V2); // 0

/*
* @FAILED: Time Limit Exceeded
* TC: O(n^2)
* SC: O(1)
* */
function maxProfit(prices: number[]): number {
const n = prices.length;

let max = 0;

for (let i = 0; i < n; i++) {
let currentMax = 0;

for (let j = i + 1; j < n; j++) {
if (prices[i] <= prices[j]) {
const profit = prices[j] - prices[i];

currentMax = Math.max(currentMax, profit);
}
}

max = Math.max(max, currentMax);
}

return max;
}

const tc1 = maxProfit([7, 1, 5, 3, 6, 4]);
console.info("πŸš€ : tolluset.ts:5: tc1=", tc1); // 5

const tc2 = maxProfit([7, 6, 4, 3, 1]);
console.info("πŸš€ : tolluset.ts:8: tc2=", tc2); // 0
72 changes: 72 additions & 0 deletions group-anagrams/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* n: strs.length, m: strs.mean
* TC: O(n * m * logm)
* SC: O(n * m)
* */
function groupAnagramsV2(strs: string[]): string[][] {
const map = new Map<string, string[]>();

const strSort = (str: string) => str.split("").sort().join("");

for (const str of strs) {
const sortedStr = strSort(str);

if (map.has(sortedStr)) {
map.get(sortedStr)!.push(str);
} else {
map.set(sortedStr, [str]);
}
}

return Array.from(map.values());
}

const tc1V2 = groupAnagramsV2(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"],["nat","tan"],["ate","eat","tea"]]
console.info("πŸš€ : tolluset.ts:19: tc1V2=", tc1V2);

/**
* @FAILED - Time Limit Exceeded
* TC: O(n^2)
* SC: O(n)
*/
function groupAnagrams(strs: string[]): string[][] {
const n = strs.length;

const res: string[][] = [];

const strSort = (str: string) => str.split("").sort().join("");

for (let i = 0; i < n; i++) {
const bucket: string[] = [];
const cur = strs[i];

if (cur === "#") {
continue;
}

bucket.push(cur);

const sortedCur = strSort(cur);

for (let j = i + 1; j < n; j++) {
const tmpSortedStr = strSort(strs[j]);

if (tmpSortedStr === "#") {
continue;
}

if (sortedCur === tmpSortedStr) {
bucket.push(strs[j]);
strs[j] = "#";
}
}

strs[i] = "#";

res.push(bucket);
}

return res;
}

const tc1 = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"],["nat","tan"],["ate","eat","tea"]]
console.info("πŸš€ : tolluset.ts:7: tc1=", tc1);
97 changes: 97 additions & 0 deletions implement-trie-prefix-tree/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class TrieNode {
public children: Map<string, TrieNode>;
public isEnd: boolean;

constructor() {
this.children = new Map();
this.isEnd = false;
}
}

class Trie {
private root: TrieNode;

constructor() {
this.root = new TrieNode();
}

/**
* TC: O(n)
* SC: O(n)
* */
insert(word: string): void {
let node = this.root;

for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}

node = node.children.get(char)!;
}

node.isEnd = true;
}

/**
* TC: O(n)
* SC: O(1)
* */
search(word: string): boolean {
let node = this.root;

for (const char of word) {
if (!node.children.has(char)) {
return false;
}

node = node.children.get(char)!;
}

return node.isEnd;
}

/**
* TC: O(n)
* SC: O(1)
* */
startsWith(prefix: string): boolean {
let node = this.root;

for (const char of prefix) {
if (!node.children.has(char)) {
return false;
}

node = node.children.get(char)!;
}

return true;
}
}

/**
* Your Trie object will be instantiated and called as such:
* var obj = new Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/

const trie = new Trie();

trie.insert("apple");

const tc1 = trie.search("apple"); // return True
console.info("πŸš€ : tolluset.ts:59: tc1=", tc1);

const tc2 = trie.search("app"); // return False
console.info("πŸš€ : tolluset.ts:61: tc2=", tc2);

const tc3 = trie.startsWith("app"); // return True
console.info("πŸš€ : tolluset.ts:63: tc3=", tc3);

trie.insert("app");

const tc4 = trie.search("app"); // return True
console.info("πŸš€ : tolluset.ts:66: tc4=", tc4);
40 changes: 40 additions & 0 deletions word-break/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* TC: O(n^2)
* SC: O(n)
* */
function wordBreak(s: string, wordDict: string[]): boolean {
const n = s.length;
const wordSet = new Set(wordDict);
const dp = Array(n + 1).fill(false);

dp[0] = true;

for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordSet.has(s.slice(j, i))) {
dp[i] = true;
break;
}
}
}

return dp[n];
}

const tc1 = wordBreak("leetcode", ["leet", "code"]); // true
console.info("πŸš€ : tolluset.ts:17: tc1=", tc1);

const tc2 = wordBreak("applepenapple", ["apple", "pen"]); // true
console.info("πŸš€ : tolluset.ts:20: tc2=", tc2);

const tc3 = wordBreak("catsandog", ["cats", "dog", "sand", "and", "cat"]); // false
console.info("πŸš€ : tolluset.ts:23: tc3=", tc3);

const tc4 = wordBreak("cars", ["car", "ca", "rs"]); // true
console.info("πŸš€ : tolluset.ts:27: tc4=", tc4);

const tc5 = wordBreak("aaaaaaa", ["aaaa", "aaa"]); // true
console.info("πŸš€ : tolluset.ts:32: tc5=", tc5);

const tc6 = wordBreak("cbca", ["bc", "ca"]); // false
console.info("πŸš€ : tolluset.ts:43: tc6=", tc6);

0 comments on commit ec67f28

Please sign in to comment.