-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
28 changed files
with
954 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,35 @@ | ||
322\. Coin Change | ||
|
||
Medium | ||
|
||
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money. | ||
|
||
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`. | ||
|
||
You may assume that you have an infinite number of each kind of coin. | ||
|
||
**Example 1:** | ||
|
||
**Input:** coins = [1,2,5], amount = 11 | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** 11 = 5 + 5 + 1 | ||
|
||
**Example 2:** | ||
|
||
**Input:** coins = [2], amount = 3 | ||
|
||
**Output:** -1 | ||
|
||
**Example 3:** | ||
|
||
**Input:** coins = [1], amount = 0 | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* `1 <= coins.length <= 12` | ||
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code> | ||
* <code>0 <= amount <= 10<sup>4</sup></code> |
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,31 @@ | ||
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search | ||
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20 | ||
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount) | ||
// #2024_12_22_Time_26_ms_(95.33%)_Space_54.1_MB_(79.36%) | ||
|
||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function(coins, amount) { | ||
const dp = new Array(amount + 1).fill(0) | ||
dp[0] = 1 | ||
|
||
for (const coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
const prev = dp[i - coin] | ||
if (prev > 0) { | ||
if (dp[i] === 0) { | ||
dp[i] = prev + 1 | ||
} else { | ||
dp[i] = Math.min(dp[i], prev + 1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] - 1 | ||
}; | ||
|
||
export { coinChange } |
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,41 @@ | ||
338\. Counting Bits | ||
|
||
Easy | ||
|
||
Given an integer `n`, return _an array_ `ans` _of length_ `n + 1` _such that for each_ `i` (`0 <= i <= n`)_,_ `ans[i]` _is the **number of**_ `1`_**'s** in the binary representation of_ `i`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** n = 2 | ||
|
||
**Output:** [0,1,1] | ||
|
||
**Explanation:** | ||
|
||
0 --> 0 | ||
1 --> 1 | ||
2 --> 10 | ||
|
||
**Example 2:** | ||
|
||
**Input:** n = 5 | ||
|
||
**Output:** [0,1,1,2,1,2] | ||
|
||
**Explanation:** | ||
|
||
0 --> 0 | ||
1 --> 1 | ||
2 --> 10 | ||
3 --> 11 | ||
4 --> 100 | ||
5 --> 101 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= n <= 10<sup>5</sup></code> | ||
|
||
**Follow up:** | ||
|
||
* It is very easy to come up with a solution with a runtime of `O(n log n)`. Can you do it in linear time `O(n)` and possibly in a single pass? | ||
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)? |
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,27 @@ | ||
// #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation | ||
// #Big_O_Time_O(num)_Space_O(num) #2024_12_22_Time_0_ms_(100.00%)_Space_57_MB_(33.09%) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function(num) { | ||
const result = new Array(num + 1).fill(0) | ||
let borderPos = 1 | ||
let incrPos = 1 | ||
|
||
for (let i = 1; i <= num; i++) { | ||
// When we reach a power of 2, reset `borderPos` and `incrPos` | ||
if (incrPos === borderPos) { | ||
result[i] = 1 | ||
incrPos = 1 | ||
borderPos = i | ||
} else { | ||
result[i] = 1 + result[incrPos++] | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
export { countBits } |
26 changes: 26 additions & 0 deletions
26
src/main/js/g0301_0400/s0347_top_k_frequent_elements/readme.md
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,26 @@ | ||
347\. Top K Frequent Elements | ||
|
||
Medium | ||
|
||
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,1,1,2,2,3], k = 2 | ||
|
||
**Output:** [1,2] | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1], k = 1 | ||
|
||
**Output:** [1] | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 10<sup>5</sup></code> | ||
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code> | ||
* `k` is in the range `[1, the number of unique elements in the array]`. | ||
* It is **guaranteed** that the answer is **unique**. | ||
|
||
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size. |
22 changes: 22 additions & 0 deletions
22
src/main/js/g0301_0400/s0347_top_k_frequent_elements/solution.js
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,22 @@ | ||
// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting | ||
// #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue | ||
// #Big_O_Time_O(n*log(n))_Space_O(k) #2024_12_22_Time_6_ms_(95.00%)_Space_54.3_MB_(53.50%) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function (nums, k) { | ||
let obj = {}, result = [] | ||
for (let item of nums) { | ||
obj[item] = (obj[item] ? obj[item] : 0) + 1 | ||
} | ||
let temp = Object.entries(obj).sort((a, b) => b[1] - a[1]) | ||
for (let i = 0; i < k; i++) { | ||
result.push(Number(temp[i][0])) | ||
} | ||
return result | ||
}; | ||
|
||
export { topKFrequent } |
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,36 @@ | ||
394\. Decode String | ||
|
||
Medium | ||
|
||
Given an encoded string, return its decoded string. | ||
|
||
The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. | ||
|
||
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, `k`. For example, there will not be input like `3a` or `2[4]`. | ||
|
||
The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "3[a]2[bc]" | ||
|
||
**Output:** "aaabcbc" | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "3[a2[c]]" | ||
|
||
**Output:** "accaccacc" | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "2[abc]3[cd]ef" | ||
|
||
**Output:** "abcabccdcdcdef" | ||
|
||
**Constraints:** | ||
|
||
* `1 <= s.length <= 30` | ||
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`. | ||
* `s` is guaranteed to be **a valid** input. | ||
* All the integers in `s` are in the range `[1, 300]`. |
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,41 @@ | ||
// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings | ||
// #Big_O_Time_O(n)_Space_O(n) #2024_12_22_Time_0_ms_(100.00%)_Space_49.4_MB_(10.78%) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {string} | ||
*/ | ||
var decodeString = function(s) { | ||
let i = 0 | ||
|
||
const helper = () => { | ||
let count = 0 | ||
let sb = '' | ||
|
||
while (i < s.length) { | ||
const c = s[i] | ||
i++ | ||
|
||
if (/[a-zA-Z]/.test(c)) { | ||
sb += c | ||
} else if (/\d/.test(c)) { | ||
count = count * 10 + Number(c) | ||
} else if (c === ']') { | ||
break | ||
} else if (c === '[') { | ||
// Recursive call for the substring | ||
const repeat = helper() | ||
while (count > 0) { | ||
sb += repeat | ||
count-- | ||
} | ||
} | ||
} | ||
|
||
return sb | ||
} | ||
|
||
return helper() | ||
}; | ||
|
||
export { decodeString } |
26 changes: 26 additions & 0 deletions
26
src/main/js/g0401_0500/s0416_partition_equal_subset_sum/readme.md
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,26 @@ | ||
416\. Partition Equal Subset Sum | ||
|
||
Medium | ||
|
||
Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,5,11,5] | ||
|
||
**Output:** true | ||
|
||
**Explanation:** The array can be partitioned as [1, 5, 5] and [11]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,5] | ||
|
||
**Output:** false | ||
|
||
**Explanation:** The array cannot be partitioned into equal sum subsets. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 200` | ||
* `1 <= nums[i] <= 100` |
35 changes: 35 additions & 0 deletions
35
src/main/js/g0401_0500/s0416_partition_equal_subset_sum/solution.js
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,35 @@ | ||
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming | ||
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_12_22_Time_18_ms_(97.98%)_Space_51.4_MB_(95.09%) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canPartition = function(nums) { | ||
let sum = nums.reduce((acc, val) => acc + val, 0) | ||
if (sum % 2 !== 0) { | ||
return false | ||
} | ||
sum /= 2 | ||
|
||
const set = new Array(sum + 1).fill(false) | ||
const arr = new Array(sum + 2).fill(0) | ||
let top = 0 | ||
|
||
for (let val of nums) { | ||
for (let i = top; i >= 0; i--) { | ||
const tempSum = val + arr[i] | ||
if (tempSum <= sum && !set[tempSum]) { | ||
if (tempSum === sum) { | ||
return true | ||
} | ||
set[tempSum] = true | ||
arr[++top] = tempSum | ||
} | ||
} | ||
} | ||
|
||
return false | ||
}; | ||
|
||
export { canPartition } |
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 @@ | ||
437\. Path Sum III | ||
|
||
Medium | ||
|
||
Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`. | ||
|
||
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes). | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg) | ||
|
||
**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The paths that sum to 8 are shown. | ||
|
||
**Example 2:** | ||
|
||
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 | ||
|
||
**Output:** 3 | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in the tree is in the range `[0, 1000]`. | ||
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code> | ||
* `-1000 <= targetSum <= 1000` |
Oops, something went wrong.