-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add:Solution] resolve daily problem of leetcode: Eliminate Max Monsters
- Loading branch information
Showing
11 changed files
with
198 additions
and
90 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
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
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
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
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,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; | ||
} | ||
} |
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
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
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
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,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})); | ||
} | ||
} |
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
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