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:

  1. A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
  2. 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. Runtime 24 ms, faster than 30.39% 这题本来以为是图,结果是数组的题。先建立每一个人的一个账户,然后DFS,DFS的时候从0-n,把第i个账户的钱都转到某一个j,这两个账户的金额是相反的,
因为只有相反的才会有交易。
class Solution {
public:
int minTransfers(vector<vector<int>>& transactions) {
map<int,int> m;
for(auto t: transactions){
m[t[]] -= t[];
m[t[]] += t[];
}
vector<int> accnt(m.size());
int cnt = ;
for(auto a : m){
if(a.second != ) accnt[cnt++] = a.second;
}
return helper(accnt, , cnt, );
}
int helper(vector<int>& accnt, int start, int n, int num){
int ret = INT_MAX;
while(start < n && accnt[start] == ) start++;
for(int i=start+; i<n; i++){
if((accnt[start] < && accnt[i] > ) || (accnt[start] > && accnt[i] < )){
accnt[i] += accnt[start];//加入第i个账户中,这个时候start账户已经没有钱了,可以进行下一个账户清理了
ret = min(ret, helper(accnt,start+, n, num+));//DFS,保存最小值
accnt[i] -= accnt[start];
}
}
return ret == INT_MAX ? num : ret;
}
};
												

LC 465. Optimal Account Balancing 【lock,hard】的更多相关文章

  1. LC 727. Minimum Window Subsequence 【lock,hard】

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  2. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  3. [LeetCode] 465. Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  4. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  5. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  6. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  8. LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  9. 【深圳,武汉】一加科技(One Plus)招聘,寻找不...

    [深圳,武汉]一加科技(One Plus)招聘,寻找不... [深圳,武汉]一加科技(One Plus)招聘,寻找不... 来自: 一加 2013-12-30 15:28:04         标题: ...

随机推荐

  1. Java反射【三、方法的反射】

    获取一个类下的所有方法 可以获取类类型后,获取到所有方法及相关信息 Method[] ms = c.getMethods(); 获取方法列表(public) Method[] ms = c.getDe ...

  2. Java十进制转二进制

    闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正. public static String toBinaryString(int j) { if (j < 0) ...

  3. c++11 移动语义move semantics

    performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...

  4. SQL语句复习【专题九】

    SQL语句复习[专题九] 视图:View视图的概念:视图是从若干基本表或其他视图构造出来的表.在创建一个视图时,只是存放的视图的定义,也即是动态检索数据的查询语句,而并不存放视图对应的数据在用户使用视 ...

  5. 8.1.Zookeeper概念简介

    1.分布式系统概述 理解1: 分布式系统:分布式系统是针对一个大系统而言,将一个大系统分成多个子系统,即多个工程系统. 我们先看下传统的系统模式:    传统的系统模式将多个功能模块全部在一个工程中写 ...

  6. checkbox 和 selected 混淆

    1.option里面的selected属性,如果我们在下拉列表里面选择了一个option那么他的selected="true" ,如果我们想设置当前的option是选中  状态的, ...

  7. 查看电脑物理地址(MAC)方法

    首先打开电脑,按ctrl+R键,将会出现以下界面 然后直接点击确认即可,会出现管理员界面,如下 我们现在有两种查看MAC地址的方法: 方法一:.直接输入ipconfig/all(或者输入ipconfi ...

  8. Websocket @serverendpoint 404

    今天写一个前后端交互的websocket , 本来写着挺顺利的,但测试的时候蒙了,前端websocket发的连接请求竟然连接不上 返回状态Status 报了个404 ,然后看后台onError方法也没 ...

  9. 点击startup.bat启动tomcat出现乱码

    找到tomcat目录下的/conf/logging.properties添加语句:java.util.logging.ConsoleHandler.encoding = GBK重启tomcat 问题解 ...

  10. 怎么卸载hexo

    npm uninstall hexo -g 卸载失败 npm uninstall hexo-cli -g 推荐这个,成功卸载