-
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.
1. Additional problems solution 2. Refactored and formatted code
- Loading branch information
1 parent
539a305
commit bf1068d
Showing
3 changed files
with
147 additions
and
7 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,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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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