Skip to content

Set of coding challenges from assorted sources. Solutions implemented in C++.

Notifications You must be signed in to change notification settings

mi-prata/CodingChallenges

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Coding Challenges

In this repository you can find my solutions to assorted computer science problems.

Easy

  • 485 - Max Consecutive Ones, from leetCode

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]

Output: 3

Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

Note:

The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000


  • 387 - First Unique Charater in a String from leetCode

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.


Given a 32-bit signed integer, reverse digits of an integer.


Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.


  • 1464 - Maximum Product of Two Elements in an Array, from leetCode

Given the array of integers nums, you will choose two different indices i and j of that array.

Return the maximum value of (nums[i]-1) * (nums[j]-1).

Example:

Input: nums = [3,4,5,2]

Output: 12

Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1) * (nums[2]-1) = (4-1) * (5-1) = 12.

Medium

In a given grid, each cell can have one of three values:

  • the value 0 representing an empty cell;
  • the value 1 representing a fresh orange;
  • the value 2 representing a rotten orange.

Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead.


  • 347 - Top K Frequent Elements, from leetCode

Given a non-empty array of integers, return the k most frequent elements.


  • 287 - Find the Duplicate Number, from leetCode

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.


  • 240 - Search a 2D Matrix II, from leetCode

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

  • 162 - Find Peak Element, from leetCode

A peak element is an element that is greater than its neighbors.

Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.


Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.


  • 74 - Search a 2D Matrix, from leetCode

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.


Given a collection of intervals, merge all overlapping intervals.


Given an array of strings, group anagrams together.


You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note: 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.


Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  • Each row must contain the digits 1-9 without repetition.
  • Each column must contain the digits 1-9 without repetition.
  • Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

validSudoku


  • 34 - Find First and Last Position of Element in Sorted Array, from leetCode

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

Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1].

Example: Input: nums = [5,7,7,8,8,10], target = 8. Output: [3,4]


  • 33 - Search in Rotated Sorted Array, from leetCode

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm's runtime complexity must be in the order of O(log n).

Example: Input: nums = [4,5,6,7,0,1,2], target = 0. Output: 4

searchInRotatedSortedArray


  • 11 - Container With Most Water, from leetCode

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

containerWithMostWater


Receives a string and writes it in zig zag pattern across a given number of rows (input parameter).

convertZigZag


  • 3 - Longest Substring Without Repeating Characters, from leetCode

Given a string, find the length of the longest substring without repeating characters. Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

longest_Substring


  • 451 - Sort Characters By Frequency, from leetCode

Given a string, sort it in decreasing order based on the frequency of characters.

Examples:

Input: "tree"

Output: "eert"

Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

sortCharactersByFrequency


  • 215 - Kth Largest Element in an Array, from leetCode

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

findKthLargest


Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]

3Sum

About

Set of coding challenges from assorted sources. Solutions implemented in C++.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages