Skip to content

Commit b7dc87f

Browse files
author
sambabib
committed
added js solutions to _3016 && _3175
1 parent 83fdb18 commit b7dc87f

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

javascript/_3016.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function minPushesToType(word) {
2+
let freqMap = new Map();
3+
4+
// Using greedy frequency-based allocation (this is basically gives pirority based on frequenecy of occurence.).
5+
// Count frequency of each letter.
6+
for (let char of word) {
7+
freqMap.set(char, (freqMap.get(char) || 0) + 1);
8+
}
9+
10+
// Sort letters by frequency in descending order.
11+
let frequencies = Array.from(freqMap.values()).sort((a, b) => b - a);
12+
13+
// Assign letters to keys 2-9.
14+
let keys = Array(8).fill(0);
15+
let totalPushes = 0;
16+
17+
for (let i = 0; i < frequencies.length; i++) {
18+
let keyIndex = i % 8;
19+
let presses = Math.floor(i / 8) + 1;
20+
totalPushes += frequencies[i] * presses;
21+
}
22+
23+
return totalPushes;
24+
}

javascript/_3175.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function findWinner(skills, k) {
2+
// This solution uses a queue to process the matches.
3+
let n = skills.length;
4+
let maxSkill = Math.max(...skills);
5+
let queue = [];
6+
7+
let currentWinner = 0;
8+
let currentWins = 0;
9+
10+
for (let i = 1; i < n; i++) {
11+
if (skills[currentWinner] > skills[i]) {
12+
currentWins++;
13+
} else {
14+
currentWinner = i;
15+
currentWins = 1;
16+
}
17+
18+
// If the champion has won k times or is the max skill player, return.
19+
if (currentWins >= k || skills[currentWinner] === maxSkill) {
20+
return currentWinner;
21+
}
22+
}
23+
24+
return currentWinner;
25+
}

paginated_contents/algorithms/4th_thousand/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
| 3185 | [Count Pairs That Form a Complete Day II](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java) | | Medium |
4343
| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java) | | Easy |
4444
| 3178 | [Find the Child Who Has the Ball After K Seconds](https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3178.java) | | Easy |
45-
| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java) | | Medium |
45+
| 3175 | [Find The First Player to win K Games in a Row](https://leetcode.com/problems/find-the-first-player-to-win-k-games-in-a-row/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3175.java) | [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_3175.js) | Medium |
4646
| 3174 | [Clear Digits](https://leetcode.com/problems/clear-digits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3174.java) | | Easy |
4747
| 3173 | [Bitwise OR of Adjacent Elements](https://leetcode.com/problems/bitwise-or-of-adjacent-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3173.java) | | Easy |
4848
| 3168 | [Minimum Number of Chairs in a Waiting Room](https://leetcode.com/problems/minimum-number-of-chairs-in-a-waiting-room/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3168.java) | | Easy |
@@ -78,7 +78,7 @@
7878
| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3024.java) | | Easy |
7979
| 3028 | [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3028.java) | | Easy |
8080
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3019.java) | | Easy |
81-
| 3016 | [Minimum Number of Pushes to Type Word II](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java) | | Medium |
81+
| 3016 | [Minimum Number of Pushes to Type Word II](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3016.java) | [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_3016.js) | Medium |
8282
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3010.java) | | Easy |
8383
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3014.java) | | Easy |
8484
| 3006 | [Find Beautiful Indices in the Given Array I](https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3006.java) | | Medium |

0 commit comments

Comments
 (0)