# [Java][BinarySearch][LeetCode] Capacity To Ship Packages Within D Days #1011

A conveyor belt has packages that must be shipped from one port to another within `days` days.

The `ith` package on the conveyor belt has a weight of `weights[i]`. Each day, we load the ship with packages on the conveyor belt (in the order given by `weights`). …

# [Java][BinarySearch][LeetCode] Single Element in a Sorted Array #540

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

Return the single element that appears only once.

Your solution must run in `O(log n)` time and `O(1)` space.

Example 1:

`Input: nums = [1,1,2,3,3,4,4,8,8]Output: 2`

As mentioned of problem, except for one element which appears exactly once, others are twice. It’s meant, if we separate from middle, single element have to at odd-side. Because value of middle have to equal value of middle+1 or middle-1.

# [Java][BinarySearch][LeetCode] Search in Rotated Sorted Array II #81

There is an integer array `nums` sorted in non-decreasing order (not necessarily with distinct values).

Before being passed to your function, `nums` is rotated at an unknown pivot index `k` (`0 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed)…

# [Java][BinarySearch][LeetCode] Peak Index In A Mountain Array #852

Let’s call an array `arr` a mountain if the following properties hold:

• `arr.length >= 3`
• There exists some `i` with `0 < i < arr.length - 1` such that:
• `arr[0] < arr[1] < ... arr[i-1] < arr[i]`
• `arr[i] > arr[i+1] > ... > arr[arr.length - 1]`

Given an integer array…

# [Java][BinarySearch][LeetCode] Sqrt(x) #69

Given a non-negative integer `x`, compute and return the square root of `x`.

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

Note: You are not allowed to use any built-in exponent function or operator, such as…

# [Java][Arrays][LeetCode] Longest Word in Dictionary #720

Given an array of strings `words` representing an English Dictionary, return the longest word in `words` that can be built one character at a time by other words in `words`.

If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.

Example 1:

`Input: words = ["w","wo","wor","worl","world"]Output: "world"Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".`

I knew that the best solution is “Tire”. But, I used Hash first and that is method of brute force. I will used “Tire” later.
Sort first. check the word with hashset one by one. If those words are include in hashset, I will compare length of result and the word, and replace result to longer word.

# [Java][TwoPoints][LeetCode] Longest Word in Dictionary through Deleting #524

Given a string `s` and a string array `dictionary`, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

`Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]Output: "apple"`

Because return the longest word with the smallest lexicographical order. So, I need sort dictionary first. Each of two arrays have a pointer. Base on dictionary array and check s string one by one to find the same as char of dictionary. Record the string of dictionary, if satisfaction condition. I compare and get the longest word of dictionary, then return it.

# [Java][TwpPoints][LeetCode] Valid Palindrome II #680

Given a string `s`, return `true` if the `s` can be palindrome after deleting at most one character from it.

Example 1:

`Input: s = "aba"Output: true`

Right point start from 0 and shift to left. Left point start from length-1 and shift to right. Until find different character of right and left point. Check remain string is it a palindrome string, when right point shift to left 1 step or left point shift to right 1 step. I need check two conditions. If both true, return true. Otherwise, return false.

# [Java][TwoPoints][LeetCode] Sum of Square Numbers #633

Given a non-negative integer `c`, decide whether there're two integers `a` and `b` such that `a2 + b2 = c`.

Example 1:

`Input: c = 5Output: trueExplanation: 1 * 1 + 2 * 2 = 5`

I suppose first this question is extension of Two Sum. Then I used HashSet to solve this question. But… The RunTime is too bad. So, I change my thinking to Two Points. small point is start from 0 and shift to right, large point is start from squrt(c) and shift to left. If sum of square of small point and square of large point lesser equal c, return true. If lesser than c, small point shift to right. If larger than c, large point shift to left.

# [Java][TwoPoints][LeetCode] Merge Sorted Array #88

You are given two integer arrays `nums1` and `nums2`, sorted in non-decreasing order, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.

Merge `nums1` and `nums2` into a single array sorted in non-decreasing order.

The final sorted array should not be returned by…

## SP Hou

Get the Medium app