In this repository you can find my solutions to assorted computer science problems.
- Easy
- 485 - Max Consecutive Ones
- 387 - First Unique Character in a String
- 350 - Intersection of Two Arrays II
- 344 - Reverse String
- 283 - Move Zeroes
- 278 - First Bad Version
- 242 - Valid Anagram
- 217 - Contains Duplicate
- 206 - Reverse Linked List
- 189 - Rotate Array
- 136 - Single Number
- 122 - Best Time To Buy And Sell Stock II
- 104 - Maximum Depth of Binary Tree
- 66 - Plus One
- 27 - Remove Element
- 26 - Remove Duplicates From Sorted Array
- 20 - Valid Parentheses
- 7 - Reverse Integer
- 1 - Two Sum
- 1464 - Maximum Product of Two Elements in an Array
- Medium
- 994 - Rotting Oranges
- 347 - Top K Frequent Elements
- 287 - Find the Duplicate Number
- 240 - Search a 2D Matrix II
- 162 - Find Peak Element
- 75 - Sort Colors
- 74 - Search a 2D Matrix
- 73 - Set Matrix Zeroes
- 56 - Merge Intervals
- 49 - Group Anagrams
- 48 - Rotate Image
- 36 - Valid Sudoku
- 34 - Find First and Last Position of Element in Sorted Array
- 33 - Search in Rotated Sorted Array
- 11 - Container With Most Water
- 6 - ZigZag Conversion
- 3 - Longest Substring Without Repeating Characters
- 451 - Sort Characters By Frequency
- 215 - Kth Largest Element in an Array
- 15 - 3Sum
- 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.
- 7 - Reverse Integer, from leetCode
Given a 32-bit signed integer, reverse digits of an integer.
- 1 - Two Sum, from leetCode
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.
- 994 - Rotting Oranges, from leetCode
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.
- 75 - Sort Colors, from leetCode
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.
- 73 - Set Matrix Zeroes, from leetCode
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
- 56 - Merge Intervals, from leetCode
Given a collection of intervals, merge all overlapping intervals.
- 49 - Group Anagrams, from leetCode
Given an array of strings, group anagrams together.
- 48 - Rotate Image, from leetCode
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.
- 36 - Valid Sudoku, from leetCode
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.
- 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
- 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.
- 6 - ZigZag Conversion, from leetCode
Receives a string and writes it in zig zag pattern across a given number of rows (input parameter).
- 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.
- 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.
- 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.
- 15 - 3Sum, from leetCode
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] ]

