From 40c6d6d1e095f2001c9ae11331794173e05ac9b2 Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Tue, 8 Aug 2023 15:46:16 +0530 Subject: [PATCH 1/2] Create 1675-minimize-deviation-in-array.cpp --- cpp/1675-minimize-deviation-in-array.cpp | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cpp/1675-minimize-deviation-in-array.cpp diff --git a/cpp/1675-minimize-deviation-in-array.cpp b/cpp/1675-minimize-deviation-in-array.cpp new file mode 100644 index 000000000..f245d863e --- /dev/null +++ b/cpp/1675-minimize-deviation-in-array.cpp @@ -0,0 +1,43 @@ +/* + You are given an array nums of n positive integers. + + You can perform two types of operations on any element of the array any number of times: + + If the element is even, divide it by 2. + For example, if the array is [1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2]. + If the element is odd, multiply it by 2. + For example, if the array is [1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4]. + + The deviation of the array is the maximum difference between any two elements in the array. + +Return the minimum deviation the array can have after performing some number of operations. + Ex. Input: nums = [1,2,3,4] + Output: 1 + Explanation: You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1. + + Time : O(N); + Space : O(N); +*/ + +class Solution { +public: + int minimumDeviation(vector& nums) { + priority_queue pq; + int minimum = INT_MAX; + for(auto i : nums) { + if(i & 1) + i *= 2; + minimum = min(minimum, i); + pq.push(i); + } + int res = INT_MAX; + while(pq.top() % 2 == 0) { + int val = pq.top(); + res = min(res, val - minimum); + minimum = min(val/2, minimum); + pq.pop(); + pq.push(val/2); + } + return min(res, pq.top() - minimum); + } +}; From 26c4a54b938500296b35a2abb1c222c89030da93 Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Tue, 8 Aug 2023 15:57:35 +0530 Subject: [PATCH 2/2] Create 0024-swap-nodes-in-pairs.c --- c/0024-swap-nodes-in-pairs.c | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 c/0024-swap-nodes-in-pairs.c diff --git a/c/0024-swap-nodes-in-pairs.c b/c/0024-swap-nodes-in-pairs.c new file mode 100644 index 000000000..fc0609ef8 --- /dev/null +++ b/c/0024-swap-nodes-in-pairs.c @@ -0,0 +1,41 @@ +/* + Given a linked list, swap every two adjacent nodes and return its head. + You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) + + Ex. Input: head = [1,2,3,4] + Output: [2,1,4,3] + + Time : O(N) + Space : O(1) +*/ + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* swapPairs(struct ListNode* head) { + if (head == NULL || head->next == NULL) + return head; + + struct ListNode *new_head = head->next; + struct ListNode *prev = NULL; + + while (head != NULL && head->next != NULL) { + struct ListNode *next_pair = head->next->next; + struct ListNode *second = head->next; + + if (prev != NULL) + prev->next = second; + + second->next = head; + head->next = next_pair; + + prev = head; + head = next_pair; + } + + return new_head; +}