Contains Duplicate Link to original Problem on LeetCode
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
Company: Amazon
Solution (Using Sorting):
Time Complexity: O(n log n) Space Complexity: O(1)
Top Programming Questions [Updated]
Roman to Integer Link to original Problem on LeetCode
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVIIi, which is XX + V + II.
Majority Element Link to original Problem on LeetCode
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
Company: Yahoo, Google, Amazon, Microsoft
Solution (Using Hashing - 2 pass):
Subsets Link to original Problem on LeetCode
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Company: Amazon, Microsoft
Solution (Using Iteration):
Time Complexity: Space Complexity:
class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ # Let's take an example, say we have nums = [1, 2, 3] # Initially we have an empty subset in the result i.
Top K Frequent Elements Link to original Problem on LeetCode
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2]
Example 2:
Input: nums = [1], k = 1 Output: [1]
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.