Final Prices With a Special Discount in a Shop

Easy

You are given an integer array prices, where prices[i] is the price of the ith item in a shop.

The shop has a special discount. If you buy item i, you get a discount equal to prices[j], where j is the first index after i (so j > i) with prices[j] <= prices[i]. If there is no such j, item i gets no discount.

Return an array answer, where answer[i] is what you pay for item i after its discount.

Examples

Example 1

Input:  prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
Example 1: each item and its discountpricediscountyou pay844422624202303

Item 0 costs 8. The first price after it that is 8 or less is 4, so you pay 8 - 4 = 4. Item 1 costs 4: the next price, 6, is too high, so its discount is the 2 after that, and you pay 4 - 2 = 2. Item 2 pays 6 - 2 = 4. Items 3 and 4 have no price after them that is low enough, so they cost full price.

Example 2

Input:  prices = [1,2,3,4,5]
Output: [1,2,3,4,5]

Prices only go up, so no item gets a discount.

Example 3

Input:  prices = [10,1,1,6]
Output: [9,0,1,6]
Example 3: an equal price counts10116discount1100you pay9016The first 1 is discounted by the second 1, so it ends up free.price

Item 1's discount comes from item 2, which costs the same, so item 1 ends up free.

Example 4

Input:  prices = [5,5,5]
Output: [0,0,5]
Example 4555discount550you pay005Each item is discounted by the equal price right after it.price

An equal price counts. Each item is discounted by the one right after it, except the last item, which has nothing after it.

Example 5

Input:  prices = [6,9,7,3]
Output: [3,2,4,3]
Example 5: the discount can be far away6973discount3730you pay3243The 6 skips the 9 and the 7, which both cost more than 6.price

For item 0, the 9 and the 7 are both more than 6, so its discount is the 3 at the end.

Constraints

  • 1 <= prices.length <= 500
  • 1 <= prices[i] <= 1000
TypeScript
Loading editor…
Test cases31
Case 1
prices = [8,4,6,2,3]
Case 2
prices = [1,2,3,4,5]
Case 3
prices = [10,1,1,6]
Case 4
prices = [5,5,5]
Case 5
prices = [6,9,7,3]
Case 6
prices = [1]
Case 7
prices = [1000]
Case 8
prices = [2,1]
Case 9
prices = [1,1]
Case 10
prices = [5,4,3,2,1]
Case 11
prices = [3,3,2,2,1,1]
Case 12
prices = [4,7,1,9,4,8,8,9,4]
Case 13
prices = [1000,1,1000,1,1000]
Case 14
prices = [7,8,9,10,7]
Case 15
prices = [9,8,9,8,9]
Case 16
prices = [2,3,4,5,1,6]
Case 17
prices = [7,9,3,1,9,2]
Case 18
prices = [6,1,2,5,8,3,6,2]
Case 19
prices = [6,5,1,10,7,6,9,2,5,5]
Case 20
prices = [5,6,8,2,5,3,9,9,4,8,5,3]
Case 21
prices = [1,4,10,7,7,2,4,4,5,9,2,1,3,1,4]
Case 22
prices = [10,8,3,3,5,8,9,4,4,5,5,9,5,10,7,4,7,3,7,5]
Case 23
prices = [6,8,9,9,6,9,4,8,4,2,4,2,5,3,4,4,10,3,6,5,4,9,10,3,10]
Case 24
prices = [477,795,725,814,827,560,447,142,956,552,357,673,794,131,814,898,941,765,552,239,837,320,820,747,289,111,760,476,776,933,390,188,436,13,477,476,502,400,558,159]
Case 25
prices = [376,760,667,818,551,591,266,986,583,247,85,191,356,901,278,767,880,48,60,783,994,714,840,483,114,335,872,285,151,989,146,25,916,834,594,20,208,361,427,19,190,702,457,737,458,486,896,634,769,21,229,890,254,186,563,73,422,411,645,850]
Case 26
prices = [961,72,528,286,194,700,251,937,282,89,452,497,935,689,45,694,953,821,41,190,198,928,175,262,957,815,843,915,444,367,189,342,542,992,265,123,886,606,815,336,692,32,951,879,743,608,925,680,730,628,510,105,109,371,26,90,278,247,461,659,185,738,572,164,924,640,321,20,401,721,475,780,836,332,731,881,165,318,431,589,271,213,218,648,147,685,320,251,148,664,352,299,200,303,602,800,260,996,528,975]
Case 27
prices = [2,1,1,1,1,1,3,1,2,3,3,1,2,2,2,2,3,1,1,3,1,1,2,3,3,1,1,2,1,1,3,3,1,2,2,2,3,3,1,3,2,3,1,3,3,1,1,3,1,1,2,2,2,1,3,2,2,1,3,3,2,1,2,1,2,2,1,1,3,3,1,3,2,3,1,2,1,2,2,1,2,1,2,3,3,3,3,3,3,2,2,1,3,2,1,2,2,2,2,3,3,2,1,1,1,1,3,3,1,3,2,3,2,2,1,1,3,3,2,3,3,2,3,2,3,3,1,3,2,2,2,2,2,1,1,1,1,2,1,2,2,3,1,3,2,1,3,2,3,2]
Case 28
prices = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,1]
Case 29
prices = [471,206,487,925,195,856,977,824,880,862,239,134,757,643,522,139,1,217,185,216,552,168,935,237,258,115,226,51,865,674,777,270,79,382,297,688,581,579,882,162,509,224,495,407,390,647,792,33,926,340,282,944,960,196,356,580,146,342,902,772,39,833,662,924,763,775,26,259,115,100,389,844,165,257,710,126,940,933,497,307,587,193,102,623,122,69,519,251,885,573,239,717,61,542,520,948,81,215,960,715,753,126,273,100,894,890,433,884,303,858,928,904,684,701,968,168,400,128,579,776,253,237,977,709,423,150,477,246,758,502,258,116,226,776,633,753,335,853,633,191,967,255,197,89,302,135,703,282,128,24,401,894,716,510,788,934,529,281,286,221,734,917,405,244,944,414,650,798,600,829,437,759,771,920,561,493,399,720,37,71,923,109,240,90,643,596,725,159,815,196,719,753,703,768,227,173,502,816,856,426,352,536,774,225,592,231,575,343,667,105,708,344,519,710,7,814,77,537,430,285,86,27,763,508,398,489,708,387,613,919,749,500,606,167,735,956,661,993,430,348,753,284,589,474,545,77,395,972,987,231,422,849,96,194,409,23,282,359,187,263,855,721,837,730,293,351,758,402,947,128,617,564,707,684,409,50,961,372,15,147,659,667,911,133,971,161,552,356,262,609,540,722,318,166,43,949,282,716,947,889,202,687,730,182,240,847,575,60,726,26,987,536,487,568,658,709,30,208,367,382,437,710,788,936,855,846,665,106,567,152,929,153,669,540,451,800,923,748,531,493,709,895,75,886,759,622,698,85,800,965,858,867,209,706,132,961,491,321,225,510,391,410,674,823,158,631,129,200,637,745,254,927,508,5,807,994,104,956,731,525,220,812,849,133,590,409,580,602,527,294,862,438,685,464,260,946,762,516,826,819,603,407,775,570,251,369,9,763,416,52,398,489,832,961,801,185,848,408,3,524,939,746,528,288,210,629,502,57,28,364,862,999,846,390,60,767,556,163,537,711,647,566,376,757,963,492,862,894,157,79,607,534,482,759,339,957,886,431,522,64,427,72,378,398,398,161,205,535,424,372,977,302,610,98,36,275,423,959,835,817,102,988,816,529,105,323,661,266,85,385,881,187,449,160,913,516,536,97,967,452]
Case 30
prices = [1,2,3,6,10,12,12,13,14,14,20,23,24,24,28,28,30,34,40,40,47,47,48,49,49,50,51,51,57,57,57,64,64,66,68,70,71,71,71,72,74,75,77,78,79,80,86,92,92,93,100,100,102,107,108,119,121,121,122,122,123,126,127,134,134,135,136,148,148,150,150,152,154,155,155,156,160,164,164,166,170,171,171,171,171,177,179,179,182,185,186,191,191,193,193,194,194,195,195,196,198,199,200,215,218,221,222,226,228,229,235,236,238,242,250,252,255,256,256,263,264,265,277,279,281,282,282,284,285,288,292,295,299,299,299,300,301,304,307,308,309,313,315,315,317,318,322,323,323,324,324,325,325,325,335,338,340,341,345,345,351,355,357,359,360,361,362,363,363,366,372,375,380,381,389,390,394,394,396,397,399,404,405,406,407,407,408,413,414,414,415,416,419,422,424,425,429,431,432,437,441,441,441,445,446,446,447,448,450,451,452,458,459,461,462,463,463,464,467,470,472,476,478,481,485,490,490,490,493,494,494,497,500,500,501,505,505,508,510,511,514,516,519,521,522,523,525,526,527,527,530,530,532,533,535,536,542,543,544,547,548,549,549,550,553,557,558,559,559,560,563,563,564,565,567,569,571,571,574,580,582,583,586,588,589,589,590,590,593,594,604,604,605,610,610,610,613,614,615,617,619,627,628,632,632,639,644,648,649,650,651,651,656,657,661,661,663,663,665,665,665,666,666,666,668,668,671,674,674,675,675,677,678,681,683,685,686,695,700,705,707,708,708,710,722,723,725,727,728,730,731,731,734,735,741,741,741,741,742,745,748,751,752,753,753,754,759,762,764,766,768,770,770,772,774,775,779,780,781,782,784,791,792,792,794,794,797,798,802,804,804,806,807,808,809,810,810,816,816,817,817,819,820,821,822,824,824,825,826,829,833,834,835,836,836,836,840,840,841,841,842,842,842,844,845,848,848,849,852,858,861,864,865,866,869,871,877,878,882,883,884,889,890,891,897,903,904,907,908,909,909,909,910,910,912,912,915,921,927,930,930,930,931,932,934,935,936,936,937,937,939,939,941,941,941,942,948,949,955,956,957,960,961,962,963,964,967,971,973,974,975,980,984,986,987,987,989,989,989,989]
Case 31
prices = [453,858,247,781,122,731,143,664,20,507,10,703,172,573,392,873,161,822,236,725,227,508,47,856,175,777,414,978,403,760,270,821,39,733,206,922,237,642,151,500,422,803,153,899,84,745,219,947,358,986,113,912,286,887,74,920,454,978,460,952,433,514,276,928,165,903,107,607,365,942,96,942,327,678,478,931,432,658,220,863,230,584,320,537,126,700,25,579,342,581,466,566,94,585,330,752,160,682,133,770,265,542,203,853,244,757,64,627,22,573,293,838,444,912,118,508,248,963,39,798,165,664,291,782,230,723,276,723,388,946,413,790,101,910,470,957,472,727,401,791,395,738,32,574,465,996,84,529,272,851,80,870,456,714,356,958,265,796,6,853,497,559,67,797,72,976,43,689,467,524,422,611,110,536,337,864,23,617,493,552,388,952,446,629,130,668,21,773,335,697,424,633,494,512,46,901,141,735,238,725,324,517,269,683,292,696,466,912,355,866,48,979,427,883,133,754,316,747,330,872,9,914,411,684,171,605,178,690,106,595,367,757,74,860,303,882,225,811,474,600,91,755,263,891,60,693,475,596,106,557,372,688,153,966,186,524,93,579,56,900,315,803,245,542,17,721,456,792,32,868,271,971,485,917,423,537,347,662,489,971,128,974,130,881,202,580,249,787,425,502,423,693,172,570,314,529,102,515,79,679,232,624,330,594,406,988,394,619,455,751,194,920,457,606,213,696,312,659,447,513,382,637,262,654,138,808,331,541,65,971,446,795,265,801,46,715,492,800,345,723,389,583,250,672,243,610,87,784,271,815,244,576,459,972,79,797,358,915,395,764,471,945,155,650,158,700,231,745,123,743,277,871,439,931,164,775,137,936,35,809,281,505,382,602,202,886,317,985,335,881,426,913,343,858,281,762,89,843,266,743,498,565,295,548,94,821,38,810,370,782,5,926,348,763,92,680,121,706,441,981,423,590,339,609,52,912,8,607,152,840,234,584,143,603,397,945,328,897,200,776,22,673,213,815,189,702,67,698,162,507,440,519,352,828,353,736,415,939,493,661,316,769,234,940,136,558,192,615,89,997,320,584,330,972,268,929,321,975,368,958,229,765,218,775,192,681,490,621,413,746,280,527,3,885,150,697,494,648,166,577]