Skip to content

Instantly share code, notes, and snippets.

@taixingbi
Last active June 11, 2019 01:22
Show Gist options
  • Save taixingbi/fb07b5daf07f1aec21cd0f9e0b3b9d3e to your computer and use it in GitHub Desktop.
Save taixingbi/fb07b5daf07f1aec21cd0f9e0b3b9d3e to your computer and use it in GitHub Desktop.
772. Basic Calculator III
https://leetcode.com/problems/basic-calculator-iii/
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
The expression string contains only non-negative integers, +, -, *, / operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].
Some examples:
"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-12
class Solution {
public int calculate(String s) {
if(s==null || s.length()==0) return 0;
Stack<Integer> stack= new Stack<>();
int num= 0;
char sign= '+';
for(int i=0; i<s.length(); i++) {
if(Character.isDigit( s.charAt(i))) {
num = num*10 + s.charAt(i) - '0';
}
if(s.charAt(i)=='(') {
int st= i;
for(int cnt=0; i<s.length(); i++) {
if( s.charAt(i)=='(' ) cnt++;
if( s.charAt(i)==')' ) cnt--;
if(cnt==0 ) break;
}
num= calculate( s.substring(st+1, i)) ;
}
if( s.charAt(i)=='+' || s.charAt(i)=='-' || s.charAt(i)=='*' || s.charAt(i)=='/' || i==s.length()-1) {
if(sign=='+') stack.push(num);
if(sign=='-') stack.push(-num);
if(sign=='*') stack.push( stack.pop()*num );
if(sign=='/') stack.push( stack.pop()/num );
sign= s.charAt(i);
num= 0;
}
}
int sum= 0;
for(int e: stack)
sum += e;
return sum;
}
}
465. Optimal Account Balancing
https://leetcode.com/problems/optimal-account-balancing/
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for Bill's lunch for $10. Then later Chris gave Alice $5 for a taxi ride. We can model each transaction as a tuple (x, y, z) which means person x gave person y $z. Assuming Alice, Bill, and Chris are person 0, 1, and 2 respectively (0, 1, 2 are the person's ID), the transactions can be represented as [[0, 1, 10], [2, 0, 5]].
Given a list of transactions between a group of people, return the minimum number of transactions required to settle the debt.
Note:
A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
Person's IDs may not be linear, e.g. we could have the persons 0, 1, 2 or we could also have the persons 0, 2, 6.
Example 1:
Input:
[[0,1,10], [2,0,5]]
Output:
2
Explanation:
Person #0 gave person #1 $10.
Person #2 gave person #0 $5.
Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each.
Example 2:
Input:
[[0,1,10], [1,0,1], [1,2,5], [2,0,5]]
Output:
1
Explanation:
Person #0 gave person #1 $10.
Person #1 gave person #0 $1.
Person #1 gave person #2 $5.
Person #2 gave person #0 $5.
Therefore, person #1 only need to give person #0 $4, and all debt is settled.
public class Solution {
public int minTransfers(int[][] transactions) {
if(transactions == null || transactions.length == 0) return 0;
Map<Integer, Integer> acc = new HashMap<>();
for(int i = 0;i<transactions.length;i++){
int id1 = transactions[i][0];
int id2 = transactions[i][1];
int m = transactions[i][2];
acc.put(id1, acc.getOrDefault(id1, 0)-m);
acc.put(id2, acc.getOrDefault(id2, 0)+m);
}
List<Integer> negs = new ArrayList<>();
List<Integer> poss = new ArrayList<>();
for(Integer key:acc.keySet()){
int m = acc.get(key);
if(m == 0) continue;
if(m<0) negs.add(-m);
else poss.add(m);
}
int ans = Integer.MAX_VALUE;
Stack<Integer> stNeg = new Stack<>(), stPos = new Stack<>();
for(int i =0;i<1000;i++){
for(Integer num:negs) stNeg.push(num);
for(Integer num:poss) stPos.push(num);
int cur = 0;
while(!stNeg.isEmpty()){
int n = stNeg.pop();
int p = stPos.pop();
cur++;
if(n == p) continue;
if(n>p){
stNeg.push(n-p);
} else {
stPos.push(p-n);
}
}
ans = Math.min(ans, cur);
Collections.shuffle(negs);
Collections.shuffle(poss);
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment