File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed
solution/1700-1799/1746.Maximum Subarray Sum After One Operation Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,36 @@ public:
116116};
117117```
118118
119+ ### **Rust**
120+
121+ ```rust
122+ impl Solution {
123+ #[allow(dead_code)]
124+ pub fn max_sum_after_operation(nums: Vec<i32>) -> i32 {
125+ // Here f[i] represents the value of max sub-array that ends with nums[i] with no substitution
126+ let mut f = 0;
127+ // g[i] represents the case with exact one substitution
128+ let mut g = 0;
129+ let mut ret = 1 << 31;
130+
131+ // Begin the actual dp process
132+ for e in &nums {
133+ // f[i] = MAX(f[i - 1], 0) + nums[i]
134+ let new_f = std::cmp::max(f, 0) + *e;
135+ // g[i] = MAX(MAX(f[i - 1], 0) + nums[i] * nums[i], g[i - 1] + nums[i])
136+ let new_g = std::cmp::max(std::cmp::max(f, 0) + *e * *e, g + *e);
137+ // Update f[i] & g[i]
138+ f = new_f;
139+ g = new_g;
140+ // Since we start at 0, update answer after updating f[i] & g[i]
141+ ret = std::cmp::max(ret, g);
142+ }
143+
144+ ret
145+ }
146+ }
147+ ```
148+
119149### ** Go**
120150
121151``` go
Original file line number Diff line number Diff line change @@ -89,6 +89,36 @@ public:
8989};
9090```
9191
92+ ### **Rust**
93+
94+ ```rust
95+ impl Solution {
96+ #[allow(dead_code)]
97+ pub fn max_sum_after_operation(nums: Vec<i32>) -> i32 {
98+ // Here f[i] represents the value of max sub-array that ends with nums[i] with no substitution
99+ let mut f = 0;
100+ // g[i] represents the case with exact one substitution
101+ let mut g = 0;
102+ let mut ret = 1 << 31;
103+
104+ // Begin the actual dp process
105+ for e in &nums {
106+ // f[i] = MAX(f[i - 1], 0) + nums[i]
107+ let new_f = std::cmp::max(f, 0) + *e;
108+ // g[i] = MAX(MAX(f[i - 1], 0) + nums[i] * nums[i], g[i - 1] + nums[i])
109+ let new_g = std::cmp::max(std::cmp::max(f, 0) + *e * *e, g + *e);
110+ // Update f[i] & g[i]
111+ f = new_f;
112+ g = new_g;
113+ // Since we start at 0, update answer after updating f[i] & g[i]
114+ ret = std::cmp::max(ret, g);
115+ }
116+
117+ ret
118+ }
119+ }
120+ ```
121+
92122### ** Go**
93123
94124``` go
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ #[ allow( dead_code) ]
3+ pub fn max_sum_after_operation ( nums : Vec < i32 > ) -> i32 {
4+ // Here f[i] represents the value of max sub-array that ends with nums[i] with no substitution
5+ let mut f = 0 ;
6+ // g[i] represents the case with exact one substitution
7+ let mut g = 0 ;
8+ let mut ret = 1 << 31 ;
9+
10+ // Begin the actual dp process
11+ for e in & nums {
12+ // f[i] = MAX(f[i - 1], 0) + nums[i]
13+ let new_f = std:: cmp:: max ( f, 0 ) + * e;
14+ // g[i] = MAX(MAX(f[i - 1], 0) + nums[i] * nums[i], g[i - 1] + nums[i])
15+ let new_g = std:: cmp:: max ( std:: cmp:: max ( f, 0 ) + * e * * e, g + * e) ;
16+ // Update f[i] & g[i]
17+ f = new_f;
18+ g = new_g;
19+ // Since we start at 0, update answer after updating f[i] & g[i]
20+ ret = std:: cmp:: max ( ret, g) ;
21+ }
22+
23+ ret
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments