博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 465: Optimal Account Balance
阅读量:6870 次
发布时间:2019-06-26

本文共 1107 字,大约阅读时间需要 3 分钟。

DFS to search all possible combine.

class Solution {    public int minTransfers(int[][] transactions) {        if (transactions.length == 0) return 0;        int nums = 0;        for (int[] trans : transactions) {            nums = Math.max(nums, Math.max(trans[0], trans[1]));        }        int[] debt = new int[nums + 1];        for (int[] trans : transactions) {            debt[trans[0]] += trans[2];            debt[trans[1]] -= trans[2];        }        return dfs(debt, 0, 0);    }        private int dfs(int[] debt, int index, int count) {        while (index < debt.length && debt[index] == 0) index++;        int result = Integer.MAX_VALUE;        for (int i = index + 1, prev = 0; i < debt.length; i++) {            if (debt[i] != prev && debt[i] * debt[index] < 0) {                debt[i] += debt[index];                result = Math.min(result, dfs(debt, index + 1, count + 1));                debt[i] -= debt[index];                prev = debt[i];            }        }        return result < Integer.MAX_VALUE ? result : count;    }}

 

转载于:https://www.cnblogs.com/shuashuashua/p/7656407.html

你可能感兴趣的文章
kubernetes-1.11.0集群部署之master集群 (二)
查看>>
IDEA PermGen space内存溢出
查看>>
Create a RHEL6 PXE Installation Server
查看>>
【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!
查看>>
桌面支持--Office2013没有Office Picture Manage怎么安装
查看>>
chmod修改文件权限失败
查看>>
数据结构与算法-->互为素数
查看>>
Linux系统学习方法——写给小白
查看>>
Nginx服务器报500 Internal Server Error错误
查看>>
链表的游标实现
查看>>
Linux下查看CPU信息、机器型号等硬件信息命令
查看>>
Lync Server 2013 部署 _ 部署简介及系统要求
查看>>
前端小随笔
查看>>
view属性大全
查看>>
Java文件编码示例
查看>>
CactiFans V1.0中文版发布
查看>>
HTML如何显示小于号“<”等特殊符号?
查看>>
别伤了虚拟桌面管理员的"心"
查看>>
Windows系统使用IntelliJ IDEA 搭建Hadoop的开发调试环境(一)
查看>>
yum安装lamp
查看>>