Overflowing Buckets

Hello everyone 👋, Today I want to share with you a problem I’ve encountered along my journey. The problem is fairly simple: You have a finite number of water units. You have a finite number of buckets. You need to distribute the water units into the buckets. Each bucket has a maximum capacity. When a bucket is full, the water overflows into the next two buckets by splitting evenly. You need to print the number of the buckets that are full. The following diagram illustrates the problem: ...

June 15, 2023 · 3 min · Denis Nutiu

LeetCode: Removing Stars From a String & Simplify Path

Hello 👋, I’ve started doing some LeetCode dailies with Paula and I’d like to share our solutions for the last two problems. https://leetcode.com/problems/removing-stars-from-a-string/ https://leetcode.com/problems/simplify-path/ I also want to warmly welcome the new nuculabs.dev subscribers! Thank you for subscribing! If you want to attempt the problems by yourself then stop reading otherwise you’ll be spoiled with solutions. Removing Stars From a String In this problem you are given a string with stars ‘*’ and when you encounter a start you’ll need to remove the start along with the character to it’s left. ...

April 12, 2023 · 2 min · Denis Nuțiu, Paula Rusti

LeetCode: Boats to Save People

Introduction Hello everyone! 👋 I’ve been practicing with some Leetcode daily challenges lately and since I’ve recently moved to Substack I wanted to make my first post here 😁 Boats to Save People The problem Boats to Save People is solved using a Greedy approach. Given a number of people and their weights, along with a carry limit for the boat. You must find the number of boats needed to carry the people. A boat can carry a maximum of two people at a time. ...

April 8, 2023 · 2 min · Denis Nuțiu, Paula Rusti

LeetCode: Reverse Linked List Solution and Explanation

Hi, In this article I will explain my solution for the following LeetCode problem: Reverse Linked List. If you’re interested in solving this problem then please try to spend at least one hour or more on it before looking at my solution. To help you solve this problem I’ve created the following table: Current Prev 1 NULL 2 1 3 2 NULL 3 Think about how you can use this table to write an iterative or recursive algorithm that reverses the linked list. ...

November 17, 2020 · 3 min · Denis Nuțiu

LeetCode: Flood Fill

Hello, Here’s my solution for the flood fill problem, found on LeetCode. If you want me to write about certain topics please let me know in the comments, thank you! Link to the problem: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 """ An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image. To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor. At the end, return the modified image. """ from typing import List, Tuple class Solution: def __init__(self): self.visited = set() def _set_pixel(self, image: List[List[int]], point: Tuple[int, int], value: int): try: image[point[0]][point[1]] = value except IndexError: pass def _get_pixel(self, image: List[List[int]], point: Tuple[int, int]): if point[0] < 0 or point[1] < 0: return None try: return image[point[0]][point[1]] except IndexError: return None def _floodFill(self, image: List[List[int]], point: Tuple[int, int], color: int, newColor: int) -> List[List[int]]: pixel = self._get_pixel(image, point) if pixel is not None and pixel == color and point not in self.visited: self.visited.add(point) self._set_pixel(image, point, newColor) self._floodFill(image, (point[0], point[1] + 1), color, newColor) self._floodFill(image, (point[0], point[1] - 1), color, newColor) self._floodFill(image, (point[0] + 1, point[1]), color, newColor) self._floodFill(image, (point[0] - 1, point[1]), color, newColor) def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]: point = (sr, sc) pixel = self._get_pixel(image, point) self.visited = set() if pixel is not None: self._floodFill(image, point, pixel, newColor) return image if __name__ == '__main__': s = Solution() out = s.floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2) print("Output", out) assert out == [[2,2,2],[2,2,0],[2,0,1]] out = s.floodFill([[0,0,0],[0,0,0]], 0, 0, 2) print("Output", out) assert out == [[2,2,2],[2,2,2]] out = s.floodFill([[0,0,0],[0,1,1]], 1, 1, 1) print("Output", out) assert out == [[0, 0, 0], [0, 1, 1]]

July 16, 2020 · 2 min · Denis Nuțiu

LeetCode: Find The Town Judge

Hello In this article I will present you my Python3 solution for the following problem which I found on LeetCode: find-the-town-judge. Note: The solution is on the second page, so you won’t get spoiled if you want to attempt to solve the problem by yourself. Have fun! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 from collections import defaultdict from typing import List """ In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b. If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1. """ class Solution: def _filterForJudge(self, N, trust_dict): no_trustees = None for i in range(1, N + 1): person_trusted = trust_dict.get(i, set()) if len(person_trusted) == 0: if no_trustees is None: no_trustees = i else: return None return no_trustees def findJudge(self, N: int, trust: List[List[int]]) -> int: trust_dict = defaultdict(set) for i in trust: trust_dict[i[0]].add(i[1]) # The town judge trusts nobody. no_trustee = self._filterForJudge(N, trust_dict) if no_trustee: # Everybody trusts the town judge. people_who_trust = 0 for p in trust_dict.items(): # Check if the person trusts the town judge. if no_trustee in p[1]: people_who_trust += 1 if people_who_trust == N-1: return no_trustee return -1 if __name__ == '__main__': s = Solution() print(s.findJudge(2, [[1,2]])) print(s.findJudge(3, [[1,3],[2,3]])) print(s.findJudge(3, [[1,3],[2,3],[3,1]])) print(s.findJudge(3, [[1,2],[2,3]])) print(s.findJudge(4, [[1,3],[1,4],[2,3],[2,4],[4,3]])) Thanks for reading! ...

May 11, 2020 · 2 min · Denis Nuțiu

LeetCode: Arrays 101: Inserting Items Into an Array

Hello, Here are my solutions for the second part of the card: Arrays 101, from LeetCode. Duplicate Zeroes Given an array of integers, remove duplicate zeroes and shift the remaining elements. 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def duplicateZeros(self, arr: List[int]) -> None: """ Do not return anything, modify arr in-place instead. """ index = 0 arr_length = len(arr) while index < arr_length: if arr[index] == 0: arr.insert(index, 0) arr.pop() index += 1 index += 1 Merge Sorted Array Given two sorted arrays, merge them together into nums1. ...

April 30, 2020 · 2 min · Denis Nuțiu

LeetCode: Arrays 101: Introduction &#8211; Solutions

Hello, LeetCode is a good place to practice programming by solving problems of all levels! After you solve a problem, you’ll get access to all submitted solutions, sorted by time and memory usage. That’s a nice way to compare your code to others and see what you did well and where you can improve. Here’s my solutions for the Arrays 101: Introduction card: Max Consecutive Ones 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: max_counter = 0 counter = 0 for i in nums: if i == 1: counter += 1 else: if counter > max_counter: max_counter = counter counter = 0 if counter > max_counter: max_counter = counter return max_counter Find Numbers with Even Number of Digits 1 2 3 4 5 6 7 8 9 10 11 12 class Solution: @staticmethod def is_even(value): return len(str(value)) % 2 == 0 def findNumbers(self, nums: List[int]) -> int: return_value = 0 for number in nums: if self.is_even(number): return_value += 1 return return_value Squares of a Sorted Array When writing code like this be careful because you’re altering the A list in place. ...

April 26, 2020 · 2 min · Denis Nuțiu

LeetCode: Add Two Numbers Python 3 iterative solution

[Problem Link] Hello, Here’s my solution for the add two numbers problem on LeetCode. Example: 1 2 3 <strong>Input:</strong> (2 -> 4 -> 3) + (5 -> 6 -> 4) <strong>Output:</strong> 7 -> 0 -> 8 <strong>Explanation:</strong> 342 + 465 = 807. When doing problems on websites such as LeetCode, I like to code in my own IDE, that’s why you’ll see some additional helper functions besides the solution code. ...

March 15, 2020 · 2 min · Denis Nuțiu