codewars045: Twice linear

Instructions:

Twice linearjava

Solution:

import java.util.*;
import java.util.Map.Entry;

class DoubleLinear {
	private static Map<Integer, Boolean> map = new TreeMap<Integer, Boolean>();
	private static int[] arr = new int[100000];
	static {
		map.put(1, false);

		int count = 0;
		while (count < 100000) {
			for (Entry<Integer, Boolean> x : map.entrySet()) {
				if (!x.getValue()) {
					map.put(x.getKey() * 2 + 1, false);
					map.put(x.getKey() * 3 + 1, false);
					arr[count++] = x.getKey();
					map.remove(x.getKey());
					break;
				}
			}
		}
		Arrays.sort(arr);
	}

	public static int dblLinear(int n) {

		return arr[n];
	}
}

Example Test:

import static org.junit.Assert.*;
import org.junit.Test;


public class DoubleLinearTest {

    private static void testing(int actual, int expected) {
        assertEquals(expected, actual);
    }
 
    @Test
    public void test() {
        System.out.println("Fixed Tests dblLinear");
        testing(DoubleLinear.dblLinear(10), 22);
        testing(DoubleLinear.dblLinear(20), 57);
        testing(DoubleLinear.dblLinear(30), 91);
        testing(DoubleLinear.dblLinear(50), 175);
             
    }
}
相關文章
相關標籤/搜索