A: 35. Search Insert Position#
Given a sorted array and a target value, find the target value in the array and return its index. If the target value is not found in the array, return the index where it would be inserted in order.
You must use an algorithm with a time complexity of O(log n).
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Constraints:
- 1 <= nums.length <= 10^4
- -10^4 <= nums[i] <= 10^4
- nums is a sorted array of distinct integers.
- -10^4 <= target <= 10^4
function searchInsert(nums: number[], target: number): number {
let left = 0
let right = nums.length - 1
if (target <= nums[0]) {
return left
}
if (target > nums[right]) {
return right + 1
}
let mid = Math.floor((left + right) / 2)
while (left < right - 1) {
const midValue = nums[mid]
if (midValue === target) {
return mid
}
if (midValue < target) {
left = mid
} else {
right = mid
}
mid = Math.floor((left + right) / 2)
}
return right
}
Submission result:
65/65 cases passed (64 ms)
Your runtime beats 72.07 % of typescript submissions
Your memory usage beats 52.45 % of typescript submissions (43.7 MB)
Seeing that the problem requires an algorithm with a time complexity of O(log n), it can be inferred that binary search is needed. I was able to start faster this time because I just did a binary search problem a few days ago. I spent some time thinking about the boundary conditions when writing. First, since mid
is always the middle value between left
and right
, it cannot cover the case where it exceeds this boundary, so I first checked if the target would exceed the maximum boundary. If it does, return the boundary case. Then, left
and right
will continue to shrink until left === right - 1
, when the interval can no longer be compressed, so the loop needs to be terminated. Finally, because we have already determined in advance that the value will not exceed the range of left
and right
, if no item is hit during the intermediate process, the range right
is the correct insertion position.
R: How to Engineer the Perfect Stable Diffusion Prompt#
From now on, articles will be summarized using the QA method, which makes it easier to grasp the content of the article and is also more convenient for review in the future.
Q: Why is a high-quality Prompt important for Stable Diffusion?
It provides the model with a framework for instructions and operations, which affects the execution of the model and thus the quality of the results. Mastering the techniques of Prompt Engineering allows artists to explore artistic paths, experiment with artistic styles, and even define the artistic identity of assets in the field of AI art.
Q: What are the steps to create an effective Stable Diffusion Prompt?
- Define the type of image
Whether it is a photograph, sketch, 3D render, or painting, defining the type can serve as a good foundation. - Define the subject
Clearly define the subject and its attributes. The subject can be an object, a person, a landscape, or the core element in other images. - Define the scene
Define the environment in which the subject is located, such as background details, weather conditions, time of day, etc. The clearer the description, the better the effect. - Define the style
For example, realism, surrealism, impressionism, or the style of a specific artist. - Specify lighting details
Use specific words to describe the lighting conditions, such as backlight, candlelight, or natural lighting; describe the level of detail, such as highly detailed, grainy, or smooth. - Declare the composition
Define the layout of elements in the image, such as aerial view, close-up, wide-angle, or other composition methods. - Define the color scheme
If there are color preferences, they should also be defined, such as a triadic color scheme or a washed color scheme. - Avoid excessive complexity
Overly complex or contradictory instructions may prevent the model from producing high-quality results.
T: SeamlessM4T - A Multilingual, Multimodal Model for All#
Meta AI has recently open-sourced a multimodal model called SeamlessM4T, which can perform over 100 tasks including speech-to-text, speech-to-speech, text-to-text translation, and speech recognition in multiple languages.
S: Excerpt from "My Reading Experience" by Li Xiaolai#
I previously excerpted a sentence from an article by Li Xiaolai here, and I feel that the article by Li Xiaolai is really well-written. Today, I remembered another sentence from that article that resonated with me:
Reading academic literature in bulk is like leaving the shelter of one's parents and living independently. At first, there may be a bit of fear, but soon you start to pity your past self. How silly you were in the past, always relying on others for clothes and food, and having nothing if they were not provided. After undergraduate education, in theory, most of your time should be spent reading academic literature. Unfortunately, it seems that in our country, this concept simply does not exist. Academic research is translated into popular reading materials, and this time lag is too severe.
Recently, I have also read some papers on large language models, and on the one hand, I have experienced the feeling that Li Xiaolai described as "a bit scared at first, but soon realizing that it should have been like this". Indeed, papers are a fast and effective way to understand the cutting-edge of a field that one cares about, and reading papers is a habit worth cultivating.