Skip to content

Commit b25e338

Browse files
committed
contest 33 and reservoir sampling for random index
1 parent 8b23247 commit b25e338

4 files changed

Lines changed: 196 additions & 5 deletions

File tree

‎src/algo/Solution.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package algo;
2+
3+
/**
4+
* Created by sherxon on 5/20/17.
5+
*/
6+
7+
class Solution {
8+
9+
static int findBusiestPeriod(int[][] data) {
10+
// your code goes here
11+
int max = 0;
12+
int maxTime = 0;
13+
14+
int counter = 0;
15+
16+
for (int i = 0; i < data.length; i++) {
17+
18+
if (data[i][2] == 0) {
19+
counter -= data[i][1];
20+
} else {
21+
counter += data[i][1];
22+
}
23+
24+
if (i < data.length - 1 && data[i][0] == data[i + 1][0]) {
25+
continue;
26+
}
27+
28+
if (max < counter) {
29+
30+
max = counter;
31+
maxTime = data[i][0];
32+
33+
}
34+
if (i < data.length - 1 && data[i][0] != data[i + 1][0]) {
35+
counter = 0;
36+
}
37+
}
38+
39+
return maxTime;
40+
41+
}
42+
43+
public static void main(String[] args) {
44+
System.out.println(findBusiestPeriod(new int[][]{
45+
{1487799425, 14, 1},
46+
{1487799425, 4, 0},
47+
{1487799425, 2, 0},
48+
{1487800378, 10, 1},
49+
{1487801478, 18, 0},
50+
{1487801478, 18, 1},
51+
{1487901013, 1, 0},
52+
{1487901211, 7, 1},
53+
{1487901211, 7, 0}
54+
}));
55+
}
56+
}
57+

‎src/problems/easy/FileSystem.java‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package problems.easy;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Created by sherxon on 5/20/17.
10+
*/
11+
public class FileSystem {
12+
13+
Node root;
14+
15+
public FileSystem() {
16+
root = new Node("");
17+
}
18+
19+
public List<String> ls(String path) {
20+
List<String> list = new ArrayList<>();
21+
ls(path.split("/"), 0, list, root);
22+
return list;
23+
}
24+
25+
private void ls(String[] path, int i, List<String> list, Node root) {
26+
if (root == null) {
27+
return;
28+
}
29+
if (i == path.length - 1) {
30+
list.addAll(root.children.keySet());
31+
return;
32+
}
33+
34+
ls(path, i + 1, list, root.children.get(path[i + 1]));
35+
}
36+
37+
public void mkdir(String path) {
38+
mkdir(path.split("/"), root, 1);
39+
}
40+
41+
private void mkdir(String[] split, Node root, int i) {
42+
if (i == split.length - 1) {
43+
return;
44+
}
45+
if (!root.children.containsKey(split[i])) {
46+
root.children.put(split[i], new Node(split[i]));
47+
}
48+
mkdir(split, root.children.get(split[i]), i + 1);
49+
}
50+
51+
public void addContentToFile(String filePath, String content) {
52+
53+
}
54+
55+
56+
private class Node {
57+
58+
String name;
59+
Map<String, Node> children = new HashMap<>();
60+
String content;
61+
62+
public Node(String n) {
63+
this.name = n;
64+
}
65+
}
66+
67+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package problems.easy;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
6+
/**
7+
* Created by sherxon on 5/20/17.
8+
*/
9+
public class LongestHarmoniousSubsequence {
10+
11+
public static void main(String[] args) {
12+
System.out.println(findLHS(new int[]{1, 1, 1, 1}));
13+
}
14+
15+
static public int findLHS(int[] nums) {
16+
if (nums == null || nums.length == 0) {
17+
return 0;
18+
}
19+
Arrays.sort(nums);
20+
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
21+
for (int i = 0; i < nums.length; i++) {
22+
if (!map.containsKey(nums[i])) {
23+
map.put(nums[i], 1);
24+
} else {
25+
map.put(nums[i], map.get(nums[i]) + 1);
26+
}
27+
}
28+
Integer prev = null;
29+
int max = 0;
30+
for (Integer key : map.keySet()) {
31+
if (prev != null && (key - 1 == prev || key + 1 == prev)) {
32+
max = Math.max(map.get(prev) + map.get(key), max);
33+
}
34+
prev = key;
35+
}
36+
37+
return max;
38+
}
39+
40+
}

‎src/problems/medium/RandomPickIndex.java‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package problems.medium;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
3+
import java.util.Random;
74

85
/**
96
* Created by sherxon on 1/16/17.
107
*/
118
public class RandomPickIndex {
12-
Map<Integer, List<Integer>> map = new HashMap<>();
9+
/*Map<Integer, List<Integer>> map = new HashMap<>();
1310
1411
public RandomPickIndex(int[] nums) {
1512
for (int i = 0; i < nums.length; i++) {
@@ -30,5 +27,35 @@ public static void main(String[] args) {
3027
public int pick(int target) {
3128
int random = (int) (Math.random() * map.get(target).size());
3229
return map.get(target).get(random);
30+
}*/
31+
32+
/**
33+
* This is reservoir sample solution
34+
*/
35+
Random random;
36+
int[] nums;
37+
38+
public RandomPickIndex(int[] nums) {
39+
this.nums = nums;
40+
this.random = new Random();
3341
}
42+
43+
public int pick(int target) {
44+
int count = 0;
45+
int result = -1;
46+
for (int i = 0; i < nums.length; i++) {
47+
48+
if (nums[i] != target) {
49+
continue;
50+
}
51+
52+
if (random.nextInt(++count) == 0) {
53+
result = i;
54+
}
55+
56+
}
57+
return result;
58+
}
59+
60+
3461
}

0 commit comments

Comments
 (0)