Given an array of integers find the maximum and minimum elements by using minimum comparisons.
Java
LeetCode – Best Time to Buy and Sell Stock III (Java)
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: A transaction is a buy & a sell. You may not engage in multiple transactions at the same time … Read more
LeetCode – Best Time to Buy and Sell Stock II (Java)
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in … Read more
LeetCode – Best Time to Buy and Sell Stock (Java)
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Java Solution Instead of keeping track … Read more
LeetCode – Combination Sum (Java)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , … Read more
LeetCode – Majority Element (Java)
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. (assume that the array is non-empty and the majority element always exist in the array.) Java Solution 1 – Sorting Assuming the majority exists and since the majority always takes more … Read more
LeetCode – Reverse Words in a String (Java)
Given an input string, reverse the string word by word. For example, given s = “the sky is blue”, return “blue is sky the”. Java Solution This problem is pretty straightforward. We first split the string to words array, and then iterate through the array and add each element to a new string. Note: StringBuilder … Read more
LeetCode – Min Stack (Java)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. getMin() — Retrieve the minimum element in the stack. Java Solution To make constant time of … Read more
LeetCode – Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠num[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. You may imagine that num[-1] = num[n] … Read more
LeetCode – Number of Squareful Arrays (Java)
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square. Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i]. Example … Read more
Reverse Double Linked List
Given a double linked list’s head node, reverse the list and return the new head node. Java Solution /* * For your reference: * * DoublyLinkedListNode { * int data; * DoublyLinkedListNode next; * DoublyLinkedListNode prev; * } * */ static DoublyLinkedListNode reverse(DoublyLinkedListNode head) { DoublyLinkedListNode p = head; DoublyLinkedListNode newHead = head; while(p!=null){ … Read more
LeetCode – Find Minimum in Rotated Sorted Array (Java)
Suppose a sorted array 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). Find the minimum element.You may assume no duplicate exists in the array. Analysis This problem is a binary search and the key is breaking the … Read more
LeetCode – Subarray Sum Equals K (Java)
Given an array of integers and an integer k, find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note that empty array is not considered as a subarray. Java Solution The sum problems can often be solved by using a hash map … Read more
LeetCode – Partition to K Equal Sum Subsets (Java)
Given an array of integers nums and a positive integer k, find whether it’s possible to divide this array into k non-empty subsets whose sums are all equal. Example 1: Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4 Output: True Explanation: It’s possible to divide it into 4 subsets (5), … Read more