Skip to content

Commit 52f9374

Browse files
author
Sonia Mathew
committed
more
1 parent 6ef798e commit 52f9374

16 files changed

+979
-227
lines changed

.idea/workspace.xml

+233-227
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DailyCoding16.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure
3+
* to accomplish this, with the following API:
4+
*
5+
* record(order_id): adds the order_id to the log
6+
* get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
7+
*/
8+
class LogDataStructure {
9+
private int[] circularBuffer;
10+
private int N;
11+
private int index;
12+
public LogDataStructure(int N) {
13+
this.N = N;
14+
this.circularBuffer = new int[N];
15+
this.index = 0;
16+
}
17+
public void record(int order_id) {
18+
circularBuffer[index] = order_id;
19+
index = (index + 1) % N;
20+
}
21+
public int get_last(int i) {
22+
int idx = (index - i + N) % N;
23+
return circularBuffer[idx];
24+
}
25+
}
26+
public class DailyCoding16 {
27+
public static void main(String[] args) {
28+
LogDataStructure log = new LogDataStructure(3);
29+
log.record(1);
30+
log.record(2);
31+
System.out.println("Last: " + log.get_last(2)); // 1
32+
log.record(3);
33+
log.record(4);
34+
System.out.println("Last: " + log.get_last(2)); // 3
35+
}
36+
}

src/DailyCoding268.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Given a 32-bit positive integer N, determine whether it is a power of four in faster than O(log N) time
3+
*/
4+
public class DailyCoding268 {
5+
public static void main(String[] args) {
6+
System.out.println(isPowerOfFour(16));
7+
System.out.println(isPowerOfFour(5));
8+
}
9+
public static boolean isPowerOfFour(int n) {
10+
//num & (num-1) == 0 will be true for powers of 2
11+
//num & 0x55555555 == num only for powers of 4, since powers of 4 have only one
12+
//1 and it is in an odd place.
13+
return n > 0 && (n & (n-1)) == 0 && ((n & 0x55555555) == n);
14+
}
15+
}

src/DailyCoding269.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* You are given an string representing the initial conditions of some dominoes. Each element can take one of three
3+
* values:
4+
*
5+
* L, meaning the domino has just been pushed to the left,
6+
* R, meaning the domino has just been pushed to the right, or
7+
* ., meaning the domino is standing still.
8+
* Determine the orientation of each tile when the dominoes stop falling. Note that if a domino receives a force
9+
* from the left and right side simultaneously, it will remain upright.
10+
*
11+
* For example, given the string .L.R....L, you should return LL.RRRLLL.
12+
*
13+
* Given the string ..R...L.L, you should return ..RR.LLLL.
14+
*/
15+
public class DailyCoding269 {
16+
public static void main(String[] args) {
17+
System.out.println(pushDominoes(".L.R...LR..L..").equals("LL.RR.LLRRLL.."));
18+
System.out.println(pushDominoes(".L.R....L").equals("LL.RRRLLL"));
19+
System.out.println(pushDominoes("..R...L.L").equals("..RR.LLLL"));
20+
}
21+
public static String pushDominoes(String dominoes) {
22+
char[] chars = dominoes.toCharArray();
23+
int n = chars.length;
24+
int i=0;
25+
while(i<n){
26+
//System.out.println(i);
27+
if(chars[i]=='R') {
28+
while(i<n && chars[i]=='R'){
29+
i++;
30+
}
31+
//System.out.println(i);
32+
if(i==n){
33+
break;
34+
}
35+
//find the next char
36+
int j = i;
37+
while(j<n && chars[j]=='.'){
38+
j++;
39+
}
40+
if(j==n) {
41+
//no L/R
42+
fill(chars, i, n, 'R');
43+
break;
44+
}
45+
if(chars[j]=='R') {
46+
// System.out.println(i);
47+
fill(chars, i, j, 'R');
48+
i=j;
49+
} else {
50+
int count = j-i;
51+
fill(chars, i, i+count/2, 'R');
52+
fill(chars, j-count/2, j, 'L');
53+
i = j+1;
54+
}
55+
56+
57+
} else if(chars[i]=='L'){
58+
int j = i-1;
59+
while(j >= 0 && chars[j]=='.'){
60+
j--;
61+
}
62+
if(j >=0 && chars[j]=='R'){
63+
i++;
64+
continue;
65+
}
66+
fill(chars, j+1, i, 'L');
67+
while(i<n && chars[i]=='L'){
68+
i++;
69+
}
70+
} else {
71+
i++;
72+
}
73+
}
74+
return new String(chars);
75+
}
76+
public static void fill(char[] chars, int start, int end, char c) {
77+
for(int i=start; i<end; i++) {
78+
chars[i] = c;
79+
}
80+
}
81+
}

src/DailyCoding270.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Deque;
2+
import java.util.LinkedList;
3+
4+
/**
5+
* A network consists of nodes labeled 0 to N. You are given a list of edges (a, b, t), describing the time t it takes
6+
* for a message to be sent from node a to node b. Whenever a node receives a message, it immediately passes the message
7+
* on to a neighboring node, if possible.
8+
*
9+
* Assuming all nodes are connected, determine how long it will take for every node to receive a message that begins
10+
* at node 0.
11+
*
12+
* For example, given N = 5, and the following edges:
13+
*
14+
* edges = [
15+
* (0, 1, 5),
16+
* (0, 2, 3),
17+
* (0, 5, 4),
18+
* (1, 3, 8),
19+
* (2, 3, 1),
20+
* (3, 5, 10),
21+
* (3, 4, 5)
22+
* ]
23+
* You should return 9, because propagating the message from 0 -> 2 -> 3 -> 4 will take that much time.
24+
*/
25+
class QEntry{
26+
int node;
27+
int dist;
28+
public QEntry(int node, int dist) {
29+
this.node = node;
30+
this.dist = dist;
31+
}
32+
}
33+
public class DailyCoding270 {
34+
public static void main(String[] args) {
35+
int[][] edges = {{0,1,5}, {0,2,3}, {0,5,4}, {1,3,8}, {2,3,1}, {3,5,10}, {3,4,5}};
36+
System.out.println(propagationTime(edges,5));
37+
38+
}
39+
public static int propagationTime(int[][] edges, int n) {
40+
Deque<QEntry> queue = new LinkedList<>();
41+
queue.push(new QEntry(0, 0));
42+
boolean[] visited = new boolean[n+1];
43+
int maxTime = 0;
44+
while(!queue.isEmpty()) {
45+
QEntry node = queue.poll();
46+
for (int i=0; i<edges.length; i++) {
47+
if (edges[i][0] == node.node && !visited[edges[i][1]]) {
48+
visited[edges[i][1]] = true;
49+
queue.push(new QEntry(edges[i][1], node.dist + edges[i][2]));
50+
//System.out.println(node.node + " " + edges[i][1] + " " + (node.dist+edges[i][2]));
51+
maxTime = Math.max(maxTime, node.dist+edges[i][2]);
52+
}
53+
}
54+
}
55+
return maxTime;
56+
}
57+
}

src/DailyCoding272.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import javax.rmi.CORBA.Util;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
/**
6+
* Write a function, throw_dice(N, faces, total), that determines how many ways it is possible to throw N dice with
7+
* some number of faces each to get a specific total.
8+
*
9+
* For example, throw_dice(3, 6, 7) should equal 15
10+
*/
11+
public class DailyCoding272 {
12+
public static void main(String[] args) {
13+
System.out.println(numDiceThrow(3, 6, 7)==15);
14+
System.out.println(numDiceThrowEfficient(3, 6, 7)==15);
15+
}
16+
public static int numDiceThrow(int numThrows, int faces, int total) {
17+
int[] count = new int[1];
18+
numDiceThrowHelper(numThrows, faces, total, new HashMap<>(), count, 0);
19+
return count[0];
20+
}
21+
public static void numDiceThrowHelper(int numThrows, int faces, int total, HashMap<Integer, Integer> map,
22+
int[] count, int currThrow) {
23+
if (total == 0 && currThrow == numThrows) {
24+
count[0] = count[0]+1;
25+
return;
26+
}
27+
if (currThrow == numThrows) {
28+
return;
29+
}
30+
for (int i=1; i<=faces; i++) {
31+
if (i <= total) {
32+
int c = map.getOrDefault(i, 0);
33+
map.put(i, c+1);
34+
numDiceThrowHelper(numThrows, faces, total-i, map, count, currThrow+1);
35+
c = map.get(i);
36+
c--;
37+
if (c==0) {
38+
map.remove(i);
39+
} else {
40+
map.put(i, c);
41+
}
42+
}
43+
}
44+
}
45+
public static int numDiceThrowEfficient(int numThrows, int faces, int total) {
46+
//DP approach
47+
/* Sum(m, n, X) = Finding Sum (X - 1) from (n - 1) dice plus 1 from nth dice
48+
+ Finding Sum (X - 2) from (n - 1) dice plus 2 from nth dice
49+
+ Finding Sum (X - 3) from (n - 1) dice plus 3 from nth dice
50+
...................................................
51+
...................................................
52+
...................................................
53+
+ Finding Sum (X - m) from (n - 1) dice plus m from nth dice */
54+
int[][] sums = new int[numThrows+1][total+1];
55+
for (int i=1; i<=faces; i++) {
56+
sums[1][i] = 1;
57+
}
58+
for (int i=2; i<=numThrows; i++) {
59+
for (int j=1; j<=total; j++) {
60+
for (int k=1;k<=faces; k++) {
61+
if (k <= j) {
62+
sums[i][j] += sums[i-1][j-k];
63+
}
64+
}
65+
}
66+
}
67+
return sums[numThrows][total];
68+
}
69+
}

src/DailyCoding273.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A fixed point in an array is an element whose value is equal to its index. Given a sorted array of distinct elements,
3+
* return a fixed point, if one exists. Otherwise, return -1.
4+
*
5+
* For example, given [-6, 0, 2, 40], you should return 2. Given [1, 5, 7, 8], you should return -1.
6+
*/
7+
public class DailyCoding273 {
8+
public static void main(String[] args) {
9+
System.out.println(findFixedPoint(new int[]{-6,0,2,40})==2);
10+
System.out.println(findFixedPoint(new int[]{1,5,7,8})==-1);
11+
}
12+
public static int findFixedPoint(int[] nums) {
13+
int start = 0, end = nums.length;
14+
while (start <= end) {
15+
int mid = (start+end)/2;
16+
if (nums[mid] == mid){
17+
return mid;
18+
} else if (nums[mid] < mid) {
19+
start = mid+1;
20+
} else {
21+
end = mid-1;
22+
}
23+
}
24+
return -1;
25+
}
26+
}

src/DiceRoll.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* A six-sided dice is a small cube with a different number of pips. Each face(side) ranging from 1 to 6.
3+
* On any two opposite side of the cube, the number of pips adds up to 7; that is, there are three pairs of opposite
4+
* sides: 1 and 6, 2 and 5, and 3 and 4.
5+
* There are N dice lying on a table, each showing the pips on its top face. In one move, you can take one dice and
6+
* rotate it to an adjacent face.
7+
* For example, you can rotate a dice that shows 1 to show 2, 3, 4 or 5. However, it cannot show 6 in a single move,
8+
* because the faces with one pip and six pips visible are opposite sides rather than adjacent.
9+
* You want to show the same number of pips on the top face of all N dice. Given that each of the dice can be moved
10+
* multiple times, count the minimum number of moves needed to get equal faces.
11+
*
12+
* Write a function:
13+
* that, given an array A consisting of N integers describing the number of pips (from 1 to 6) shown on each dice’s
14+
* top face, returns the minimum number of moves necessary for each dice show the same number of pips.
15+
*/
16+
public class DiceRoll {
17+
public static void main(String[] args) {
18+
System.out.println(minRolls(new int[]{1,2,3})==2);
19+
System.out.println(minRolls(new int[]{1,1,6})==2);
20+
System.out.println(minRolls(new int[]{1,6,2,3})==3);
21+
}
22+
public static int minRolls(int[] dices) {
23+
//since we know that the final number of pips would be one of 1-6, we need to find the number of rolls required
24+
//to reach each of these outcomes and then find the minimum among that
25+
int[] count = new int[7];
26+
for (int d : dices) {
27+
count[d]++;
28+
}
29+
int min = Integer.MAX_VALUE;
30+
for (int i=1; i<7; i++) {
31+
int num = 0;
32+
for (int j=1; j<7; j++) {
33+
if (j == 7-i) {
34+
num += 2*count[j]; //coz we have to flip twice
35+
} else if(j != i) {
36+
num += count[j];
37+
}
38+
}
39+
min = Math.min(min, num);
40+
}
41+
return min;
42+
}
43+
}
44+

src/DisjointSetTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class DisjointSet {
2+
int[] parent;
3+
int[] rank;
4+
public DisjointSet(int n){
5+
parent = new int[n];
6+
rank = new int[n];
7+
for (int i=0; i<parent.length; i++){
8+
parent[i] = i;
9+
rank[i] = 0;
10+
}
11+
}
12+
public void union(int x, int y) {
13+
if (parent[x] != parent[y]) {
14+
int xroot = find(x);
15+
int yroot = find(y);
16+
if (rank[xroot] < rank[yroot]) {
17+
parent[xroot] = yroot;
18+
} else if (rank[yroot] < rank[xroot]){
19+
parent[yroot] = xroot;
20+
} else {
21+
parent[yroot] = xroot;
22+
rank[xroot]++;
23+
}
24+
}
25+
}
26+
public int find(int y) {
27+
if (parent[y] == y) {
28+
return y;
29+
}
30+
parent[y] = find(parent[y]); // path compression, adding the current node directly under representative node
31+
return parent[y];
32+
}
33+
}
34+
public class DisjointSetTest {
35+
public static void main(String[] args) {
36+
DisjointSet ds = new DisjointSet(5);
37+
ds.union(1,2);
38+
ds.union(2,3);
39+
System.out.println(ds.find(1) == ds.find(3));
40+
}
41+
}

0 commit comments

Comments
 (0)