Skip to content

Commit

Permalink
153rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 16, 2024
1 parent c22fcb1 commit 2899f9e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 18 deletions.
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ Ace Coding Interview with 75 Qs
[334]: ./src/page-4/334.%20Increasing%20Triplet%20Subsequence/increasingTriplet.ts
[443]: ./src/page-5/443.%20String%20Compression/compress.ts

| Two Pointers | | |
| ------------------------------- | --------------- | ------ |
| 283. Move Zeroes | [Solution][283] | Easy |
| 392. Is Subsequence | [Solution][392] | Easy |
| 11. Container With Most Water | [Solution][11] | Medium |
| 1679. Max Number of K-Sum Pairs | Solution | Medium |
| Two Pointers | | |
| ------------------------------- | ---------------- | ------ |
| 283. Move Zeroes | [Solution][283] | Easy |
| 392. Is Subsequence | [Solution][392] | Easy |
| 11. Container With Most Water | [Solution][11] | Medium |
| 1679. Max Number of K-Sum Pairs | [Solution][1679] | Medium |

[283]: ./src/page-3/283.%20Move%20Zeroes/moveZeroes.ts
[392]: ./src/page-4/392.%20Is%20Subsequence/isSubsequence.ts
[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
[1679]: ./src/page-16/1679.%20Max%20Number%20of%20K-Sum%20Pairs/maxOperations.ts

| Sliding Window | | |
| ------------------------------------------------------------- | -------- | ------ |
Expand Down Expand Up @@ -272,7 +273,7 @@ Must-do List for Interview Prep
| 58. Length of Last Word | [Solution][58] | Easy |
| 14. Longest Common Prefix | [Solution][14] | Easy |
| 151. Reverse Words in a String | Solution | Medium |
| 6. Zigzag Conversion | Solution | Medium |
| 6. Zigzag Conversion | [Solution][6] | Medium |
| 28. Find the Index of the First Occurrence in a String | [Solution][28] | Easy |
| 68. Text Justification | [Solution][68] | Hard |

Expand All @@ -283,6 +284,7 @@ Must-do List for Interview Prep
[13]: ./src/page-1/13.%20Roman%20to%20Integer/romanToInt.ts
[58]: ./src/page-1/58.%20Length%20of%20Last%20Word/length-of-last-word.ts
[14]: ./src/page-1/14.%20Longest%20Common%20Prefix/longestCommonPrefix.ts
[6]: ./src/page-1/6.%20Zigzag%20Conversion/convert.ts
[28]: ./src/page-1/28.%20Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/strStr.ts
[68]: ./src/page-1/68.%20Text%20Justification/fullJustify.ts

Expand Down Expand Up @@ -500,16 +502,20 @@ Must-do List for Interview Prep
| 322. Coin Change | Solution | Medium |
| 300. Longest Increasing Subsequence | Solution | Medium |

| Multidimensional DP | | |
| ---------------------------------------- | -------- | ------ |
| 120. Triangle | Solution | Medium |
| 64. Minimum Path Sum | Solution | Medium |
| 63. Unique Paths II | Solution | Medium |
| 5. Longest Palindromic Substring | Solution | Medium |
| 97. Interleaving String | Solution | Medium |
| 72. Edit Distance | Solution | Medium |
| 123. Best Time to Buy and Sell Stock III | Solution | Hard |
| 188. Best Time to Buy and Sell Stock IV | Solution | Hard |
| 221. Maximal Square | Solution | Medium |
[70]: ./src/page-1/70.%20Climbing%20Stairs/climb-stairs.ts

| Multidimensional DP | | |
| ---------------------------------------- | ------------- | ------ |
| 120. Triangle | Solution | Medium |
| 64. Minimum Path Sum | Solution | Medium |
| 63. Unique Paths II | Solution | Medium |
| 5. Longest Palindromic Substring | [Solution][5] | Medium |
| 97. Interleaving String | Solution | Medium |
| 72. Edit Distance | Solution | Medium |
| 123. Best Time to Buy and Sell Stock III | Solution | Hard |
| 188. Best Time to Buy and Sell Stock IV | Solution | Hard |
| 221. Maximal Square | Solution | Medium |

[5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts

</details>
9 changes: 9 additions & 0 deletions src/page-1/6. Zigzag Conversion/convert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { convert } from './convert';

describe('6. Zigzag Conversion', () => {
test('convert', () => {
expect(convert('PAYPALISHIRING', 3)).toBe('PAHNAPLSIIGYIR');
expect(convert('PAYPALISHIRING', 4)).toBe('PINALSIGYAHRPI');
expect(convert('A', 1)).toBe('A');
});
});
38 changes: 38 additions & 0 deletions src/page-1/6. Zigzag Conversion/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type Convert = (s: string, numRows: number) => string;

/**
* Accepted
*/
export const convert: Convert = (s, numRows) => {
// No zigzag pattern if numRows is 1
if (numRows === 1) return s;

const rows: string[] = [];

for (let i = 0; i < Math.min(numRows, s.length); i++) {
rows[i] = '';
}

let currentRow = 0;
let direction = 1; // 1 means downward, -1 means upward

for (const char of s) {
rows[currentRow] += char;

if (currentRow === 0) {
direction = 1; // Change direction to downward when at the top row
} else if (currentRow === numRows - 1) {
direction = -1; // Change direction to upward when at the bottom row
}

currentRow += direction; // Move to the next row
}

let result = '';

for (const row of rows) {
result += row;
}

return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { maxOperations } from './maxOperations';

describe('1679. Max Number of K-Sum Pairs', () => {
test('maxOperations', () => {
expect(maxOperations([1, 2, 3, 4], 5)).toBe(2);
expect(maxOperations([3, 1, 3, 4, 3], 6)).toBe(1);
});
});
39 changes: 39 additions & 0 deletions src/page-16/1679. Max Number of K-Sum Pairs/maxOperations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type MaxOperations = (nums: number[], k: number) => number;

/**
* Accepted
*/
export const maxOperations: MaxOperations = (nums, k) => {
const numFreq = new Map<number, number>();

let count = 0;

// Count frequencies of each number in nums
for (const num of nums) {
if (!numFreq.has(num)) numFreq.set(num, 0);
numFreq.set(num, (numFreq.get(num) || 0) + 1);
}

// Iterate through nums to find pairs
for (const num of nums) {
const complement = k - num;
const numCount = numFreq.get(num) || 0;
const complementCount = numFreq.get(complement) || 0;

if (numCount > 0 && complementCount > 0) {
if (num === complement) {
// If num and complement are the same, use two of the same number
count += Math.floor(numCount / 2);
numFreq.set(num, 0);
} else {
// Otherwise, use one of each
const minPairs = Math.min(numCount, complementCount);
count += minPairs;
numFreq.set(num, numCount - minPairs);
numFreq.set(complement, complementCount - minPairs);
}
}
}

return count;
};

0 comments on commit 2899f9e

Please sign in to comment.