Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TC: O(N)
// SC: O(1)
function maxProfit(prices: number[]): number {
let maxProfit = 0
let minPrice = Infinity

for(const price of prices) {
maxProfit = Math.max(price - minPrice, maxProfit)
minPrice = Math.min(price, minPrice)
}

return maxProfit

};

102 changes: 102 additions & 0 deletions encode-and-decode-strings/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Because the string may contain any of the 256 legal ASCII characters, your algorithm must be able to handle any character that may appear
*
* Example 1:
* Input: ["lint","code","love","you"]
* Output: ["lint","code","love","you"]
* Explanation:
* One possible encode method is: "lint:;code:;love:;you"
*
* Example 2:
* Input: ["we", "say", ":", "yes"]
* Output: ["we", "say", ":", "yes"]
* Explanation:
* One possible encode method is: "we:;say:;:::;yes"
*
*
*
* encode:
* result = ""
* loop strs,
*
* loop str,
* char to charCode
* not last str AND last char ? charCode + "*"
* not last str AND not last char ? charCode + "+"
*
*
*
* decode:
* strs = []
* split str with "*", => ["12+79+NaN", "92+32"]
* split each element with "+", => [ ["12", "79", "NaN"], ["92", "32"] ]
* loop outer array,
* str = null
* loop inner array,
* element is "NaN" ? ""
* element is not "NaN" ? String.fromCharCode(Number(element))
* str +=
* str is not null ? strs.push(str)
*
*/

// TC: O(N)
// SC: O(N)
class Solution {
encode (strs: string[]): string {
return strs.map((str) => {
if(str.length === 0) {
return "EMPTY"
}
const codes = [];
for(let i = 0; i < str.length; i++) {
codes.push(str.charCodeAt(i));
}
return codes.join('+');
}).join("*")

}

decode(str: string): string[] {
if(str.length === 0) {
return [];
}

return str.split("*").map((encodedStr) => {
if(encodedStr === "EMPTY") {
return "";
}

return encodedStr
.split("+")
.map((code) => String.fromCharCode(Number(code)))
.join("");
})
}
}
const solution = new Solution();



const tc = [
["lint","code","love","you"], // "72+79+80+94*12+34+56+78*"
["we", "say", ":", "yes"],
["", "a", "", "b"], // "NaN*74*NaN*75"
[";::''',,,,.....", ".....,,,,'''::;"],
]

function runTc(tc: string[]): boolean {
const encoded = solution.encode(tc);
const decoded = solution.decode(encoded);
if(tc.length !== decoded.length) {
return false;
}

return tc.every((el, idx) => el === decoded[idx]);
}

const totalTc = tc.length;
const failedTc = tc.reduce((acc, cur) => runTc(cur) ? acc : acc+1, 0);

console.log(`success: ${totalTc-failedTc}/${totalTc}`);

19 changes: 19 additions & 0 deletions group-anagrams/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(N*KlogK)
// SC: O(N*K)
function groupAnagrams(strs: string[]): string[][] {

const idMap = new Map<string, string[]>();

for(const str of strs) {
const id = str.split("").sort().join("");

if(!idMap.has(id)) {
idMap.set(id, [])
}

idMap.get(id)!.push(str)
}

return [...idMap.values()]

};
Loading