# 704. Binary search

One question every day on September 6, 2021. I was preached by the teacher on the first day of school. It's so annoying

## Title Description

Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search the target in nums. If the target value exists, return the subscript, otherwise return - 1.

Example 1:

Input: num = [- 1,0,3,5,9,12], target = 9

Output: 4

Explanation: 9 appears in nums and the subscript is 4

Example 2:

Input: num = [- 1,0,3,5,9,12], target = 2

Output: - 1

Explanation: 2 does not exist in nums, so - 1 is returned

Tips:

You can assume that all elements in nums are not repeated.

n will be between [1, 10000].

Each element of num will be between [- 9999, 9999].

Source: LeetCode

Link: https://leetcode-cn.com/problems/binary-search

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

## thinking

Write a binary template I usually write. I learned from brother weiwei. It is basically applicable to any situation

After understanding, I feel better than those less than or equal to. I can adjust the size of the left and right boundaries as needed

class Solution { public int search(int[] nums, int target) { //Binary search int left = 0; int right = nums.length - 1; while(left < right){ int mid = (right - left) / 2 + left; if(nums[mid] < target) left = mid + 1; else right = mid; } return nums[left] == target ? left : -1; } }

If you want to write fast after you can, you must know how to call the API:

This method returns the subscript of the target value when the searched element can be found

When it cannot be found, it returns (- (insertion point) - 1), and the negative insertion position is minus 1

class Solution { public int search(int[] nums, int target) { //Binary search int idx = Arrays.binarySearch(nums, target); return idx < 0 ? -1 : (nums[idx] == target ? idx : -1); } }

# 1221. Split balanced string

2021.9.7 one question per day

## Title Description

In a balanced string, the number of 'L' and 'R' characters is the same.

Give you a balanced string s, please divide it into as many balanced strings as possible.

Note: each split string must be a balanced string.

Returns the maximum number of balanced strings that can be split.

Example 1:

Input: s = "rlrrllrll"

Output: 4

Explanation: s can be divided into "RL", "RRLL", "RL" and "RL". Each substring contains the same number of 'L' and 'R'.

Example 2:

Input: s = "rlllrrrlr"

Output: 3

Explanation: s can be divided into "RL", "LLLRRR" and "LR". Each substring contains the same number of 'L' and 'R'.

Example 3:

Enter: s = "llrrrr"

Output: 1

Explanation: s can only keep the original "llrrrr"

Example 4:

Input: s = "rlrrllrll"

Output: 2

Explanation: s can be divided into "RL" and "rrrlllrll". Each substring contains the same number of 'L' and 'R'.

Tips:

1 <= s.length <= 1000

s[i] = 'L' or 'R'

s is a balanced string

Source: LeetCode

Link: https://leetcode-cn.com/problems/split-a-string-in-balanced-strings

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

## thinking

greedy

class Solution { public int balancedStringSplit(String s) { int l = s.length(); int countr = 0; int countl = 0; int cut = 0; for(int i = 0; i < l; i++){ if(s.charAt(i) == 'R') countr++; else countl++; if(countr == countl){ cut++; countl = 0; countr = 0; } } return cut; } }

# 502. IPO

2021.9.8 one question per day

## Title Description

Suppose LeetCode is about to start IPO. In order to sell shares to venture capital companies at a higher price, Li Kou hopes to carry out some projects to increase its capital before IPO. Due to limited resources, it can only complete up to k different projects before IPO. Help force buckle design the way to get the maximum total capital after completing up to k different projects.

Here are n items for you. For each project I, it has a net profit profits[i], and the minimum capital[i] required to start the project.

Initially, your capital was w. When you complete a project, you will get a net profit, and the profit will be added to your total capital.

In summary, select a list of up to k different projects from a given project to maximize the final capital and output the maximum capital available.

The answer is guaranteed to be in the range of 32-bit signed integers.

Example 1:

Input: k = 2, w = 0, profiles = [1,2,3], capital = [0,1,1]

Output: 4

Explanation:

Since your initial capital is 0, you can only start from project 0.

After completion, you will get a profit of 1 and your total capital will become 1.

At this point, you can choose to start project 1 or 2.

Since you can select up to two projects, you need to complete project 2 to get the maximum capital.

Therefore, the output of the last maximized capital is 0 + 1 + 3 = 4.

Example 2:

Input: k = 3, w = 0, profiles = [1,2,3], capital = [0,1,2]

Output: 6

Tips:

1 <= k <= 10^5

0 <= w <= 10^9

n == profits.length

n == capital.length

1 <= n <= 10^5

0 <= profits[i] <= 10^4

0 <= capital[i] <= 10^9

Source: LeetCode

Link: https://leetcode-cn.com/problems/ipo

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

## thinking

This problem needs to be noted that there is no need to subtract the start-up capital, which is not a difficulty

Look at the notes for ideas, more than 52, and see where you can optimize it

class Solution { public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) { //The project should be completed depends on the total capital //It can also be regarded as greedy to start the project with the maximum profit under the current capital every time //Obviously, it needs to be sorted, but how can it ensure that it is nlogn in the end //First, change all items into a binary form, put them in the list, and sort them by start-up funds //Then put the items that can be started into the heap and sort them by line, according to the profit size //Then take out the most profitable items each time //Then put the projects that can be carried out under the current capital, and so on int l = profits.length; List<int[]> list = new ArrayList<>(); for(int i = 0; i < l; i++){ list.add(new int[]{profits[i], capital[i]}); } Collections.sort(list, (a, b) -> (a[1] - b[1])); PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> (b - a)); int idx = 0; while(k-- > 0){ //Put all the items that can be started into the list while(idx < l && list.get(idx)[1] <= w){ pq.offer(list.get(idx)[0]); idx++; } if(!pq.isEmpty()){ w += pq.poll(); }else{ break; } } return w; } }