gpt4 book ai didi

java - 可以改进解决方案吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:20 27 4
gpt4 key购买 nike

<分区>

问题陈述

It is the wedding day of Sanchi, the beautiful princess of Byteland. Her fiance Krishna is planning to gift her an awesome ruby necklace. Krishna has currently b -blue rubies, g -green rubies, r-red rubies and y -yellow rubies. He has to arrange the rubies next to each other in a straight line to make the necklace. But, there are a couple of rules to be followed while making this necklace:

  • A blue ruby should be followed by either a blue ruby or a red ruby
  • A green ruby should be followed by either a green ruby or a yellow ruby
  • A red ruby should be followed by either a green ruby or a yellow ruby
  • A yellow ruby should be followed by either a blue ruby or a red ruby
  • If it is possible, we should always start a necklace with a blue or a red ruby

Can you tell what is the maximum possible length of the necklace that Krishna can make. The length of a necklace is the number of rubies in it.

Input Format

  • The first line contains an integer representing b.
  • The second line contains an integer representing r.
  • The third line contains an integer representing y.
  • The fourth line contains an integer representing g.

Constraints

  • 0 <= b, r, y, g <= 2000
  • At least one of b, r, y, g is greater than 0

Output Format

  • A single integer which is the answer to the problem.

Sample TestCase 1

Input
1
1
1
0
Output
3

我的解决方案:

package ruby;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class CandidateCode {

public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Integer b = new Integer(br.readLine());
Integer r = new Integer(br.readLine());
Integer y = new Integer(br.readLine());
Integer g = new Integer(br.readLine());

if (b + r + y + g > 0 && b <= 2000 && b <= 2000 && r <= 2000 &&
g <= 2000 && y <= 2000 && b >= 0 && r >= 0
&& g >= 0 && y >= 0) {

ArrayList<Character> necklace = new ArrayList<Character>();
CandidateCode rn = new CandidateCode();
int length = rn.calculateLengthofNecklace(b, r, y, g,
necklace);
System.out.println(length);
}
} catch (Exception e) {
}

}

private int calculateLengthofNecklace(Integer b, Integer r, Integer y, Integer g, ArrayList<Character> necklace) {
if (anyRemaining(b))
necklace = addBlue(b, r, y, g, necklace);
else if (anyRemaining(r))
necklace = addRed(b, r, y, g, necklace);
else if (anyRemaining(g))
necklace = addGreen(b, r, y, g, necklace);
else if (anyRemaining(y))
necklace = addYellow(b, r, y, g, necklace);
// System.out.println("necklace :" + necklace.toString());
return necklace.size();
}

private ArrayList<Character> addRed(Integer b, Integer r, Integer y, Integer g, ArrayList<Character> necklace) {
char lastruby = necklace.size() > 0 && necklace.get(necklace.size() - 1) != null
? necklace.get(necklace.size() - 1) : 'z';
if (anyRemaining(r) && (lastruby == 'y' || lastruby == 'b' || lastruby == 'z')) {
necklace.add('r');
r = r - 1;
// System.out.println("in addRed\n b:" + b + " r: " + r + " y: " + y
// + " g: " + g);
} else {
return necklace;
}
ArrayList<Character> n1 = new ArrayList<Character>(necklace);
ArrayList<Character> n2 = new ArrayList<Character>(necklace);
n1 = addGreen(b, r, y, g, n1);
n2 = addYellow(b, r, y, g, n2);
necklace = n1.size() > n2.size() ? n1 : n2;
return necklace;

}

private ArrayList<Character> addYellow(Integer b, Integer r, Integer y, Integer g, ArrayList<Character> necklace) {
char lastruby = necklace.size() > 0 && necklace.get(necklace.size() - 1) != null
? necklace.get(necklace.size() - 1) : 'z';
if (anyRemaining(y) && (lastruby == 'r' || lastruby == 'g' || lastruby == 'z')) {
necklace.add('y');
y = y - 1;
// System.out.println("in addYellow\n b:" + b + " r: " + r + " y: "
// + y + " g: " + g);
} else {
return necklace;
}
ArrayList<Character> n1 = new ArrayList<Character>(necklace);
ArrayList<Character> n2 = new ArrayList<Character>(necklace);
n1 = addBlue(b, r, y, g, n1);
n2 = addRed(b, r, y, g, n2);
necklace = n1.size() > n2.size() ? n1 : n2;
return necklace;
}

private ArrayList<Character> addGreen(Integer b, Integer r, Integer y, Integer g, ArrayList<Character> necklace) {
char lastruby = necklace.size() > 0 && necklace.get(necklace.size() - 1) != null
? necklace.get(necklace.size() - 1) : 'z';
if (anyRemaining(g) && (lastruby == 'r' || lastruby == 'g' || lastruby == 'z')) {
necklace.add('g');
g = g - 1;
// System.out.println("in addGreen\n b:" + b + " r: " + r + " y: " +
// y + " g: " + g);
} else {
return necklace;
}
ArrayList<Character> n1 = new ArrayList<Character>(necklace);
ArrayList<Character> n2 = new ArrayList<Character>(necklace);
n1 = addGreen(b, r, y, g, n1);
n2 = addYellow(b, r, y, g, n2);
necklace = n1.size() > n2.size() ? n1 : n2;
return necklace;

}

private ArrayList<Character> addBlue(Integer b, Integer r, Integer y, Integer g, ArrayList<Character> necklace) {
char lastruby = necklace.size() > 0 && necklace.get(necklace.size() - 1) != null
? necklace.get(necklace.size() - 1) : 'z';
if (anyRemaining(b) && (lastruby == 'y' || lastruby == 'b' || lastruby == 'z')) {
necklace.add('b');
b = b - 1;
// System.out.println("in addBlue\n b:" + b + " r: " + r + " y: " +
// y + " g: " + g);
} else {
return necklace;
}
ArrayList<Character> n1 = new ArrayList<Character>(necklace);
ArrayList<Character> n2 = new ArrayList<Character>(necklace);
n1 = addBlue(b, r, y, g, n1);
n2 = addRed(b, r, y, g, n2);
necklace = n1.size() > n2.size() ? n1 : n2;
return necklace;

}

private boolean anyRemaining(Integer b) {
return b > 0;

}

}

这个解决方案可以改进吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com