class Solution { public static void main(String[] args) { System.out.println("What is next?"); } }
- class Solution {
- public static void main(String[] args) {
System.out.println("Hello darkness, my old friend...");- System.out.println("What is next?");
- }
- }
import static org.junit.Assert.*; import org.junit.Test; public class TestT { @Test public void test1() { assertEquals(1, 1); } }
- import static org.junit.Assert.*;
- import org.junit.Test;
- public class TestT {
- @Test
- public void test1() {
- assertEquals(1, 1);
- }
- }
import java.util.*; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; /** Time Complexity : O(N) Space Complexity : O(N) */ class MaxOccurence { public static int findMax(int[] nums) { Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums) .boxed() .collect(Collectors.groupingBy(num -> num)) .entrySet() .stream() .max((e1, e2) -> e1.getValue().size() - e2.getValue().size()); return max.isPresent() ? max.get().getKey() : -1; } }
- import java.util.*;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- /**
- Time Complexity : O(N)
- Space Complexity : O(N)
- */
- class MaxOccurence {
- public static int findMax(int[] nums) {
return findMaxOccurenceLinkedHashMap(nums);- Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
- .boxed()
- .collect(Collectors.groupingBy(num -> num))
- .entrySet()
- .stream()
- .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
- return max.isPresent() ? max.get().getKey() : -1;
- }
private static int findMaxOccurenceLinkedHashMap(int[] nums) {int maxKey = 0, maxNum = 0;if(nums.length < 1) return -1;if(nums.length == 1) return nums[0];Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>(nums.length);for(int i = 0, len = nums.length; i < len; i++) {if(!counts.containsKey(nums[i])) {counts.put(nums[i], 1);} else {counts.put(nums[i], (counts.get(nums[i]) + 1));}}for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {if (entry.getValue() > maxNum) {maxKey = entry.getKey();maxNum = entry.getValue();}}return maxKey;}- }
import java.util.*; import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; public class MaxOccurenceTest { @Test public void test1() { int[] nums = {1, 2, 9, 3, 4, 3, 3, 1, 2, 4, 5, 3, 8, 3, 9, 0, 3, 2}; assertEquals(3, MaxOccurence.findMax(nums)); } @Ignore @Test public void test2() { int[] nums = {5, 4, 6 ,7, 9, 8, 2, 4, 3, 6, 5, 8, 6, 1, 2, 5, 3, 4, 7, 9, 0, 8, 5, 7, 3, 4, 6, 1, 3}; // there is no instruction what of the numbers should be printed first if there are many max numbers in the row assertEquals(5, MaxOccurence.findMax(nums)); } @Test public void test3() { int[] nums = {8}; assertEquals(8, MaxOccurence.findMax(nums)); } @Test public void test4() { int[] nums = {}; assertEquals(-1, MaxOccurence.findMax(nums)); } @Test public void test5() { int[] nums = {2, 2, 3, 3, 4, 4, 5, 5, 6, 6}; assertEquals(2, MaxOccurence.findMax(nums)); } @Test public void test6() { int[] nums = {2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}; assertEquals(5, MaxOccurence.findMax(nums)); } }
- import java.util.*;
- import org.junit.Ignore;
- import org.junit.Test;
- import static org.junit.Assert.*;
- public class MaxOccurenceTest {
- @Test
- public void test1() {
- int[] nums = {1, 2, 9, 3, 4, 3, 3, 1, 2, 4, 5, 3, 8, 3, 9, 0, 3, 2};
- assertEquals(3, MaxOccurence.findMax(nums));
- }
- @Ignore
- @Test
- public void test2() {
- int[] nums = {5, 4, 6 ,7, 9, 8, 2, 4, 3, 6, 5, 8, 6, 1, 2, 5, 3, 4, 7, 9, 0, 8, 5, 7, 3, 4, 6, 1, 3};
- // there is no instruction what of the numbers should be printed first if there are many max numbers in the row
- assertEquals(5, MaxOccurence.findMax(nums));
- }
- @Test
- public void test3() {
- int[] nums = {8};
- assertEquals(8, MaxOccurence.findMax(nums));
- }
- @Test
- public void test4() {
- int[] nums = {};
- assertEquals(-1, MaxOccurence.findMax(nums));
- }
- @Test
- public void test5() {
- int[] nums = {2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
- assertEquals(2, MaxOccurence.findMax(nums));
- }
- @Test
- public void test6() {
- int[] nums = {2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
- assertEquals(5, MaxOccurence.findMax(nums));
- }
- }
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Primes { public static boolean isPrime(int n) { if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } public static List<Integer> generatePrimes(int min, int max) { if (!(min > 0 && max > 0)) return null; return IntStream.range(min, max) .filter(e -> isPrime(e)) .boxed() .collect(Collectors.toList()); } }
import java.util.*;- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class Primes {
public static List<Integer> generatePrimes(int min, int max) {List<Integer> primes = new ArrayList<Integer>();boolean isPrime = false;if((min > max) || min < 0 || max <= 0) {return null;}for(int i = min; i <= max; i++) {long endLimit = (long)Math.floor(Math.sqrt(i));isPrime = true;for(long j = 2; j <= endLimit; j++) {if (i % j == 0) {isPrime = false;break;- public static boolean isPrime(int n) {
- if (n == 2)
- return true;
- if (n % 2 == 0) return false;
- for (int i = 3; i * i <= n; i += 2) {
- if (n % i == 0)
- return false;
- }
}if(isPrime) {primes.add(i);}- return true;
- }
return primes;}- public static List<Integer> generatePrimes(int min, int max) {
- if (!(min > 0 && max > 0))
- return null;
- return IntStream.range(min, max)
- .filter(e -> isPrime(e))
- .boxed()
- .collect(Collectors.toList());
- }
- }
Hashes
Data Structures
/* HashMap Example */ import java.util.*; import java.lang.*; import java.io.*; class HashMapDemo { public static void main (String[] args) throws java.lang.Exception { Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{ put(1, 1); put(2, 1); put(3, 1); put(4, 1); put(5, 1); put(6, 1); }}; map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue())); } }
- /* HashMap Example */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class HashMapDemo
- {
public static void main (String[] args) throws java.lang.Exception{Map<Integer, Integer> map = new HashMap<Integer, Integer>();map.put(1, 1);map.put(2, 1);map.put(3, 1);map.put(4, 1);map.put(5, 1);map.put(6, 1);for(Map.Entry<Integer, Integer> entry : map.entrySet()) {int key = entry.getKey();int value = entry.getValue();System.out.println(key + " " + value);}}- public static void main (String[] args) throws java.lang.Exception {
- Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
- put(1, 1);
- put(2, 1);
- put(3, 1);
- put(4, 1);
- put(5, 1);
- put(6, 1);
- }};
- map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.*; public class MockTest { @Test public void test() { assertEquals(1, 1); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.util.*;
- public class MockTest {
- @Test
- public void test() {
- assertEquals(1, 1);
- }
- }