Skip to content

Commit 9a10533

Browse files
committed
Added js tasks
1 parent 537d0ab commit 9a10533

File tree

28 files changed

+954
-0
lines changed

28 files changed

+954
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
322\. Coin Change
2+
3+
Medium
4+
5+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
6+
7+
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`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
**Example 1:**
12+
13+
**Input:** coins = [1,2,5], amount = 11
14+
15+
**Output:** 3
16+
17+
**Explanation:** 11 = 5 + 5 + 1
18+
19+
**Example 2:**
20+
21+
**Input:** coins = [2], amount = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** coins = [1], amount = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
* `1 <= coins.length <= 12`
34+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
35+
* <code>0 <= amount <= 10<sup>4</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Breadth_First_Search
2+
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
3+
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount)
4+
// #2024_12_22_Time_26_ms_(95.33%)_Space_54.1_MB_(79.36%)
5+
6+
/**
7+
* @param {number[]} coins
8+
* @param {number} amount
9+
* @return {number}
10+
*/
11+
var coinChange = function(coins, amount) {
12+
const dp = new Array(amount + 1).fill(0)
13+
dp[0] = 1
14+
15+
for (const coin of coins) {
16+
for (let i = coin; i <= amount; i++) {
17+
const prev = dp[i - coin]
18+
if (prev > 0) {
19+
if (dp[i] === 0) {
20+
dp[i] = prev + 1
21+
} else {
22+
dp[i] = Math.min(dp[i], prev + 1)
23+
}
24+
}
25+
}
26+
}
27+
28+
return dp[amount] - 1
29+
};
30+
31+
export { coinChange }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
338\. Counting Bits
2+
3+
Easy
4+
5+
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`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 2
10+
11+
**Output:** [0,1,1]
12+
13+
**Explanation:**
14+
15+
0 --> 0
16+
1 --> 1
17+
2 --> 10
18+
19+
**Example 2:**
20+
21+
**Input:** n = 5
22+
23+
**Output:** [0,1,1,2,1,2]
24+
25+
**Explanation:**
26+
27+
0 --> 0
28+
1 --> 1
29+
2 --> 10
30+
3 --> 11
31+
4 --> 100
32+
5 --> 101
33+
34+
**Constraints:**
35+
36+
* <code>0 <= n <= 10<sup>5</sup></code>
37+
38+
**Follow up:**
39+
40+
* 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?
41+
* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
2+
// #Big_O_Time_O(num)_Space_O(num) #2024_12_22_Time_0_ms_(100.00%)_Space_57_MB_(33.09%)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number[]}
7+
*/
8+
var countBits = function(num) {
9+
const result = new Array(num + 1).fill(0)
10+
let borderPos = 1
11+
let incrPos = 1
12+
13+
for (let i = 1; i <= num; i++) {
14+
// When we reach a power of 2, reset `borderPos` and `incrPos`
15+
if (incrPos === borderPos) {
16+
result[i] = 1
17+
incrPos = 1
18+
borderPos = i
19+
} else {
20+
result[i] = 1 + result[incrPos++]
21+
}
22+
}
23+
24+
return result
25+
}
26+
27+
export { countBits }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
347\. Top K Frequent Elements
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,1,1,2,2,3], k = 2
10+
11+
**Output:** [1,2]
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1], k = 1
16+
17+
**Output:** [1]
18+
19+
**Constraints:**
20+
21+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
22+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
23+
* `k` is in the range `[1, the number of unique elements in the array]`.
24+
* It is **guaranteed** that the answer is **unique**.
25+
26+
**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting
2+
// #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue
3+
// #Big_O_Time_O(n*log(n))_Space_O(k) #2024_12_22_Time_6_ms_(95.00%)_Space_54.3_MB_(53.50%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} k
8+
* @return {number[]}
9+
*/
10+
var topKFrequent = function (nums, k) {
11+
let obj = {}, result = []
12+
for (let item of nums) {
13+
obj[item] = (obj[item] ? obj[item] : 0) + 1
14+
}
15+
let temp = Object.entries(obj).sort((a, b) => b[1] - a[1])
16+
for (let i = 0; i < k; i++) {
17+
result.push(Number(temp[i][0]))
18+
}
19+
return result
20+
};
21+
22+
export { topKFrequent }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
394\. Decode String
2+
3+
Medium
4+
5+
Given an encoded string, return its decoded string.
6+
7+
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.
8+
9+
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]`.
10+
11+
The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "3[a]2[bc]"
16+
17+
**Output:** "aaabcbc"
18+
19+
**Example 2:**
20+
21+
**Input:** s = "3[a2[c]]"
22+
23+
**Output:** "accaccacc"
24+
25+
**Example 3:**
26+
27+
**Input:** s = "2[abc]3[cd]ef"
28+
29+
**Output:** "abcabccdcdcdef"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 30`
34+
* `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
35+
* `s` is guaranteed to be **a valid** input.
36+
* All the integers in `s` are in the range `[1, 300]`.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
2+
// #Big_O_Time_O(n)_Space_O(n) #2024_12_22_Time_0_ms_(100.00%)_Space_49.4_MB_(10.78%)
3+
4+
/**
5+
* @param {string} s
6+
* @return {string}
7+
*/
8+
var decodeString = function(s) {
9+
let i = 0
10+
11+
const helper = () => {
12+
let count = 0
13+
let sb = ''
14+
15+
while (i < s.length) {
16+
const c = s[i]
17+
i++
18+
19+
if (/[a-zA-Z]/.test(c)) {
20+
sb += c
21+
} else if (/\d/.test(c)) {
22+
count = count * 10 + Number(c)
23+
} else if (c === ']') {
24+
break
25+
} else if (c === '[') {
26+
// Recursive call for the substring
27+
const repeat = helper()
28+
while (count > 0) {
29+
sb += repeat
30+
count--
31+
}
32+
}
33+
}
34+
35+
return sb
36+
}
37+
38+
return helper()
39+
};
40+
41+
export { decodeString }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
416\. Partition Equal Subset Sum
2+
3+
Medium
4+
5+
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.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,5,11,5]
10+
11+
**Output:** true
12+
13+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3,5]
18+
19+
**Output:** false
20+
21+
**Explanation:** The array cannot be partitioned into equal sum subsets.
22+
23+
**Constraints:**
24+
25+
* `1 <= nums.length <= 200`
26+
* `1 <= nums[i] <= 100`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
2+
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_12_22_Time_18_ms_(97.98%)_Space_51.4_MB_(95.09%)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {boolean}
7+
*/
8+
var canPartition = function(nums) {
9+
let sum = nums.reduce((acc, val) => acc + val, 0)
10+
if (sum % 2 !== 0) {
11+
return false
12+
}
13+
sum /= 2
14+
15+
const set = new Array(sum + 1).fill(false)
16+
const arr = new Array(sum + 2).fill(0)
17+
let top = 0
18+
19+
for (let val of nums) {
20+
for (let i = top; i >= 0; i--) {
21+
const tempSum = val + arr[i]
22+
if (tempSum <= sum && !set[tempSum]) {
23+
if (tempSum === sum) {
24+
return true
25+
}
26+
set[tempSum] = true
27+
arr[++top] = tempSum
28+
}
29+
}
30+
}
31+
32+
return false
33+
};
34+
35+
export { canPartition }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
437\. Path Sum III
2+
3+
Medium
4+
5+
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`.
6+
7+
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).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
12+
13+
**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
14+
15+
**Output:** 3
16+
17+
**Explanation:** The paths that sum to 8 are shown.
18+
19+
**Example 2:**
20+
21+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
22+
23+
**Output:** 3
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[0, 1000]`.
28+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
29+
* `-1000 <= targetSum <= 1000`

0 commit comments

Comments
 (0)