###### tags: `Weekly Contest` # Weekly Contest 391 ## [3099. Harshad Number]() (<font color=#00B8A3>Easy</font>) 限制 : <ul> <li><code>1 <= x <= 100</code></li> </ul> ### Solution 這題是要把字元拆開再組回去,然後看有沒有跟原數有倍數關係。 #### 時間複雜度: $O(1)$ #### 空間複雜度: $O(1)$ 程式碼: ```c++= class Solution { public: int sumOfTheDigitsOfHarshadNumber(int x) { if(x == 100) return 1; int num = x%10 + x/10; if(x % num) return -1; return num; } }; ``` ## [3100. Water Bottles II](https://leetcode.com/problems/water-bottles-ii/) (<font color=#FFC011>Medium</font>) 限制 : <ul> <li><code>1 <= numBottles <= 100</code></li> <li><code>1 <= numExchange <= 100</code></li> </ul> ### Solution 這題其實就是依照喝、交換、換回來的瓶子做運算,跟著他給的步驟就好。 #### 時間複雜度: $O(N)$ #### 空間複雜度: $O(1)$ 程式碼: ```c++= class Solution { public: int maxBottlesDrunk(int numBottles, int numExchange) { int bottlesDrink = 0; int fullBottles = numBottles; numBottles = 0; while (fullBottles > 0) { // drink bottlesDrink += fullBottles; numBottles += fullBottles; fullBottles = 0; // exchange while (numBottles >= numExchange) { fullBottles += 1; numBottles -= numExchange++; } } return bottlesDrink; } }; ``` ## [3]()(<font color=#FFC011>Medium</font>) 限制 : <ul> <li><code>10<sup>4</sup></code></li> </ul> ### Solution #### 時間複雜度: $O()$ #### 空間複雜度: $O()$ 程式碼: ```c++= ``` ## [4]()(<font color=#FF375F>Hard</font>) 限制 : <ul> <li><code>10<sup>4</sup></code></li> </ul> ### Solution #### 時間複雜度: $O()$ #### 空間複雜度: $O()$ 程式碼: ```c++= ```