tags: Weekly Contest

Weekly Contest 391

3099. Harshad Number (Easy)

限制 :

  • 1 <= x <= 100

Solution

這題是要把字元拆開再組回去,然後看有沒有跟原數有倍數關係。

時間複雜度:
O(1)

空間複雜度:
O(1)

程式碼:

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 (Medium)

限制 :

  • 1 <= numBottles <= 100
  • 1 <= numExchange <= 100

Solution

這題其實就是依照喝、交換、換回來的瓶子做運算,跟著他給的步驟就好。

時間複雜度:
O(N)

空間複雜度:
O(1)

程式碼:

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(Medium)

限制 :

  • 104

時間複雜度:
O()

空間複雜度:
O()

程式碼:

4(Hard)

限制 :

  • 104

時間複雜度:
O()

空間複雜度:
O()

程式碼: