Find the largest multiple of 3 解答
Question
Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements.
For example, if the input array is {8, 1, 9}, the output should be “9 8 1″, and if the input array is {8, 1, 7, 6, 0}, output should be “8 7 6 0″.
Solution
A number is multiple of 3 if and only if the sum of digits of number is multiple of 3.
1. Sort the array in non-decreasing order. 2. Take three queues. One for storing elements which on dividing by 3 gives remainder as 0.The second queue stores digits which on dividing by 3 gives remainder as 1. The third queue stores digits which on dividing by 3 gives remainder as 2. Call them as queue0, queue1 and queue2 3. Find the sum of all the digits. 4. Three cases arise:
……4.1 The sum of digits is divisible by 3. Dequeue all the digits from the three queues. Sort them in non-increasing order. Output the array. ……4.2 The sum of digits produces remainder 1 when divided by 3.
Remove one item from queue1. If queue1 is empty, remove two items from queue2. If queue2 contains less than two items, the number is not possible. ……4.3 The sum of digits produces remainder 2 when divided by 3.
Remove one item from queue2. If queue2 is empty, remove two items from queue1. If queue1 contains less than two items, the number is not possible. 5. Finally empty all the queues into an auxiliary array. Sort the auxiliary array in non-increasing order. Output the auxiliary array.
Find the largest multiple of 3 解答的更多相关文章
- Largest Rectangle in Histogram 解答
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode(四)
Find Kth Largest Number public class Solution { public int findKthLargest(int[] nums, int k) { retur ...
- Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...
- Kth Largest Element in an Array 解答
Question Find the kth largest element in an unsorted array. Note that it is the kth largest element ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 二分套二分 hrbeu.acm.1211Kth Largest
Kth Largest TimeLimit: 1 Second MemoryLimit: 32 Megabyte Description There are two sequences A and ...
随机推荐
- 推荐2个小工具 .NET reflector resharper
- 在 Java 应用程序中使用 Elasticsearch
如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...
- hibernate 对 sql server 2005 分页改进
Hibernate 可以实现分页查询 如下 Query q = session.createQuery("from Cat as c"); q.setFirstResult(100 ...
- JavaScript进阶篇 - -第1章 系好安全带
第1章 系好安全带 html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", ...
- 马士兵Servlet&Jsp学习
Servlet&JSP 1>http常见错误信息: 404--url地址找不找, 403--禁止访问 500--服务器内部错误 2>Servlet的生命周期: *生命全过 ...
- mysql 从data文件恢复数据库
安装在D:\mysql\mysql-5.6.24-winx64下的mysql 由于系统坏了,移到另外一台机器上启动 步骤如下 1.复制以前的mysql安装文件及data文件下:2.全新安装mysql3 ...
- 56个PHP开发常用代码
2016/02/14 6203 4 在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常 ...
- bootstrap学习以及其插件
Bootstrap中文网地址,里面有bootstrap组件的下载与使用说明,现在使用bootstrap3: http://www.bootcss.com/ W3CSchool.CC里面有学习boots ...
- (转)iOS7界面设计规范(13) - UI基础 - 与iOS的系统整合
突然就到了周日傍晚.你永远不会知道自己的生活在接下来的一周当中能够发生多少变化:各种不可预知性所带来的更多是快感还是焦虑与不安,冷暖自知.相比之下,白天工作当中那些需求列表与排期文档就显得那么可爱了, ...
- [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)
Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the ...