-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c22fcb1
commit 2899f9e
Showing
5 changed files
with
118 additions
and
18 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
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,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'); | ||
}); | ||
}); |
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 @@ | ||
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; | ||
}; |
8 changes: 8 additions & 0 deletions
8
src/page-16/1679. Max Number of K-Sum Pairs/maxOperations.test.ts
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,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
39
src/page-16/1679. Max Number of K-Sum Pairs/maxOperations.ts
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,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; | ||
}; |