Recent JPMorgan Chase OA has begun to be distributed in batches, and the platform is still the familiar HackerRank. The overall difficulty is not high, but the rhythm requirements are very tight. It is a typical screening question that "you will get points if you know it, but you will get stuck if you don't know it". This time I share a very high-frequency combination: sliding window + greedy (heap). Basically, many people will encounter similar versions.
Topic 1: Highly Profitable Months
Question analysis
Question meaning: Given an array of stock prices StockPrices And parameters K, find all lengths K The continuous sub-intervals satisfy the strict increasing stock price within the interval, and count the number of such intervals.
- Special circumstances: when
K=1When, all single elements meet the conditions, the length of the array is returned directlyN. - Core judgment: For each length of
KWindow to check whether it satisfiesStockPrices[i] < stockPrices[i+1] < ... < stockPrices[i+k-1].
Problem-solving ideas:
- First preprocess the array to generate an "increasing tag array":
Inc[i] = 1If and only ifStockPrices[i] < stockPrices[i+1], otherwise0. - The length is
KA strictly increasing interval of , which is equivalent to having a length ofK-1Continuous1. - Statistical markers consecutive in array
K-1Indivual1The number of windows is the answer.
Example:
StockPrices = [5, 3, 5, 7, 8],K=3
- Tag array:
[0, 1, 1, 1](because5>3,3<5,5<7,7<8) - Find continuous
2Indivual1Window:[1,1](Position 1-2),[1,1](Positions 2-3), total 2 , consistent with the example results.
Question 2: Array Reduction 1
Question analysis
Question meaning: Given an array of integers Num, each operation selects two different elements, deletes them and adds the sum to the end of the array, the operation cost is the sum of the two numbers. Find the minimum total cost of reducing an array to only 1 element.
Problem-solving ideas:
This is the classic Huffman Coding problem, which is equivalent to the merging fruit problem:
- Each time the two smallest elements are merged, the total cost is the smallest.
- Principle: Smaller numbers will be included in the total multiple times. Merging decimals first can allow their sum to be accumulated less times, thus minimizing the total cost.
- Implementation: Use a minimum heap (small root heap), pop out the sum of the two minimum elements each time, put the sum back into the heap, and accumulate the cost until there is only one element left in the heap.
Example:
Num = [4,6,8]
- Heap initialization:
[4,6,8] - First merge:
4+6=10,cost10, the heap becomes[8,10] - Second merge:
8+10=18,cost18, the total cost10+18=28, consistent with the sample minimum result.
A little sharing about JPMorgan Chase OA
This time I was able to pass JPMorgan Chase OA smoothly and quickly. To be honest, a large part of the reason was that I searched in advance. ProgramHelp Help with targeted preparations. Their team configuration is indeed quite hard-core: there are a total of 7 core people, all with top backgrounds, including backgrounds in prestigious schools, and there are also engineers working at first-tier manufacturers such as Amazon, Google and Alibaba.
In form, it is remote assistance, which will not affect your normal operation. It is overall relatively low-key and safe. If you are currently preparing for OA/written examinations such as JPMorgan Chase, Goldman Sachs, or Amazon, it will indeed be much more stable if you have someone to help you.