Skip to content

Commit 03551d1

Browse files
author
Tushar Roy
committed
Some changes
1 parent 259077b commit 03551d1

File tree

5 files changed

+132
-29
lines changed

5 files changed

+132
-29
lines changed

src/com/interview/array/MaxProductSubarray.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,34 @@
1515
public class MaxProductSubarray {
1616

1717
public int maxProduct(int[] nums) {
18-
int min = 1;
19-
int max = 1;
20-
int maxSoFar = nums[0];
18+
//neg maintains the multiplication which is negative since last 0
19+
//pos maintains the multiplication which is positive since last 0
20+
int neg = 1;
21+
int pos = 1;
22+
int maxProduct = nums[0];
2123
for (int i = 0; i < nums.length; i++) {
22-
if (nums[i] > 0) {
23-
max = max * nums[i];
24-
min = Math.min(min * nums[i], 1);
25-
maxSoFar = Math.max(maxSoFar, max);
26-
} else if (nums[i] == 0) {
27-
min = 1;
28-
max = 1;
29-
maxSoFar = Math.max(maxSoFar, 0);
24+
if (nums[i] == 0) {
25+
neg = 1;
26+
pos = 1;
27+
maxProduct = Math.max(maxProduct, 0);
28+
} else if (nums[i] < 0) {
29+
int temp = pos;
30+
if (neg < 0) {
31+
pos = neg * nums[i];
32+
maxProduct = Math.max(pos, maxProduct);
33+
} else {
34+
pos = 1;
35+
}
36+
neg = temp * nums[i];
3037
} else {
31-
int t = max * nums[i];
32-
maxSoFar = Math.max(maxSoFar, min * nums[i]);
33-
max = Math.max(1, min*nums[i]);
34-
min = t;
38+
if (neg < 0) {
39+
neg *= nums[i];
40+
}
41+
pos *= nums[i];
42+
maxProduct = Math.max(pos, maxProduct);
3543
}
3644
}
37-
return maxSoFar;
45+
return maxProduct;
3846
}
3947

4048
public static void main(String args[]){

src/com/interview/array/MultiplyAllFieldsExceptOwnPosition.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.interview.array;
22

33
/**
4-
* http://www.careercup.com/question?id=5179916190482432
5-
* Look out for division by 0
4+
* https://leetcode.com/problems/product-of-array-except-self/
65
*/
76
public class MultiplyAllFieldsExceptOwnPosition {
87

@@ -11,17 +10,15 @@ public int[] multiply(int nums[]) {
1110
return new int[0];
1211
}
1312
int[] output = new int[nums.length];
14-
int product = nums[0];
13+
output[0] = 1;
1514
for (int i = 1; i < nums.length; i++) {
16-
output[i] = product;
17-
product *= nums[i];
15+
output[i] = output[i - 1] * nums[i - 1];
1816
}
1917

20-
output[0] = 1;
21-
product = nums[nums.length - 1];
22-
for (int i = nums.length - 2; i >= 0; i--) {
23-
output[i] *= product;
24-
product *= nums[i];
18+
int mult = 1;
19+
for (int i = nums.length - 1; i >= 0; i--) {
20+
output[i] *= mult;
21+
mult *= nums[i];
2522
}
2623
return output;
2724
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.interview.binarysearch;
22

33
/**
4-
* Created by tushar_v_roy on 3/21/16.
4+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
55
*/
66
public class MinimumInSortedRotatedArray {
77
public int findMin(int[] nums) {
88
int low = 0;
99
int high = nums.length - 1;
1010
while (low < high) {
1111
int middle = (low + high)/2;
12-
if ((middle == 0 || nums[middle] < nums[middle - 1]) && (middle == nums.length - 1 || nums[middle] < nums[middle + 1])) {
12+
if ((middle == 0 && nums[middle] < nums[middle + 1]) || (middle > 0 && nums[middle] < nums[middle - 1])) {
1313
return nums[middle];
1414
}
1515
else if (nums[middle] > nums[high]) {
@@ -18,7 +18,6 @@ else if (nums[middle] > nums[high]) {
1818
high = middle - 1;
1919
}
2020
}
21-
2221
return nums[low];
2322
}
2423
}

src/com/interview/multithreaded/DependencyTaskExecutor.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.Executor;
1111
import java.util.concurrent.ExecutorService;
1212
import java.util.concurrent.Executors;
13+
import java.util.concurrent.RunnableFuture;
1314

1415
/**
1516
* Given a Task with list of its dependencies and execute method. Run the task such that dependencies are executed first.
@@ -106,4 +107,32 @@ public void execute() {
106107
e.printStackTrace();
107108
}
108109
}
110+
}
111+
112+
class FutureTask implements Runnable {
113+
114+
Task task;
115+
List<FutureTask> chainedTasks = new ArrayList<>();
116+
Executor executor;
117+
FutureTask(Task task, Executor executor) {
118+
this.task = task;
119+
this.executor = executor;
120+
}
121+
@Override
122+
public void run() {
123+
task.execute();
124+
for (FutureTask t : chainedTasks) {
125+
supplyAsync(t, executor);
126+
}
127+
}
128+
129+
void supplyAsync(FutureTask task, Executor executor) {
130+
executor.execute(task);
131+
}
132+
133+
void addChain(FutureTask task) {
134+
task.addChain(this);
135+
}
136+
137+
109138
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.interview.recursion;
2+
3+
/**
4+
* Date 05/09/2017
5+
* @author Tushar Roy
6+
*
7+
* https://leetcode.com/problems/optimal-division/#/description
8+
*/
9+
public class OptimalDivision {
10+
public String optimalDivision(int[] nums) {
11+
Result r = optimalDivison(nums, 0, nums.length - 1, true);
12+
System.out.println(r.val);
13+
return r.str;
14+
}
15+
16+
private Result optimalDivison(int[] nums, int start, int end, boolean maximize) {
17+
if (start == end) {
18+
return new Result(nums[start], String.valueOf(nums[start]));
19+
}
20+
21+
double maxResult = 0;
22+
double minResult = Double.MAX_VALUE;
23+
String result = "";
24+
int cutI = start;
25+
String part1 = "";
26+
String part2 = "";
27+
for (int i = start; i < end; i++) {
28+
Result d1 = optimalDivison(nums, start, i, maximize);
29+
Result d2 = optimalDivison(nums, i + 1, end, !maximize);
30+
double val = d1.val / d2.val;
31+
if (maximize) {
32+
if (maxResult < val) {
33+
maxResult = val;
34+
part1 = d1.str;
35+
part2 = d2.str;
36+
cutI = i;
37+
}
38+
} else {
39+
if (minResult > val) {
40+
minResult = val;
41+
part1 = d1.str;
42+
part2 = d2.str;
43+
cutI = i;
44+
}
45+
}
46+
}
47+
if (cutI < end - 1) {
48+
result = part1 + "/(" + part2 + ")";
49+
} else {
50+
result = part1 + "/" + part2;
51+
}
52+
return maximize ? new Result(maxResult, result) : new Result(minResult, result);
53+
}
54+
55+
class Result {
56+
double val;
57+
String str;
58+
Result(double val, String str) {
59+
this.val = val;
60+
this.str = str;
61+
}
62+
}
63+
64+
public static void main(String args[]) {
65+
// int[] nums = {1000, 100, 10, 2};
66+
int[] nums = {6,2,3,4,5};
67+
OptimalDivision od = new OptimalDivision();
68+
System.out.println(od.optimalDivision(nums));
69+
}
70+
}

0 commit comments

Comments
 (0)