---
# System prepended metadata

title: BiWeekly Contest 110
tags: [BiWeekly Contest]

---

###### tags: `BiWeekly Contest`
# BiWeekly Contest 110

## [2806. Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase/) (<font color=#00B8A3>Easy</font>)

限制 :

<ul>
<li><code>0 <= purchaseAmount <= 100</code></li>
</ul>

### Solution

#### 時間複雜度: $O(1)$

#### 空間複雜度: $O(1)$

程式碼:

```c++=
class Solution {
public:
    int accountBalanceAfterPurchase(int purchaseAmount) {
        purchaseAmount = ((purchaseAmount+5)/10) *10;
        return 100 - purchaseAmount;
    }
};
```

## [2807. Insert Greatest Common Divisors in Linked List](https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list) (<font color=#FFC011>Medium</font>)

限制 :

<ul>
<li><code>The number of nodes in the list is in the range [1, 5000].
</code></li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
### Solution

#### 時間複雜度: $O(n)$

#### 空間複雜度: $O(1)$

程式碼:

```c++=
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:

    int calculateGCD(int a, int b)
    {
        if(b)
        {
            return calculateGCD(b, a%b);
        }
        else
        return a;
    }

    ListNode* insertGreatestCommonDivisors(ListNode* head) {
        ListNode *ptr = head;
        ListNode *next_ptr = ptr -> next;
        while(next_ptr != nullptr)
        {
            int a = (ptr -> val), b = (next_ptr -> val);
            int val = calculateGCD(a, b);
            ListNode *temp = new ListNode(val);
            temp -> next = next_ptr;
            ptr -> next = temp;
            ptr = next_ptr;
            next_ptr = next_ptr -> next;
        }
        return head;
    }
};
```

## [2808. Minimum Seconds to Equalize a Circular Array](https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array)(<font color=#FFC011>Medium</font>)

限制 :

<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>

### Solution

- 這題我卡在這句 :
    <code>For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n] </code>
    這句的意思應該是將 nums[i] 不變、換成左邊或右邊的元素。
- 所以，照理來說就會是要去算要怎麼樣可以**用最快的速度將所有元素都變成同一個元素**。也就是說，假設例題1的這種情況，就會有 1 跟 2 兩個元素，要嘛就是將所有元素都變成 1 ，要嘛就是所有都變成 2。

- 參考資料 : [【每日一题】LeetCode 2808. Minimum Seconds to Equalize a Circular Array](https://www.youtube.com/live/aiUYZ7Kn5K8?feature=share)

#### 時間複雜度: $O(n^2)$

#### 空間複雜度: $O(n)$

程式碼:

```c++=
class Solution {
public:
    int minimumSeconds(vector<int>& nums) {
        unordered_map<int, vector<int>> Map;
        for(int i=0;i<nums.size();i++)
        {
            Map[nums[i]].push_back(i);
        }
        int n = nums.size();

        int result = INT_MAX;
        for(auto& [key, arr]:Map)
        {
            arr.push_back(n + arr[0]);
            int d = 0;
            for(int i=0;i<arr.size()-1; i++)
            {
                d = max(d, (arr[i+1]-arr[i])/2);
            }
            result = min(result, d);
        }
        return result;
    }
};
```

## [2809. Minimum Time to Make Array Sum At Most x](https://leetcode.com/problems/minimum-time-to-make-array-sum-at-most-x)(<font color=#FF375F>Hard</font>)

限制 :

<ul>
<li><code>1 <= nums1.length <= 10<sup>3</sup></code></li>
<li><code>1 <= nums1[i] <= 10<sup>3</sup></code></li>
<li><code>0 <= nums2[i] <= 10<sup>3</sup></code></li>
<li><code>nums1.length == nums2.length
</code></li>
<li><code>0 <= x <= 10<sup>6</sup></code></li>
</ul>

### Solution

#### 時間複雜度: $O()$

#### 空間複雜度: $O()$

程式碼:

```c++=
```
