Roll Dice

A six-sided die is a small cube with a different number of pips on each face (side), ranging from 1 to 6.
On any two opposite side of the cube, the number of pips adds up to 7; that is, there are three pairs of opposite sides: 1 and 6, 2 and 5, and 3 and 4.
There are N dice lying on a table, each showing the pips on its top face. In one move, you can take one die and rotate it to an adjacent face.
For example, you can rotate a die that shows 1 s that it shows 2, 3, 4 or 5. However, it cannot show 6 in a single move, because the faces with one pip and six pips visible are opposite sides rather than adjacent.
You want to show the same number of pips on the top face of all N dice. Given that each of the dice can be moved multiple times, count the minimum number of moves needed to get equal faces.

Write a function that, given an array A consisting of N integers describing the number of pips (from 1 to 6) shown on each die's top face, returns the minimum number of moves necessary for each die show the same number of pips.

Example 1:

Input: A = [1, 2, 3]
Output: 2
Explanation: You can pick the first two dice and rotate each of them in one move so that they all show three pips on the top face.
Notice that you can also pick any other pair of dice in this case.

Example 2:

Input: A = [1, 1, 6]
Output: 2
Explanation: The only optimal answer is to rotate the last die so that it shows one pip. It is necessary to use two rotations to achieve this.

Example 3:

Input: A = [1, 6, 2, 3]
Output: 3
Explanation: For instance, you can make all dice show 2: just rotate each die which is not showing 2.
Notice that for each die you can do this in one move.

A ssume that:
• N is an integer within the range [1..100];
• each element of the array A is an integer within the range [1..6].

二 分析

这也是讨论区的题目,没有LeetCode在线题目编号,一看英语很长头就大,慢慢看还好(主要靠猜),扔骰子的问题。所以头两段介绍骰子(其实方言习惯说shai),6个面,分别是1,-6个数,对面的数字加起来是7.扔骰子的时候,有个面朝上,边上4个数字翻一下也能朝上,底面的需要两步才能朝上。

问题:有个数组,里面的数字就是1-6的范围,求最小的移动次数,使得所有面朝上一致。

一看到扔骰子以为是概率问题,好紧张。仔细看是求最小代价。。

首先得分析好点。这里就是每种不同情况需要移动的步骤:

1. 底面在下的。就是相加=7的,需要移动两步。

2相同的,不需要移动。

3.其他的(就是抛去顶面、底面周边一圈的4个),移动一步。

这样就可以暴力循环,把1-6范围的每种情况对应的数组移动步数算出来,取最小值。

public static void main(String[] args) {
		int[] num = new int[]{1,2,3};
	    System.out.println(minNumber(num));
	    num = new int[]{1,1,6} ;
	    System.out.println(minNumber(num));
	    num = new int[]{1, 6, 2, 3};
	    System.out.println(minNumber(num));
		
	}
	
	public static int minNumber(int[] nums) {
		 int min = Integer.MAX_VALUE;
		for (int i = 1; i < 7; i++) {
			int tmp =0;
			for(int j=0;j< nums.length;j++){
				if(i == j){
					continue;
				}
				else if(i+j ==7){
					tmp = tmp+2;
				}
				else{
					tmp = tmp+1;
				}
			}
			min = Math.min(min, tmp);
		}	
		
		return min;
		
	}

在线讨论区有更快的方法没看懂。感兴趣的可以去看看。

https://leetcode.com/discuss/interview-question/331158/Amazon-or-Online-Assessment-2019-or-Roll-Dice

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值