From 7b35976a9bfd69f065ede66aebddd6274116afc7 Mon Sep 17 00:00:00 2001 From: KOTBI Abderrahmane Date: Sat, 11 Nov 2023 20:10:29 +0100 Subject: [PATCH] [Add:Solution] resolve daily problem of leetcode: Eliminate Max Monsters --- src/main/java/com/AlmostEquivalentString.java | 17 +++-- src/main/java/com/CapitilizedString.java | 48 +++++++------ src/main/java/com/ColoredPiecesGame.java | 9 ++- src/main/java/com/DistinctString.java | 16 +++-- src/main/java/com/EliminateMaxMonster.java | 45 ++++++++++++ src/main/java/com/LonguestSentence.java | 18 +++-- src/main/java/com/TwinSum.java | 69 +++++++++---------- src/test/java/com/CapitilizedStringTest.java | 8 +-- .../java/com/EliminateMaxMonsterTest.java | 34 +++++++++ src/test/java/com/LonguestSentenceTest.java | 13 ++-- src/test/java/com/TwinSumTest.java | 11 +-- 11 files changed, 198 insertions(+), 90 deletions(-) create mode 100644 src/main/java/com/EliminateMaxMonster.java create mode 100644 src/test/java/com/EliminateMaxMonsterTest.java diff --git a/src/main/java/com/AlmostEquivalentString.java b/src/main/java/com/AlmostEquivalentString.java index 2f9ee34..9d43cbe 100644 --- a/src/main/java/com/AlmostEquivalentString.java +++ b/src/main/java/com/AlmostEquivalentString.java @@ -1,15 +1,20 @@ package com; /** + * Snowflake + * * @author KOTBI Abderrahmane * @version 1.1 + * @see Leetcode-2068. + * Check Whether Two Strings are Almost Equivalent */ 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; @@ -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. */ diff --git a/src/main/java/com/CapitilizedString.java b/src/main/java/com/CapitilizedString.java index b3f925b..4f18b52 100644 --- a/src/main/java/com/CapitilizedString.java +++ b/src/main/java/com/CapitilizedString.java @@ -4,29 +4,37 @@ import java.util.List; /** + * Snowflake + * * @author KOTBI Abderrahmane * @version 1.1 + * @see Leetcode-2129. Capitalize + * the Title */ 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 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 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; } -} \ No newline at end of file + return o.trim(); + } +} diff --git a/src/main/java/com/ColoredPiecesGame.java b/src/main/java/com/ColoredPiecesGame.java index df2deac..da00dea 100644 --- a/src/main/java/com/ColoredPiecesGame.java +++ b/src/main/java/com/ColoredPiecesGame.java @@ -1,14 +1,19 @@ package com; /** + * Cyclone + * * @author KOTBI Abderrahmane * @version 1.1 + * @see Leetcode-2038. + * Remove Colored Pieces if Both Neighbors are the Same Color */ 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; diff --git a/src/main/java/com/DistinctString.java b/src/main/java/com/DistinctString.java index 9481852..f70f136 100644 --- a/src/main/java/com/DistinctString.java +++ b/src/main/java/com/DistinctString.java @@ -1,15 +1,19 @@ package com; /** + * Cyclone + * * @author KOTBI Abderrahmane * @version 1.1 + * @see Leetcode-2053. + * Kth Distinct String in an Array */ 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; @@ -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; diff --git a/src/main/java/com/EliminateMaxMonster.java b/src/main/java/com/EliminateMaxMonster.java new file mode 100644 index 0000000..b493863 --- /dev/null +++ b/src/main/java/com/EliminateMaxMonster.java @@ -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 Leetcode-1921. + * Eliminate Maximum Number of Monsters + */ +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 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; + } +} diff --git a/src/main/java/com/LonguestSentence.java b/src/main/java/com/LonguestSentence.java index 300e197..1a2f402 100644 --- a/src/main/java/com/LonguestSentence.java +++ b/src/main/java/com/LonguestSentence.java @@ -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; + } } diff --git a/src/main/java/com/TwinSum.java b/src/main/java/com/TwinSum.java index 0521d4e..a426290 100644 --- a/src/main/java/com/TwinSum.java +++ b/src/main/java/com/TwinSum.java @@ -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 values = new ArrayList(); - 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 values = new ArrayList(); + 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; + } } diff --git a/src/test/java/com/CapitilizedStringTest.java b/src/test/java/com/CapitilizedStringTest.java index 2c2cae9..3c0608f 100644 --- a/src/test/java/com/CapitilizedStringTest.java +++ b/src/test/java/com/CapitilizedStringTest.java @@ -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")); + } } diff --git a/src/test/java/com/EliminateMaxMonsterTest.java b/src/test/java/com/EliminateMaxMonsterTest.java new file mode 100644 index 0000000..d6717c9 --- /dev/null +++ b/src/test/java/com/EliminateMaxMonsterTest.java @@ -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})); + } +} diff --git a/src/test/java/com/LonguestSentenceTest.java b/src/test/java/com/LonguestSentenceTest.java index d4f1733..2501036 100644 --- a/src/test/java/com/LonguestSentenceTest.java +++ b/src/test/java/com/LonguestSentenceTest.java @@ -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)); + } } diff --git a/src/test/java/com/TwinSumTest.java b/src/test/java/com/TwinSumTest.java index 6069e1e..07a6f32 100644 --- a/src/test/java/com/TwinSumTest.java +++ b/src/test/java/com/TwinSumTest.java @@ -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))))); + } }