1615. 投资结果
描述
给定一个列表funds表示投资人每次的投资额。现在有三个公司A, B, C,它们的初始资金分别为a,b,c。投资人每次投资时会对当前资金最少的公司进行投资(当有多个公司资金相同时,投资人会对编号最小的公司进行投资)。返回A, B, C三家公司最后的资金。
1<= funds的长度<= 500000
1<= funds[i],a,b<=100
您在真实的面试中是否遇到过这个题?
样例
给定funds=[1,2,1,3,1,1],a=1,b=2,c=1, 返回[4,5,4]
解释:
第一次投资时A和C的资金相同,选择对编号较小的A投资,此时a=2, b=2, c=1
第二次投资时C的资金最少,对C进行投资,此时a=2, b=2, c=3
第三次投资时A和B的资金相同,选择对编号较小的A投资,此时a=3, b=2, c=3
第四次投资时B的资金最少,对B进行投资,此时a=3, b=5, c=3
第五次投资时A和C的资金相同,选择对编号较小的A投资,此时a=4, b=5, c=3
第六次投资时C的资金最少,对C进行投资,此时a=4, b=5, c=4
给定funds=[2,1,1,1],a=1,b=2,c=2, 返回[4,3,3]
解释:
第一次投资时A的资金最少,对A进行投资,此时a=3, b=2, c=2
第二次投资时B和C的资金相同,选择对编号较小的B投资,此时a=3, b=3, c=2
第三次投资时C的资金最少,对C进行投资,此时`a=3, b=3, c=3
第四次投资时A, B和C的资金相同,选择对编号较小的`A`投资,此时a=4, b=3, c=3
public class Solution {
/**
* @param funds: The investment each time
* @param a: The initial funds of A
* @param b: The initial funds of B
* @param c: The initial funds of C
* @return: The final funds
*/
public static int[] getAns(int[] funds, int a, int b, int c) {
// Write your code here
int[] arr = new int[] {a,b,c};
for (int i=0;i<funds.length;i++) {
if(arr[2] < arr[1] && arr[2] < arr[0]) {
arr[2] += funds[i];
}else if(arr[1] < arr[0] && arr[1] <= arr[2] ) {
arr[1] += funds[i];
}else {
arr[0] += funds[i];
}
}
return arr;
}
}
Description
Given a list funds representing the investor's investment each time. There are three companies A, B, C, and their initial funds are a, b, and c. Investors will invest in the company with the fewest funds each time (when multiple companies have the same funds, the investor will invest in the company with the smallest id). Output the final funds of A, B, C.
1<= The length of funds<= 500000
1<= funds[i],a,b<=100
Have you met this question in a real interview?
Example
Given funds=[1,2,1,3,1,1], a=1, b=2, c=1, return [4,5,4]
Explanation:
In the first round of investment, the funds of A and C are the same, and A is selected. At this time, a=2, b=2, c=1
In the second round of investment, C has the minimal funds, and invest in C, at this time a=2, b=2, c=3
In the third round of investment, the funds of A and B are the same, and A is selected. At this time, a=3, b=2, c=3
In the fourth round of investment, B has the minimal funds, and invest in B, at this time a=3, b=5, c=3
In the fifth round of investment, the funds of A and C are the same, and A is selected. At this time, a=4, b=5, c=3
In the sixth round of investment, C has the minimal funds, and invest in C, at this time a=4, b=5, c=4
Given funds=[2,1,1,1], a=1, b=2, c=2, return [4,3,3]
Explanation:
In the first round of investment, A has the minimal funds, and invest in A, at this time a=3, b=2, c=2
In the second round of investment, the funds of B and C are the same, and B is selected. At this time, a=3, b=3, c=2
In the third round of investment, C has the minimal funds, and invest in C, at this time a=3, b=3, c=3
In the fourth round of investment, the funds of A, B and C are the same, and A is selected. At this time, a=4, b=3, c=3

浙公网安备 33010602011771号