Skip to content

Commit d03f20f

Browse files
BasicCalculator and PourWater: Accepted
1 parent be81d55 commit d03f20f

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ My accepted leetcode solutions to some of the common interview problems.
3131
- [Increasing Triplet Subsequence](problems/src/array/IncreasingTripletSubsequence.java) (Medium)
3232
- [K Empty Slots](problems/src/array/KEmptySlots.java) (Hard)
3333
- [Subarray Sum Equals K](problems/src/array/SubarraySumEqualsK.java) (Medium)
34+
- [Pour Water](problems/src/array/PourWater.java) (Medium)
3435

3536
#### [Backtracking](problems/src/backtracking)
3637

@@ -217,6 +218,7 @@ My accepted leetcode solutions to some of the common interview problems.
217218
- [Implement Queue using Stacks](problems/src/stack/MyQueue.java) (Easy)
218219
- [Maximal Rectangle](problems/src/stack/MaximalRectangle.java) (Hard)
219220
- [Exclusive Time of Functions](problems/src/stack/ExclusiveTimeOfFunctions.java) (Medium)
221+
- [Basic Calculator](problems/src/stack/BasicCalculator.java) (Hard)
220222

221223

222224
#### [String](problems/src/string)

problems/src/array/PourWater.java

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 03/02/2018.
5+
* We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each
6+
* index is 1. After V units of water fall at index K, how much water is at each index?
7+
8+
Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows
9+
according to the following rules:
10+
11+
If the droplet would eventually fall by moving left, then move left.
12+
Otherwise, if the droplet would eventually fall by moving right, then move right.
13+
Otherwise, rise at it's current position.
14+
Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction.
15+
Also, "level" means the height of the terrain plus any water in that column.
16+
We can assume there's infinitely high terrain on the two sides out of bounds of the array. Also, there could not be
17+
partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.
18+
19+
Example 1:
20+
Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
21+
Output: [2,2,2,3,2,2,2]
22+
Explanation:
23+
# #
24+
# #
25+
## # ###
26+
#########
27+
0123456 <- index
28+
29+
The first drop of water lands at index K = 3:
30+
31+
# #
32+
# w #
33+
## # ###
34+
#########
35+
0123456
36+
37+
When moving left or right, the water can only move to the same level or a lower level.
38+
(By level, we mean the total height of the terrain plus any water in that column.)
39+
Since moving left will eventually make it fall, it moves left.
40+
(A droplet "made to fall" means go to a lower height than it was at previously.)
41+
42+
# #
43+
# #
44+
## w# ###
45+
#########
46+
0123456
47+
48+
Since moving left will not make it fall, it stays in place. The next droplet falls:
49+
50+
# #
51+
# w #
52+
## w# ###
53+
#########
54+
0123456
55+
56+
Since the new droplet moving left will eventually make it fall, it moves left.
57+
Notice that the droplet still preferred to move left,
58+
even though it could move right (and moving right makes it fall quicker.)
59+
60+
# #
61+
# w #
62+
## w# ###
63+
#########
64+
0123456
65+
66+
# #
67+
# #
68+
##ww# ###
69+
#########
70+
0123456
71+
72+
After those steps, the third droplet falls.
73+
Since moving left would not eventually make it fall, it tries to move right.
74+
Since moving right would eventually make it fall, it moves right.
75+
76+
# #
77+
# w #
78+
##ww# ###
79+
#########
80+
0123456
81+
82+
# #
83+
# #
84+
##ww#w###
85+
#########
86+
0123456
87+
88+
Finally, the fourth droplet falls.
89+
Since moving left would not eventually make it fall, it tries to move right.
90+
Since moving right would not eventually make it fall, it stays in place:
91+
92+
# #
93+
# w #
94+
##ww#w###
95+
#########
96+
0123456
97+
98+
The final answer is [2,2,2,3,2,2,2]:
99+
100+
#
101+
#######
102+
#######
103+
0123456
104+
Example 2:
105+
Input: heights = [1,2,3,4], V = 2, K = 2
106+
Output: [2,3,3,4]
107+
Explanation:
108+
The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower
109+
height.
110+
Example 3:
111+
Input: heights = [3,1,3], V = 5, K = 1
112+
Output: [4,4,4]
113+
Note:
114+
115+
heights will have length in [1, 100] and contain integers in [0, 99].
116+
V will be in range [0, 2000].
117+
K will be in range [0, heights.length - 1].
118+
119+
Solution:
120+
Check first left and then right to see if there are any lower levels, if yes then drop the water at this point. Else
121+
maintain the drop at the start position
122+
*/
123+
public class PourWater {
124+
125+
/**
126+
* Main method
127+
* @param args
128+
* @throws Exception
129+
*/
130+
public static void main(String[] args) throws Exception{
131+
int[] A = {2, 1, 1, 2, 1, 2, 2};
132+
int[] result = new PourWater().pourWater(A, 4, 3);
133+
for(int i : result){
134+
System.out.print(i + " ");
135+
}
136+
}
137+
138+
public int[] pourWater(int[] heights, int V, int K) {
139+
while(V-- > 0){
140+
heights[K] += 1;
141+
int index = K;
142+
int min = heights[K];
143+
for(int i = K - 1; i >= 0; i--){
144+
if(heights[i] + 1 > min){
145+
break;
146+
} else if(heights[i] + 1 < min){
147+
min = heights[i] + 1;
148+
index = i;
149+
}
150+
}
151+
if(index == K){
152+
for(int i = K + 1; i < heights.length; i++){
153+
if(heights[i] + 1 > min){
154+
break;
155+
} else if(heights[i] + 1 < min){
156+
min = heights[i] + 1;
157+
index = i;
158+
}
159+
}
160+
}
161+
if(index != K){
162+
heights[K]--;
163+
heights[index]++;
164+
}
165+
}
166+
return heights;
167+
}
168+
169+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 06/02/2018.
7+
* Implement a basic calculator to evaluate a simple expression string.
8+
9+
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative
10+
integers and empty spaces .
11+
12+
You may assume that the given expression is always valid.
13+
14+
Some examples:
15+
"1 + 1" = 2
16+
" 2-1 + 2 " = 3
17+
"(1+(4+5+2)-3)+(6+8)" = 23
18+
Note: Do not use the eval built-in library function.
19+
20+
Solution: O(n) where n is the length of the string.
21+
Maintain a stack and push each character from the string (ignore space). As soon as a close parentheses ')'
22+
is encountered, start to pop values and sum-up the total until '(' is poped. Push the total back to stack
23+
and continue to iterate. The final result will be in the top of the stack which is the last
24+
and only element in stack.
25+
*/
26+
public class BasicCalculator {
27+
28+
/**
29+
* Main method
30+
* @param args
31+
* @throws Exception
32+
*/
33+
public static void main(String[] args) throws Exception{
34+
System.out.println(new BasicCalculator().calculate("2-1 + (2 - 3) - ((2 - (2 - (3 - (4 - 5)))))"));
35+
}
36+
37+
public int calculate(String s) {
38+
Stack<String> stack = new Stack<>();
39+
String num = "";
40+
s = "(" + s + ")";
41+
for(char c : s.toCharArray()){
42+
switch (c){
43+
case ' ':
44+
case '(':
45+
case '+':
46+
case '-':
47+
if(!num.equals("")){
48+
stack.push(String.valueOf(num));
49+
num = "";
50+
}
51+
if(c != ' '){ //ignore blank
52+
stack.push(String.valueOf(c));
53+
}
54+
break;
55+
case ')':
56+
if(!num.equals("")){
57+
stack.push(String.valueOf(num));
58+
num = "";
59+
}
60+
int sum = 0;
61+
int prev = 0; //maintain a prev value inorder to handle minus '-'
62+
while(!stack.isEmpty()){
63+
String top = stack.pop();
64+
if(top.equals("-")){
65+
sum -= (prev * 2);
66+
prev = 0;
67+
} else if(top.equals("+")){
68+
//ignore
69+
} else if(top.equals("(")){
70+
stack.push(String.valueOf(sum));
71+
break;
72+
} else{
73+
sum += Integer.parseInt(top);
74+
prev = Integer.parseInt(top);
75+
}
76+
}
77+
break;
78+
default:
79+
num += String.valueOf(c);
80+
break;
81+
}
82+
}
83+
return Integer.parseInt(stack.peek());
84+
}
85+
}

0 commit comments

Comments
 (0)