Skip to content

Commit

Permalink
1. Additional problems solution 2. Refactored and formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
anchit-choudhry committed Aug 3, 2024
1 parent 539a305 commit bf1068d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 7 deletions.
51 changes: 51 additions & 0 deletions src/main/java/com/acesoft/leetcode/LeetCode3210.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.acesoft.leetcode;

/*
* https://leetcode.com/problems/find-the-encrypted-string/description
*
* You are given a string s and an integer k. Encrypt the string using the following algorithm:
*
* For each character c in s, replace c with the kth character after c in the string (in a cyclic
* manner).
* Return the encrypted string.
*
* Example 1:
* Input: s = "dart", k = 3
* Output: "tdar"
* Explanation:
* For i = 0, the 3rd character after 'd' is 't'.
* For i = 1, the 3rd character after 'a' is 'd'.
* For i = 2, the 3rd character after 'r' is 'a'.
* For i = 3, the 3rd character after 't' is 'r'.
*
* Example 2:
* Input: s = "aaa", k = 1
* Output: "aaa"
* Explanation:
* As all the characters are the same, the encrypted string will also be the same.
*
* Constraints:
* 1 <= s.length <= 100
* 1 <= k <= 10^4
* s consists only of lowercase English letters.
*/
public final class LeetCode3210 {

public String getEncryptedString(final String s, final int k) {
if (s.length() == 1) {
return s;
}
int index = s.length() > k ? k : k % s.length();
int count = s.length();
final StringBuilder stringBuilder = new StringBuilder();
while (count > 0) {
if (index == s.length()) {
index = 0;
}
stringBuilder.append(s.charAt(index));
index++;
count--;
}
return stringBuilder.toString();
}
}
86 changes: 86 additions & 0 deletions src/main/java/com/acesoft/leetcode/LeetCode3238.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.acesoft.leetcode;

import java.util.HashMap;
import java.util.Map;

/*
* https://leetcode.com/problems/find-the-number-of-winning-players
*
* You are given an integer n representing the number of players in a game and a 2D array pick
* where pick[i] = [xi, yi] represents that the player xi picked a ball of color yi.
*
* Player i wins the game if they pick strictly more than i balls of the same color. In other words,
*
* Player 0 wins if they pick any ball.
* Player 1 wins if they pick at least two balls of the same color.
* ...
* Player i wins if they pick at least i + 1 balls of the same color.
*
* Return the number of players who win the game.
*
* Note that multiple players can win the game.
*
* Example 1:
* Input: n = 4, pick = [[0,0],[1,0],[1,0],[2,1],[2,1],[2,0]]
* Output: 2
* Explanation:
* Player 0 and player 1 win the game, while players 2 and 3 do not win.
*
* Example 2:
* Input: n = 5, pick = [[1,1],[1,2],[1,3],[1,4]]
* Output: 0
* Explanation:
* No player wins the game.
*
* Example 3:
* Input: n = 5, pick = [[1,1],[2,4],[2,4],[2,4]]
* Output: 1
* Explanation:
* Player 2 wins the game by picking 3 balls with color 4.
*
* Constraints:
* 2 <= n <= 10
* 1 <= pick.length <= 100
* pick[i].length == 2
* 0 <= xi <= n - 1
* 0 <= yi <= 10
*/
public final class LeetCode3238 {

public int winningPlayerCount(final int n, final int[][] pick) {
final Map<Integer, Map<Integer, Integer>> frequency = new HashMap<>();
int count = 0, countOnes = 0;
for (final int[] selection : pick) {
if (selection[0] == 0) {
countOnes = 1;
continue;
}
Map<Integer, Integer> playerFrequency = new HashMap<>();
if (frequency.containsKey(selection[0])) {
playerFrequency = frequency.get(selection[0]);
if (playerFrequency.containsKey(selection[1])) {
playerFrequency.put(selection[1], playerFrequency.get(selection[1]) + 1);
} else {
playerFrequency.put(selection[1], 1);
}
} else {
playerFrequency.put(selection[1], 1);
}
frequency.put(selection[0], playerFrequency);
}
for (final Map.Entry<Integer, Map<Integer, Integer>> playersMap : frequency.entrySet()) {
final int playerName = playersMap.getKey();
boolean flag = false;
for (final Map.Entry<Integer, Integer> keyValue : playersMap.getValue().entrySet()) {
if (keyValue.getValue() > playerName) {
flag = true;
break;
}
}
if (flag) {
count++;
}
}
return count + countOnes;
}
}
17 changes: 10 additions & 7 deletions src/main/java/com/acesoft/leetcode/LeetCode70.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@
* Constraints:
* 1 <= n <= 45
*/
public class LeetCode70 {
public final class LeetCode70 {

private final int[] cache = new int[46];

public int climbStairs(int n) {
if (n < 2) {
return 1;
public int climbStairs(final int n) {
if (n < 1) {
return 0;
}
if (n < 3) {
return n;
}
if (cache[n] != 0) {
return cache[n];
}
int temp = climbStairs(n - 1) + climbStairs(n - 2);
cache[n] = temp;
return temp;
final int count = climbStairs(n - 1) + climbStairs(n - 2);
cache[n] = count;
return count;
}
}

0 comments on commit bf1068d

Please sign in to comment.