分金币问题---阿里巴巴2018年校招内推Java研发岗在线编程测验

分金币的问题

思路 一:

使用huffman树实现

代码:

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;

//创建Huffman树的节点
class Node<T> implements Comparable<Node<T>> {
	private double weight;
	private Node<T> left;
	private Node<T> right;
	public Node(double weight) {		
		this.weight = weight;
	}	
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public Node<T> getLeft() {
		return left;
	}
	public void setLeft(Node<T> left) {
		this.left = left;
	}
	public Node<T> getRight() {
		return right;
	}
	public void setRight(Node<T> right) {
		this.right = right;
	}
	public String toString() {
		return "weight : " + this.weight + "\n";
	}
	public int compareTo(Node<T> other) {
		return (int) (other.getWeight() - this.getWeight());
	}
}
//创建Huffman树
class HuffmanTree<T> {
	static <T> Node<T> createTree(List<Node<T>> nodes) {
		while (nodes.size() > 1) {
			Collections.sort(nodes);
			Node<T> left = nodes.get(nodes.size() - 1);
			Node<T> right = nodes.get(nodes.size() - 2);
			Node<T> parent = new Node<T>(left.getWeight() + right.getWeight());
			parent.setLeft(left);
			parent.setRight(right);
			nodes.remove(left);
			nodes.remove(right);
			nodes.add(parent);
		}
		return nodes.get(0);
	}
	static <T> List<Node<T>> breadth(Node<T> root) {
		List<Node<T>> list = new ArrayList<Node<T>>();
		Queue<Node<T>> queue = new ArrayDeque<Node<T>>();
		if (root != null) {
			queue.offer(root);
		}
		while(!queue.isEmpty()) {
			list.add(queue.peek());
			Node<T> node = queue.poll();
			if (node.getLeft() != null) {
				queue.offer(node.getLeft());
			}
			if (node.getRight() != null) {
				queue.offer(node.getRight());
			}
		}
		return list;
	}
}
public class 分金币_huffman编码 {
	public static void main(String[] args) {
		List<Node<Integer>> list = new ArrayList<Node<Integer>>();
		list.add(new Node<Integer>(2));
		list.add(new Node<Integer>(3));
		list.add(new Node<Integer>(4));
		list.add(new Node<Integer>(5));
		list.add(new Node<Integer>(6));
		list.add(new Node<Integer>(7));
		list.add(new Node<Integer>(9));
		Node<Integer> root = HuffmanTree.createTree(list);		
		list = HuffmanTree.breadth(root);
		int sum = 0;
		for (int i = 1; i < list.size(); i++) {
			sum += list.get(i).getWeight();
		}
		System.out.println(sum);
		Object o = new Object();
	}
}

思路二:

优先队列实现

代码:

import java.util.PriorityQueue;
import java.util.Scanner;

public class 分金币_优先队列 {
	public static int getMinPrice(int[] prices){
		int len = prices.length;
		PriorityQueue<Integer> queue = new PriorityQueue<Integer>(len);
		for (int i = 0; i < len; i++) {
			queue.add(prices[i]);
		}
		int sum = 0;
		while(!queue.isEmpty()) {
			int first = queue.poll();
			if (!queue.isEmpty()) {
				int second = queue.poll();
				int third = first + second;
				sum += third;
				queue.add(third);
			} 
		}
		return sum;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int[] prices = new int[n];
			for (int i = 0; i < n; i++) {
				prices[i] = sc.nextInt();
			}
			System.out.println(getMinPrice(prices));
		}
		sc.close();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值