Skip to content

Commit 9cb0b39

Browse files
authored
Create Find Missing Observations
1 parent f2e23c5 commit 9cb0b39

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
想法:只要先算出剩餘骰子總和是多少,接著一次填入即可,因為每面骰子最小值是1
2+
所以我們可以紀錄一個left 變數來表示還有多少點數要加在未知的骰子上
3+
注意:需注意兩種邊界情況
4+
1.left 太大:就算未知骰子全部都是6也填不完
5+
2.left < 0:代表mean值太小不可能達到
6+
所以兩種情況都要輸出空陣列
7+
8+
Time Complexity : O(n) for traversing the array
9+
Space Complexity : O(n) for answer array
10+
11+
class Solution {
12+
public:
13+
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
14+
int sum = 0 ;
15+
for( auto &i : rolls )
16+
sum += i ;
17+
18+
vector<int> ans(n , 1) ;
19+
int left = mean * (n + rolls.size()) - sum - n ;
20+
for( int i = 0 ; i < n ; i++ ) {
21+
if ( left > 0 ) {
22+
ans[i] = min( 6 , 1 + left ) ;
23+
left -= ( ans[i] - 1 ) ;
24+
}
25+
}
26+
if ( left == 0 )
27+
return ans ;
28+
else
29+
return {} ;
30+
}
31+
};

0 commit comments

Comments
 (0)