Published in

The Research Nest

# Solving The Leetcode Weekly Contest With ChatGPT

## How well does it do?

`def pivot_integer(n: int) -> int:    # Compute the sum of all elements from 1 to n.    total_sum = n * (n + 1) // 2    # Iterate over the integers from 1 to n.    for x in range(1, n + 1):        # Compute the sum of all elements from 1 to x.        left_sum = x * (x + 1) // 2        # Compute the sum of all elements from x to n.        right_sum = total_sum - left_sum        # Check if the sum of the elements from 1 to x equals the sum of the elements from x to n.        if left_sum == right_sum:            return x    # If we reach this point, it means that we didn't find any integer x that satisfies the given condition.    return -1`

## Verdict

`def minCharactersToAppend(s: str, t: str) -> int:    count = 0    i = 0    j = 0    while i < len(s) and j < len(t):        if s[i] == t[j]:            i += 1            j += 1        else:            i += 1        count = len(t) - j    return count`

## Verdict

`# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def removeNodes(self, head: ListNode) -> ListNode:        # Store all nodes' values in an array        arr = []        curr = head        while curr:            arr.append(curr.val)            curr = curr.next                # Iterate over the array from right to left, and remove each element that is smaller than the maximum value seen so far        mx = -1        i = len(arr) - 1        while i >= 0:            if arr[i] > mx:                mx = arr[i]            else:                arr.pop(i)            i -= 1                # Construct a new linked list from the modified array and return the head of the linked list        dummy = curr = ListNode(-1)        for x in arr:            curr.next = ListNode(x)            curr = curr.next        return dummy.next`

## Verdict

`def count_medians(nums, k):    n = len(nums)    count = 0    # Iterate over all possible subarrays of nums    for i in range(n):        for j in range(i+1, n+1):            # Sort the current subarray in ascending order            subarray = sorted(nums[i:j])            # Find the median of the current subarray            if len(subarray) % 2 == 0:                median = (subarray[len(subarray)//2 - 1] + subarray[len(subarray)//2]) / 2            else:                median = subarray[len(subarray)//2]            # Increment the counter if the median is equal to k            if median == k:                count += 1    return count`

## Verdict

`# Find the median of the current subarrayif len(subarray) % 2 == 0:    median = subarray[len(subarray)//2 - 1]`

# Post Credits

--

--

## More from The Research Nest

Empowering humanity with exclusive insights.

## Get the Medium app

Tech 👨‍💻 | Life 🌱 | Careers 👔 | Poetry 🖊️