2つの整数を考える tomatoSlices
と cheeseSlices
。次のように異なるハンバーガーの成分は以下のとおりです。
- ジャンボバーガー:4つのトマトのスライスと1つのスライスチーズ。
- 小さなバーガー:2つのトマトのスライスと1つのスライスチーズ。
リターン [total_jumbo, total_small]
ように残りの数 tomatoSlices
は0に等しく、残りの数 cheeseSlices
が残りすることが可能でない場合は0に等しい tomatoSlices
と cheeseSlices
0リターンに等しいです []
。
例1:
入力:tomatoSlices = 16、cheeseSlices = 7 出力:[1,6] Explantion:1つのジャンボバーガーと6個の小さなハンバーガーを作るために、我々は4 * 1 + 2 * 6 = 16トマトと1 + 6 = 7チーズを必要としています。何の残りの成分はありません。
例2:
入力:tomatoSlices = 17、cheeseSlices = 4 出力:[] Explantion:小さなとジャンボハンバーガーを作るためにすべての材料を使用する方法はありません。
例3:
入力:tomatoSlices = 4、cheeseSlices = 17 出力:[] Explantion:1つのジャンボバーガーを作る残りの15チーズが存在するであろう2個の小さなハンバーガーを残りと製造16チーズが存在するであろう。
例4:
入力:tomatoSlices = 0、cheeseSlices = 0 出力:[0,0]
例5:
入力:tomatoSlices = 2、cheeseSlices = 1つの 出力:[0,1]
制約:
0 <= tomatoSlices <= 10^7
0 <= cheeseSlices <= 10^7
クラスのソリューション{ 公共の一覧<整数> numOfBurgers(int型 tomatoSlices、int型cheeseSlices){ 一覧 <整数> RES = 新しいArrayListを(); もし(tomatoSlices == 0 && cheeseSlices == 0 ){ res.add( 0 )。 res.add( 0 )。 リターンのres; } int型の半分= cheeseSlices / 2 。 // INT [] [] ARR =新しいINT [半分+ 1] [2]。 用(INTが I = 0; I <=半; I ++ ){ int型T1 = I; int型 T2 = cheeseSlices - I; もし(T1 * 4 + T2 * 2 == tomatoSlices){ res.add(T1)。 (T2)res.add。 } そう であれば(T1 * 2 + T2 * 4 == tomatoSlices){ res.add(T2)。 (T1)res.add。 } } 戻りRES。 } }
CNM、O(n)は、TLEを与え
読み取りケージの問題で鶏やウサギであることが判明し、次に答える、私は本当に理にかなって思いました
クラスソリューション{ 公共一覧<整数> numOfBurgers(INT T、INT C){ リスト <整数> RES = 新しいArrayListを()。 もし(T%2 == 0 && C * 2 <= T && T <= C * 4 ){ res.add(T / 2 - C)。 res.add(C * 2 - T / 2 )。 } それ以外の リターンのres; リターンのres; } }