Week 1: Arrays
1) Check if Numbers Are Ascending in a Sentence
A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9
with no leading zeros, or a word consisting of lowercase English letters.
For example, "a puppy has 2 eyes 4 legs"
is a sentence with seven tokens: "2"
and "4"
are numbers and the other tokens such as "puppy"
are words.
Given a string s
representing a sentence, you need to check if all the numbers in s
are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s
).
Return true
if so, or false
otherwise.
Example 1:
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
Output: true
Explanation: The numbers in s are: 1, 3, 4, 6, 12.
They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
Example 2:
Input: s = "hello world 5 x 5"
Output: false
Explanation: The numbers in s are: 5, 5. They are not strictly increasing.
Constraints:
s
consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.- The tokens in
s
are separated by a single space. - There are at least two numbers in
s
. - Each number in
s
is a positive number less than 100, with no leading zeros. s
contains no leading or trailing spaces.
2) Time Needed to Buy Tickets
There are n
people in a line queuing to buy tickets, where the 0th
person is at the front of the line and the (n - 1)th
person is at the back of the line.
You are given a 0-indexed integer array tickets
of length n
where the number of tickets that the ith
person would like to buy is tickets[i]
.
Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
Return the time taken in seconds for the person at position k
to finish buying tickets.
Example 1:
Input: tickets = [2,3,2,4], k = 2
Output: 7
Explanation:
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1, 3].
- In the second pass, the first two people in the line buy a ticket and then the person at
position 2 buys a ticket: the line becomes [0, 1, 0, 3]. The person at position 2 has
successfully bought 2 tickets and it took 4 + 3 = 7 seconds.
Example 2:
Input: tickets = [5,1,1,1], k = 0
Output: 8
Explanation:
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
Constraints:
1 <= n <= 100
1 <= tickets[i] <= 100
0 <= k < n
3) Best Time to Buy and Sell Stock
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6 - 1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 10^5
0 <= prices[i] <= 10^4
4) Container With Most Water
You are given an integer array height of length n
. There are n
vertical lines drawn such that the two endpoints of the ith
line are (i, 0)
and (i, height[i])
.
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The max area of water is calculated by 7*7
using the vertical lines located at indices '1' and '8'
causing the height and length to be 7.
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
n == height.length
2 <= n <= 10^5
0 <= height[i] <= 10^4