Sliding Window + Two Pointer in Swift

You can also read the full story on The Swift Nerd blog with the link above.

Problem Statement

There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.

In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken.

Given the integer array cardPoints and the integer k, return the maximum score you can obtain.

Examples

Input: cardPoints =…


Swift + Sieve of Eratosthenes = 🤔 🧠

You can check out the full explanation on The Swift Nerd blog along with other cool posts on the link above.

Problem Description

Count the number of prime numbers less than a non-negative number, n.

Examples

Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
Input: n = 0
Output: 0
Input: n = 1
Output: 0

Constraints

  • 0 <= n <= 5 * 106

Solution

The problem is pretty self evident and is simple to solve but a bit tricky to optimise. A prime number is a number which is only divisible…


Swift + BST + Tree inorder + LinkedList = Leetcode 109 ✅ ✅ ✅

You can check out the full story on The Swift Nerd blog with the link above.

Problem Description

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example 1:


Swift + Dynamic Programming + Greedy = 🔥 🧠 ✅

Follow the link above for more detailed solution and other interesting posts.

Given an array of non-negative integers nums, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps. You can assume that you can always reach the last index.

Examples

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. …


Find Prefix Sums in Swift

Day 3 : May Leetcode Challenge

You can also read the full story on The Swift Nerd blog with the link above.

Problem Description

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Examples

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

Constraints

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6

Solution

This is the easiest problem you would ever get on Leetcode…


Swift + Sets = 🤯

Day 30: April Leetcode Challenge (Leetcode 970)

You can also read the full story on The Swift Nerd blog along with other interesting articles on the link above.

Problem Description

Given three integers x, y, and bound, return a list of all the powerful integers that have a value less than or equal to bound.

An integer is powerful if it can be represented as xi + yj for some integers i >= 0 and j >= 0. You may return the answer in any order. In your answer, each value should occur at most once.

Example 1:

Input: x = 2, y = 3, bound = 10 Output…


Binary Search Magic with Swift (Leetcode 34) ✅ ✅ ✅

Leetcode 34

You can also read the full story on The Swift Nerd blog along with other interesting stories on link above.

Problem Description

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

Follow up: Could you write an algorithm with O(log n) runtime complexity?

Examples

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Input: nums = [], target = 0
Output: [-1,-1]

Constraints

  • 0 <= nums.length <= 105
  • -109 <= nums[i]…


Swift + Dynamic Programming = Leetcode 63 ✅ ✅ ✅

You can also read the full story on The Swift Nerd blog along with other interesting stories by clicking the link above.

Problem Description

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and space is marked as 1 and…


Swift : Day 25 of April Leetcode Challenge ✅ ✅ ✅

You can also check out the discussion on my blog along with other interesting posts.

Problem Statement

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:


April Leetcode Challenge: Day 23 in Swift

You can also read the full story along with other interesting ones on TheSwiftNerd blog with the link above.

Problem Description

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.

Example 1:

Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat…

Varun Rathi

Senior iOS Engineer. Writer at https://theswiftnerd.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store