Skip to content

Commit

Permalink
[Add:Solution] resolve daily problem of leetcode: Eliminate Max Monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
abdorah committed Nov 11, 2023
1 parent 659cd41 commit 7b35976
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 90 deletions.
17 changes: 11 additions & 6 deletions src/main/java/com/AlmostEquivalentString.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com;

/**
* Snowflake
*
* @author KOTBI Abderrahmane
* @version 1.1
* @see <a
* href="https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/description/"><b>Leetcode-2068.
* Check Whether Two Strings are Almost Equivalent</b></a>
*/
public class AlmostEquivalentString {

/**
* @param word
* @param search
* @return the numbre of occurences of the String search in the String word
* @param word The base of the search.
* @param search The target of the search.
* @return The numbre of occurences of the String search in the String word
*/
public static int numberOfOccurence(String word, String search) {
int count = 0;
Expand All @@ -20,9 +25,9 @@ public static int numberOfOccurence(String word, String search) {
}

/**
* @param word1
* @param word2
* @return tell if the two Strings word1 and word2 are almost equivalent. Therefore, it returns
* @param word1 First word to check against.
* @param word2 Second word to check with.
* @return Tell if the two Strings word1 and word2 are almost equivalent. Therefore, it returns
* true only if for each character in those two the difference between the number of occurence
* of each one of them is at most three.
*/
Expand Down
48 changes: 28 additions & 20 deletions src/main/java/com/CapitilizedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@
import java.util.List;

/**
* Snowflake
*
* @author KOTBI Abderrahmane
* @version 1.1
* @see <a href="https://leetcode.com/problems/capitalize-the-title/"><b>Leetcode-2129. Capitalize
* the Title</b></a>
*/
public class CapitilizedString {

/**
* @param title
* @return return the string title capitalized. This means that the first letter of each word is a capital letter.
* Only if the word's lenght is 2 or less, then it will be all in lower case.
*/
public static String capitalizeTitle(String title) {
List<String> l = Arrays.asList(title.split(" ")).stream().map((e) -> {
String v = "";
v = e.toLowerCase();
if(e.length()> 2){
v = v.substring(0, 1).toUpperCase() + v.substring(1);
}
return v;
}).toList();
String o = "";
for (String s : l) {
o = o + " " + s;
}
return o.trim();
/**
* @param title The input string to be capitalized.
* @return The string title capitalized. This means that the first letter of each word is a
* capital letter. Only if the word's lenght is 2 or less, then it will be all in lower case.
*/
public static String capitalizeTitle(String title) {
List<String> l =
Arrays.asList(title.split(" ")).stream()
.map(
(e) -> {
String v = "";
v = e.toLowerCase();
if (e.length() > 2) {
v = v.substring(0, 1).toUpperCase() + v.substring(1);
}
return v;
})
.toList();
String o = "";
for (String s : l) {
o = o + " " + s;
}
}
return o.trim();
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/ColoredPiecesGame.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com;

/**
* Cyclone
*
* @author KOTBI Abderrahmane
* @version 1.1
* @see <a
* href="https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/"><b>Leetcode-2038.
* Remove Colored Pieces if Both Neighbors are the Same Color </b></a>
*/
public class ColoredPiecesGame {

/**
* @param colors
* @return true if the winner is 'A' (Alice) is the winner, and false if 'B' (Bobe) is the winner.
* @param colors The input string of colors.
* @return True if the winner is 'A' (Alice) is the winner, and false if 'B' (Bobe) is the winner.
*/
public static boolean winnerOfGame(String colors) {
int aliceScore = 0;
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/DistinctString.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com;

/**
* Cyclone
*
* @author KOTBI Abderrahmane
* @version 1.1
* @see <a href="https://leetcode.com/problems/kth-distinct-string-in-an-array/"><b>Leetcode-2053.
* Kth Distinct String in an Array</b></a>
*/
public class DistinctString {

/**
* @param array
* @param index
* @return true if the String at the @param index is distinct
* @param array Input array of strings to check if they are distinct.
* @param index The index of the string to compare against.
* @return True if the String at the @param index is distinct
*/
public static boolean isDistinct(String[] array, int index) {
int num = 0;
Expand All @@ -21,9 +25,9 @@ public static boolean isDistinct(String[] array, int index) {
}

/**
* @param arr
* @param k
* @return the @param k th distinct String in @param arr
* @param arr Input array of strings to check if they are distinct.
* @param k The index of the string to compare against.
* @return The @param k th distinct String in @param arr
*/
public static String kthDistinct(String[] arr, int k) {
int count = 0;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/EliminateMaxMonster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Snowflake
*
* @author KOTBI Abderrahmane
* @version 1.1
* @see <a
* href="https://leetcode.com/problems/eliminate-maximum-number-of-monsters/description/?envType=daily-question&envId=2023-11-07"><b>Leetcode-1921.
* Eliminate Maximum Number of Monsters</b></a>
*/
public class EliminateMaxMonster {

/**
* This method follows the iterative approach of killing monsters as long as we have bullets.
*
* @param dist Array wherein the index i represents the initial distance from the monster i.
* @param speed Array of speed of every monster.
* @return The number of cycles in the game before losing or wining.
*/
public static int eliminateMaximum(int[] dist, int[] speed) {
int current = 0;

List<Integer> cycles =
new ArrayList<>(
IntStream.range(0, dist.length)
.map(i -> dist[i] % speed[i] > 0 ? dist[i] / speed[i] + 1 : dist[i] / speed[i])
.sorted()
.boxed()
.collect(Collectors.toList()));
for (int cycle : cycles) {
if (cycle > current) {
current++;
} else if (cycle == current) {
break;
}
}
return current;
}
}
18 changes: 11 additions & 7 deletions src/main/java/com/LonguestSentence.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
*/
public class LonguestSentence {

/**
* @param sentences
* @return the longest sentence of the array sentences
*/
public static int mostWordsFound(String[] sentences) {
return Arrays.stream(sentences).max((a, n) -> a.split(" ").length - n.split(" ").length).get().split(" ").length;
}
/**
* @param sentences
* @return the longest sentence of the array sentences
*/
public static int mostWordsFound(String[] sentences) {
return Arrays.stream(sentences)
.max((a, n) -> a.split(" ").length - n.split(" ").length)
.get()
.split(" ")
.length;
}
}
69 changes: 33 additions & 36 deletions src/main/java/com/TwinSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,43 @@
*/
public class TwinSum {

/**
* This class has been added from leetcode to define the singly linked list.
*/
public static class ListNode {
int val;
ListNode next;
/** This class has been added from leetcode to define the singly linked list. */
public static class ListNode {
int val;
ListNode next;

ListNode() {
}
ListNode() {}

ListNode(int val) {
this.val = val;
}
ListNode(int val) {
this.val = val;
}

ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

/**
* @param head
* @return the maximum sum of pair numbers in the linked list head. You can find detailed explanaition of what are
* pair numbers in the related leetcode problem.
*/
public static int pairSum(ListNode head) {
ListNode acctual = head;
List<Integer> values = new ArrayList<Integer>();
while (acctual != null) {
values.add(acctual.val);
acctual = acctual.next;
}
int sum = 0;
int best = 0;
for (int i = 0; i < values.size()/2; i++) {
sum = values.get(i) + values.get(values.size() - 1 - i);
if (sum > best) {
best = sum;
}
}
return best;
/**
* @param head
* @return the maximum sum of pair numbers in the linked list head. You can find detailed
* explanaition of what are pair numbers in the related leetcode problem.
*/
public static int pairSum(ListNode head) {
ListNode acctual = head;
List<Integer> values = new ArrayList<Integer>();
while (acctual != null) {
values.add(acctual.val);
acctual = acctual.next;
}
int sum = 0;
int best = 0;
for (int i = 0; i < values.size() / 2; i++) {
sum = values.get(i) + values.get(values.size() - 1 - i);
if (sum > best) {
best = sum;
}
}
return best;
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/CapitilizedStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @see CapitilizedString.capitalizeTitle
*/
public class CapitilizedStringTest {
@Test
void testCapitalizeTitle() {
assertEquals("Capitalize The Title", CapitilizedString.capitalizeTitle("capiTalIze tHe titLe"));
}
@Test
void testCapitalizeTitle() {
assertEquals("Capitalize The Title", CapitilizedString.capitalizeTitle("capiTalIze tHe titLe"));
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/EliminateMaxMonsterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* @author KOTBI Abderrahmane
* @version 1.1
* @see EliminateMaxMonster.eliminateMaximum
*/
public class EliminateMaxMonsterTest {

@Test
void testZeroKilled() {
assertEquals(1, EliminateMaxMonster.eliminateMaximum(new int[] {3, 2, 4}, new int[] {5, 3, 2}));
}

@Test
void testAllKilled() {
assertEquals(3, EliminateMaxMonster.eliminateMaximum(new int[] {1, 3, 4}, new int[] {1, 1, 1}));
}

@Test
void testTwoKilled() {
assertEquals(
1, EliminateMaxMonster.eliminateMaximum(new int[] {1, 1, 2, 3}, new int[] {1, 1, 1, 1}));
}

@Test
void testThreeKilled() {
assertEquals(3, EliminateMaxMonster.eliminateMaximum(new int[] {4, 2, 3}, new int[] {2, 1, 1}));
}
}
13 changes: 8 additions & 5 deletions src/test/java/com/LonguestSentenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
* @see LonguestSentence.mostWordsFound
*/
public class LonguestSentenceTest {
@Test
void testMostWordsFound() {
String[] sentences = new String[]{"alice and bob love leetcode", "i think so too", "this is great thanks very much"};
assertEquals(6, LonguestSentence.mostWordsFound(sentences));
}
@Test
void testMostWordsFound() {
String[] sentences =
new String[] {
"alice and bob love leetcode", "i think so too", "this is great thanks very much"
};
assertEquals(6, LonguestSentence.mostWordsFound(sentences));
}
}
11 changes: 7 additions & 4 deletions src/test/java/com/TwinSumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
* @see TwinSum.pairSum
*/
public class TwinSumTest {
@Test
void testPairSum() {
assertEquals(4, TwinSum.pairSum(new TwinSum.ListNode(3, new TwinSum.ListNode(2, new TwinSum.ListNode(1, null)))));
}
@Test
void testPairSum() {
assertEquals(
4,
TwinSum.pairSum(
new TwinSum.ListNode(3, new TwinSum.ListNode(2, new TwinSum.ListNode(1, null)))));
}
}

0 comments on commit 7b35976

Please sign in to comment.