1363. Largest Multiple of Three (H)

Largest Multiple of Three (H)

Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order.

Since the answer may not fit in an integer data type, return the answer as a string.

If there is no answer return an empty string.

Example 1:

Input: digits = [8,1,9]
Output: "981"

Example 2:

Input: digits = [8,6,7,1,0]
Output: "8760"

Example 3:

Input: digits = [1]
Output: ""

Example 4:

Input: digits = [0,0,0,0,0,0]
Output: "0"

Constraints:

  • 1 <= digits.length <= 10^4
  • 0 <= digits[i] <= 9
  • The returning answer must not contain unnecessary leading zeros.

题意

给定一个只包含个位数的数组,将其组合成一个最大的多位数字符串,使这个多位数是3的倍数。

思路

首先需要知道一条性质:如果一个多位数的各位数字之和是3的倍数,那么这个多位数一定是3的倍数(充要条件)。很好证明:
( a b c d e ) 10 = a ∗ 10000 + b ∗ 1000 + c ∗ 100 + d ∗ 10 + e = ( a + b + c + d + e ) + a ∗ 9999 + b ∗ 999 + c ∗ 99 + d ∗ 9 (abcde)_{10} = a*10000+b*1000+c*100+d*10+e=(a+b+c+d+e)+a*9999+b*999+c*99+d*9 (abcde)10=a10000+b1000+c100+d10+e=(a+b+c+d+e)+a9999+b999+c99+d9
从公式可以看出只要 a+b+c+d+e 是3的倍数,(abcde)一定是3的倍数;同样,如果(abcde)是3的倍数,那么 a+b+c+d+e也一定是3的倍数。

在这个性质的基础上,我们用一张hash表记录所有数字出现的次数,并计算出总和sum,会有3种情况:

  1. sum % 3 == 0。说明用所有数字组合成的多位数一定是3的倍数;
  2. sum % 3 == 1。说明需要去掉一个模3为1的数,或者去掉两个模3为2的数;
  3. sum % 3 == 2。说明需要去掉一个模3为2的数,或者去掉两个模3为1的数。

进行完上述操作后,将hash表中剩余的所有数字组合起来就是要求的答案。


代码实现

class Solution {
    public String largestMultipleOfThree(int[] digits) {
        int[] hash = new int[10];
        int sum = 0;
        int remain1 = 0, remain2 = 0;
        String ans = null;
        
        // 遍历生成hash表并求和
        for (int i = 0; i < digits.length; i++) {
            sum += digits[i];
            hash[digits[i]]++;
        }
        remain1 = hash[1] + hash[4] + hash[7];		// 模3余1的数的个数
        remain2 = hash[2] + hash[5] + hash[8];		// 模3余2的数的个数
        
        // 分情况处理
        if (sum % 3 == 1) {
            if (remain1 >= 1) {
                for (int i = 1; i <= 7; i += 3) {
                    if (hash[i] >= 1) {
                        hash[i]--;
                        break;
                    }
                }
            } else {
                int count = 0;
                for (int i = 2; i <= 8; i += 3) {
                    while (hash[i] != 0 && count != 2) {
                        hash[i]--;
                        count++;
                    }
                }
            }
        } else if (sum % 3 == 2) {
            if (remain2 >= 1) {
                for (int i = 2; i <= 8; i += 3) {
                    if (hash[i] >= 1) {
                        hash[i]--;
                        break;
                    }
                }
            } else {
                for (int i = 1; i <= 7; i += 3) {
                    int count = 0;
                    while (hash[i] != 0 && count != 2) {
                        hash[i]--;
                        count++;
                    }
                }
            }
        }

        return generate(hash);
    }

    private String generate(int[] hash) {
        StringBuilder sb = new StringBuilder();
        for (int i = 9; i >= 1; i--) {
            for (int j = 0; j < hash[i]; j++) {
                sb.append(i);
            }
        }
        for (int i = 0; i < hash[0]; i++) {
            sb.append(0);
            if (sb.length() == 1) {
                break;
            }
        }
        return sb.toString();
    }
}

代码实现 - 简化版

// 简化版不求和,只对余数进行操作
class Solution {
    public String largestMultipleOfThree(int[] digits) {
        int[] hash = new int[10];
        int remain1 = 0, remain2 = 0;
        for (int i = 0; i < digits.length; i++) {
            hash[digits[i]]++;
        }
        remain1 = hash[1] + hash[4] + hash[7];
        remain2 = hash[2] + hash[5] + hash[8];
        
        int remainder = (remain1 + 2 * remain2) % 3;
        if (remainder == 1) {
            if (remain1 > 0) remain1--;
            else remain2 -= 2;
        } else if (remainder == 2) {
            if (remain2 > 0) remain2--;
            else remain1 -= 2;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 9; i >= 1; i--) {
            if (i % 3 == 0) {
                while (hash[i]-- > 0) sb.append(i);
            } else if (i % 3 == 1) {
                while (hash[i]-- > 0 && remain1-- > 0) sb.append(i);
            } else {
                while (hash[i]-- > 0 && remain2-- > 0) sb.append(i);
            }
        }
        while (hash[0]-- > 0) {
            sb.append(0);
            if (sb.length() == 1) break;
        }
        
        return sb.toString();
    }
}

参考

[Java] Basic Multiple of 3 - Clean code - O(N) ~ 2ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值