生活随笔
收集整理的這篇文章主要介紹了
LeetCode 第 28 场双周赛(505/2144,前23.6%)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄 1. 比賽結(jié)果 2. 題目 1. LeetCode 5420. 商品折扣后的最終價格 easy 2. LeetCode 5422. 子矩形查詢 medium 3. LeetCode 5423. 找兩個和為目標值且不重疊的子數(shù)組 medium 4. LeetCode 5421. 安排郵筒 hard
1. 比賽結(jié)果
兩題選手😂,前兩題很水,暴力解題拼手速,第三題超時😂,第四題不太會,繼續(xù)加油!
全國排名: 505 / 2144,23.6%;全球排名: 1944 / 8571,22.7%
2. 題目
1. LeetCode 5420. 商品折扣后的最終價格 easy
題目鏈接 給你一個數(shù)組 prices ,其中 prices[i] 是商店里第 i 件商品的價格。
商店里正在進行促銷活動,如果你要買第 i 件商品,那么你可以得到與 prices[j] 相等的折扣,其中 j 是滿足 j > i 且 prices[j] <= prices[i] 的 最小 下標 ,如果沒有滿足條件的 j ,你將沒有任何折扣。
請你返回一個數(shù)組,數(shù)組中第 i 個元素是折扣后你購買商品 i 最終需要支付的價格。
示例
1 :
輸入:prices
= [ 8 , 4 , 6 , 2 , 3 ]
輸出:
[ 4 , 2 , 4 , 2 , 3 ]
解釋:
商品
0 的價格為 price
[ 0 ] = 8 ,你將得到 prices
[ 1 ] = 4 的折扣,所以最終價格為
8 - 4 = 4 。
商品
1 的價格為 price
[ 1 ] = 4 ,你將得到 prices
[ 3 ] = 2 的折扣,所以最終價格為
4 - 2 = 2 。
商品
2 的價格為 price
[ 2 ] = 6 ,你將得到 prices
[ 3 ] = 2 的折扣,所以最終價格為
6 - 2 = 4 。
商品
3 和
4 都沒有折扣。示例
2 :
輸入:prices
= [ 1 , 2 , 3 , 4 , 5 ]
輸出:
[ 1 , 2 , 3 , 4 , 5 ]
解釋:在這個例子中,所有商品都沒有折扣。示例
3 :
輸入:prices
= [ 10 , 1 , 1 , 6 ]
輸出:
[ 9 , 0 , 1 , 6 ] 提示:
1 <= prices
. length
<= 500
1 <= prices
[ i
] <= 10 ^ 3
解題:
class Solution {
public : vector
< int > finalPrices ( vector
< int > & prices
) { int i
, j
, n
= prices
. size ( ) ; for ( i
= 0 ; i
< n
- 1 ; i
++ ) { for ( j
= i
+ 1 ; j
< n
; j
++ ) { if ( prices
[ j
] <= prices
[ i
] ) { prices
[ i
] - = prices
[ j
] ; break ; } } } return prices
; }
} ;
4 ms 9.9 MB
class Solution : def finalPrices ( self
, prices
: List
[ int ] ) - > List
[ int ] : n
= len ( prices
) for i
in range ( n
- 1 ) : for j
in range ( i
+ 1 , n
) : if prices
[ j
] <= prices
[ i
] : prices
[ i
] -= prices
[ j
] break ; return prices
44 ms 13.7 MB
數(shù)據(jù)規(guī)模大的話,需要用單調(diào)棧
class Solution {
public : vector
< int > finalPrices ( vector
< int > & prices
) { int i
, n
= prices
. size ( ) ; stack
< int > stk
; vector
< int > ans ( prices
) ; for ( i
= n
- 1 ; i
>= 0 ; -- i
) { while ( ! stk
. empty ( ) && prices
[ i
] < prices
[ stk
. top ( ) ] ) stk
. pop ( ) ; if ( ! stk
. empty ( ) ) ans
[ i
] - = prices
[ stk
. top ( ) ] ; stk
. push ( i
) ; } return ans
; }
} ;
2. LeetCode 5422. 子矩形查詢 medium
題目鏈接 請你實現(xiàn)一個類 SubrectangleQueries ,它的構(gòu)造函數(shù)的參數(shù)是一個 rows x cols 的矩形(這里用整數(shù)矩陣表示),并支持以下兩種操作:
updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) 用 newValue 更新以 (row1,col1) 為左上角且以 (row2,col2) 為右下角的子矩形。 getValue(int row, int col) 返回矩形中坐標 (row,col) 的當前值。
示例
1 :
輸入:
[ "SubrectangleQueries" , "getValue" , "updateSubrectangle" ,
"getValue" , "getValue" , "updateSubrectangle" , "getValue" , "getValue" ]
[ [ [ [ 1 , 2 , 1 ] , [ 4 , 3 , 4 ] , [ 3 , 2 , 1 ] , [ 1 , 1 , 1 ] ] ] , [ 0 , 2 ] , [ 0 , 0 , 3 , 2 , 5 ] , [ 0 , 2 ] , [ 3 , 1 ] , [ 3 , 0 , 3 , 2 , 10 ] , [ 3 , 1 ] , [ 0 , 2 ] ]
輸出:
[ null
, 1 , null
, 5 , 5 , null
, 10 , 5 ]
解釋:
SubrectangleQueries subrectangleQueries
= new SubrectangleQueries ( [ [ 1 , 2 , 1 ] , [ 4 , 3 , 4 ] , [ 3 , 2 , 1 ] , [ 1 , 1 , 1 ] ] ) ;
subrectangleQueries
. getValue ( 0 , 2 ) ;
subrectangleQueries
. updateSubrectangle ( 0 , 0 , 3 , 2 , 5 ) ;
subrectangleQueries
. getValue ( 0 , 2 ) ;
subrectangleQueries
. getValue ( 3 , 1 ) ;
subrectangleQueries
. updateSubrectangle ( 3 , 0 , 3 , 2 , 10 ) ;
subrectangleQueries
. getValue ( 3 , 1 ) ;
subrectangleQueries
. getValue ( 0 , 2 ) ; 示例
2 :
輸入:
[ "SubrectangleQueries" , "getValue" , "updateSubrectangle" ,
"getValue" , "getValue" , "updateSubrectangle" , "getValue" ]
[ [ [ [ 1 , 1 , 1 ] , [ 2 , 2 , 2 ] , [ 3 , 3 , 3 ] ] ] , [ 0 , 0 ] , [ 0 , 0 , 2 , 2 , 100 ] , [ 0 , 0 ] , [ 2 , 2 ] , [ 1 , 1 , 2 , 2 , 20 ] , [ 2 , 2 ] ]
輸出:
[ null
, 1 , null
, 100 , 100 , null
, 20 ]
解釋:
SubrectangleQueries subrectangleQueries
= new SubrectangleQueries ( [ [ 1 , 1 , 1 ] , [ 2 , 2 , 2 ] , [ 3 , 3 , 3 ] ] ) ;
subrectangleQueries
. getValue ( 0 , 0 ) ;
subrectangleQueries
. updateSubrectangle ( 0 , 0 , 2 , 2 , 100 ) ;
subrectangleQueries
. getValue ( 0 , 0 ) ;
subrectangleQueries
. getValue ( 2 , 2 ) ;
subrectangleQueries
. updateSubrectangle ( 1 , 1 , 2 , 2 , 20 ) ;
subrectangleQueries
. getValue ( 2 , 2 ) ; 提示:
最多有
500 次updateSubrectangle 和 getValue 操作。
1 <= rows
, cols
<= 100
rows
== rectangle
. length
cols
== rectangle
[ i
] . length
0 <= row1
<= row2
< rows
0 <= col1
<= col2
< cols
1 <= newValue
, rectangle
[ i
] [ j
] <= 10 ^ 9
0 <= row
< rows
0 <= col
< cols
解題:
class SubrectangleQueries { vector
< vector
< int >> v
;
public : SubrectangleQueries ( vector
< vector
< int >> & rectangle
) { v
= rectangle
; } void updateSubrectangle ( int row1
, int col1
, int row2
, int col2
, int newValue
) { int i
, j
; for ( i
= row1
; i
<= row2
; ++ i
) for ( j
= col1
; j
<= col2
; ++ j
) v
[ i
] [ j
] = newValue
; } int getValue ( int row
, int col
) { return v
[ row
] [ col
] ; }
} ;
84 ms 18.6 MB
class SubrectangleQueries { vector
< vector
< int >> record
; vector
< vector
< int >> v
;
public : SubrectangleQueries ( vector
< vector
< int >> & rectangle
) { v
= rectangle
; } void updateSubrectangle ( int row1
, int col1
, int row2
, int col2
, int newValue
) { record
. push_back ( { row1
, col1
, row2
, col2
, newValue
} ) ; } int getValue ( int row
, int col
) { for ( int i
= record
. size ( ) - 1 ; i
>= 0 ; -- i
) { if ( row
>= record
[ i
] [ 0 ] && row
<= record
[ i
] [ 2 ] && col
>= record
[ i
] [ 1 ] && col
<= record
[ i
] [ 3 ] ) return record
[ i
] [ 4 ] ; } return v
[ row
] [ col
] ; }
} ;
84 ms 19.2 MB
class SubrectangleQueries : def __init__ ( self
, rectangle
: List
[ List
[ int ] ] ) : import numpy
as npself
. rec
= np
. array
( rectangle
) def updateSubrectangle ( self
, row1
: int , col1
: int , row2
: int , col2
: int , newValue
: int ) - > None : self
. rec
[ row1
: row2
+ 1 , col1
: col2
+ 1 ] = newValue
def getValue ( self
, row
: int , col
: int ) - > int : return int ( self
. rec
[ row
] [ col
] )
140 ms 30.2 MB
3. LeetCode 5423. 找兩個和為目標值且不重疊的子數(shù)組 medium
題目鏈接 給你一個整數(shù)數(shù)組 arr 和一個整數(shù)值 target 。
請你在 arr 中找 兩個互不重疊 的子數(shù)組 且它們的和都等于 target 。 可能會有多種方案,請你返回滿足要求的兩個子數(shù)組長度和 的 最小值 。
請返回滿足要求的最小長度和,如果無法找到這樣的兩個子數(shù)組,請返回 -1 。
示例
1 :
輸入:arr
= [ 3 , 2 , 2 , 4 , 3 ] , target
= 3
輸出:
2
解釋:只有兩個子數(shù)組和為
3 (
[ 3 ] 和
[ 3 ] )。它們的長度和為
2 。示例
2 :
輸入:arr
= [ 7 , 3 , 4 , 7 ] , target
= 7
輸出:
2
解釋:盡管我們有
3 個互不重疊的子數(shù)組和為
7 (
[ 7 ] , [ 3 , 4 ] 和
[ 7 ] ),
但我們會選擇第一個和第三個子數(shù)組,因為它們的長度和
2 是最小值。示例
3 :
輸入:arr
= [ 4 , 3 , 2 , 6 , 2 , 3 , 4 ] , target
= 6
輸出:
- 1
解釋:我們只有一個和為
6 的子數(shù)組。示例
4 :
輸入:arr
= [ 5 , 5 , 4 , 4 , 5 ] , target
= 3
輸出:
- 1
解釋:我們無法找到和為
3 的子數(shù)組。示例
5 :
輸入:arr
= [ 3 , 1 , 1 , 1 , 5 , 1 , 2 , 1 ] , target
= 3
輸出:
3
解釋:注意子數(shù)組
[ 1 , 2 ] 和
[ 2 , 1 ] 不能成為一個方案因為它們重疊了。提示:
1 <= arr
. length
<= 10 ^ 5
1 <= arr
[ i
] <= 1000
1 <= target
<= 10 ^ 8
解題:
先通過滑動窗口求出所有的區(qū)間,注意 使用multiset時,才能保存長度一樣的 然后在區(qū)間里雙重循環(huán),內(nèi)層找到一個解的時候就 break,然后外層循環(huán) 注意剪枝
struct cmp
{ bool operator ( ) ( const pair
< int , int > & a
, const pair
< int , int > & b
) const { return a
. second
- a
. first
< b
. second
- b
. first
; }
} ;
class Solution {
public : int minSumOfLengths ( vector
< int > & arr
, int target
) { int i
= 0 , j
= 0 , n
= arr
. size ( ) , sum
= 0 ; int minlen
= INT_MAX
; multiset
< pair
< int , int > , cmp
> v
; for ( ; j
< n
; ++ j
) { sum
+ = arr
[ j
] ; if ( sum
== target
) v
. insert ( { i
, j
} ) ; while ( sum
> target
) { sum
- = arr
[ i
++ ] ; if ( sum
== target
) v
. insert ( { i
, j
} ) ; } } for ( auto it1
= v
. begin ( ) ; it1
!= v
. end ( ) ; ++ it1
) { if ( 2 * ( it1
- > second
- it1
- > first
+ 1 ) >= minlen
) break ; auto it2
= it1
; for ( it2
++ ; it2
!= v
. end ( ) ; ++ it2
) { if ( it1
- > second
< it2
- > first
|| it1
- > first
> it2
- > second
) { minlen
= min ( minlen
, it1
- > second
- it1
- > first
+ it2
- > second
- it2
- > first
+ 2 ) ; break ; } } } return minlen
== INT_MAX
? - 1 : minlen
; }
} ;
516 ms 87.8 MB
利用前綴和,分別記錄每個位置左側(cè)的最短長度,右側(cè)的最短長度 再遍歷一次求解最短的 l+r
class Solution {
public : int minSumOfLengths ( vector
< int > & arr
, int target
) { int i
, n
= arr
. size ( ) , sum
= 0 , minlen
= INT_MAX
; unordered_map
< int , int > m
; m
[ 0 ] = - 1 ; vector
< int > left ( n
, 0 ) ; vector
< int > right ( n
, 0 ) ; for ( i
= 0 ; i
< n
; ++ i
) { sum
+ = arr
[ i
] ; m
[ sum
] = i
; if ( m
. count ( sum
- target
) ) minlen
= min ( minlen
, i
- m
[ sum
- target
] ) ; left
[ i
] = minlen
; } unordered_map
< int , int > m1
; m1
[ 0 ] = n
; sum
= 0 ; minlen
= INT_MAX
; for ( i
= n
- 1 ; i
>= 0 ; -- i
) { sum
+ = arr
[ i
] ; m1
[ sum
] = i
; if ( m1
. count ( sum
- target
) ) minlen
= min ( minlen
, m1
[ sum
- target
] - i
) ; right
[ i
] = minlen
; } minlen
= INT_MAX
; for ( i
= 0 ; i
< n
- 1 ; ++ i
) if ( left
[ i
] != INT_MAX
&& right
[ i
+ 1 ] != INT_MAX
) minlen
= min ( minlen
, left
[ i
] + right
[ i
+ 1 ] ) ; return minlen
== INT_MAX
? - 1 : minlen
; }
} ;
1172 ms 164.8 MB
4. LeetCode 5421. 安排郵筒 hard
題目鏈接 給你一個房屋數(shù)組houses 和一個整數(shù) k ,其中 houses[i] 是第 i 棟房子在一條街上的位置,現(xiàn)需要在這條街上安排 k 個郵筒。
請你返回每棟房子與離它最近的郵筒之間的距離的 最小 總和。
答案保證在 32 位有符號整數(shù)范圍以內(nèi)。
示例 1:
輸入:houses
= [ 1 , 4 , 8 , 10 , 20 ] , k
= 3
輸出:
5
解釋:將郵筒分別安放在位置
3 ,
9 和
20 處。
每個房子到最近郵筒的距離和為
| 3 - 1 | + | 4 - 3 | + | 9 - 8 | + | 10 - 9 | + | 20 - 20 | = 5 。
示例 2:
輸入:houses
= [ 2 , 3 , 5 , 12 , 18 ] , k
= 2
輸出:
9
解釋:將郵筒分別安放在位置
3 和
14 處。
每個房子到最近郵筒距離和為
| 2 - 3 | + | 3 - 3 | + | 5 - 3 | + | 12 - 14 | + | 18 - 14 | = 9 。示例
3 :
輸入:houses
= [ 7 , 4 , 6 , 1 ] , k
= 1
輸出:
8 示例
4 :
輸入:houses
= [ 3 , 6 , 14 , 10 ] , k
= 4
輸出:
0 提示:
n
== houses
. length
1 <= n
<= 100
1 <= houses
[ i
] <= 10 ^ 4
1 <= k
<= n
數(shù)組 houses 中的整數(shù)互不相同。
解題:
待補
總結(jié)
以上是生活随笔 為你收集整理的LeetCode 第 28 场双周赛(505/2144,前23.6%) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。