Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 039bd09

Browse files
feat: add golang solution to lc problem: No.2034
No.2034.Stock Price Fluctuation
1 parent b005a39 commit 039bd09

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

‎solution/2000-2099/2034.Stock Price Fluctuation/README.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,68 @@ public:
211211
*/
212212
```
213213
214+
### **Go**
215+
216+
```go
217+
type StockPrice struct {
218+
lastTs int
219+
mp map[int]int
220+
counter *redblacktree.Tree
221+
}
222+
223+
func Constructor() StockPrice {
224+
return StockPrice{
225+
mp: make(map[int]int),
226+
counter: redblacktree.NewWithIntComparator(),
227+
}
228+
}
229+
230+
func (this *StockPrice) Update(timestamp int, price int) {
231+
if timestamp > this.lastTs {
232+
this.lastTs = timestamp
233+
}
234+
if old, ok := this.mp[timestamp]; ok {
235+
cnt := getInt(this.counter, old)
236+
if cnt == 1 {
237+
this.counter.Remove(old)
238+
} else {
239+
this.counter.Put(old, cnt-1)
240+
}
241+
}
242+
this.mp[timestamp] = price
243+
this.counter.Put(price, getInt(this.counter, price)+1)
244+
}
245+
246+
func (this *StockPrice) Current() int {
247+
return this.mp[this.lastTs]
248+
}
249+
250+
func (this *StockPrice) Maximum() int {
251+
return this.counter.Right().Key.(int)
252+
}
253+
254+
func (this *StockPrice) Minimum() int {
255+
return this.counter.Left().Key.(int)
256+
}
257+
258+
func getInt(rbt *redblacktree.Tree, key int) int {
259+
val, found := rbt.Get(key)
260+
if !found {
261+
return 0
262+
}
263+
return val.(int)
264+
}
265+
266+
/**
267+
* Your StockPrice object will be instantiated and called as such:
268+
* obj := Constructor();
269+
* obj.Update(timestamp,price);
270+
* param_2 := obj.Current();
271+
* param_3 := obj.Maximum();
272+
* param_4 := obj.Minimum();
273+
*/
274+
```
275+
214276
### **...**
215277

216278
```

‎solution/2000-2099/2034.Stock Price Fluctuation/README_EN.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,68 @@ public:
202202
*/
203203
```
204204
205+
### **Go**
206+
207+
```go
208+
type StockPrice struct {
209+
lastTs int
210+
mp map[int]int
211+
counter *redblacktree.Tree
212+
}
213+
214+
func Constructor() StockPrice {
215+
return StockPrice{
216+
mp: make(map[int]int),
217+
counter: redblacktree.NewWithIntComparator(),
218+
}
219+
}
220+
221+
func (this *StockPrice) Update(timestamp int, price int) {
222+
if timestamp > this.lastTs {
223+
this.lastTs = timestamp
224+
}
225+
if old, ok := this.mp[timestamp]; ok {
226+
cnt := getInt(this.counter, old)
227+
if cnt == 1 {
228+
this.counter.Remove(old)
229+
} else {
230+
this.counter.Put(old, cnt-1)
231+
}
232+
}
233+
this.mp[timestamp] = price
234+
this.counter.Put(price, getInt(this.counter, price)+1)
235+
}
236+
237+
func (this *StockPrice) Current() int {
238+
return this.mp[this.lastTs]
239+
}
240+
241+
func (this *StockPrice) Maximum() int {
242+
return this.counter.Right().Key.(int)
243+
}
244+
245+
func (this *StockPrice) Minimum() int {
246+
return this.counter.Left().Key.(int)
247+
}
248+
249+
func getInt(rbt *redblacktree.Tree, key int) int {
250+
val, found := rbt.Get(key)
251+
if !found {
252+
return 0
253+
}
254+
return val.(int)
255+
}
256+
257+
/**
258+
* Your StockPrice object will be instantiated and called as such:
259+
* obj := Constructor();
260+
* obj.Update(timestamp,price);
261+
* param_2 := obj.Current();
262+
* param_3 := obj.Maximum();
263+
* param_4 := obj.Minimum();
264+
*/
265+
```
266+
205267
### **...**
206268

207269
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
type StockPrice struct {
2+
lastTs int
3+
mp map[int]int
4+
counter *redblacktree.Tree
5+
}
6+
7+
func Constructor() StockPrice {
8+
return StockPrice{
9+
mp: make(map[int]int),
10+
counter: redblacktree.NewWithIntComparator(),
11+
}
12+
}
13+
14+
func (this *StockPrice) Update(timestamp int, price int) {
15+
if timestamp > this.lastTs {
16+
this.lastTs = timestamp
17+
}
18+
if old, ok := this.mp[timestamp]; ok {
19+
cnt := getInt(this.counter, old)
20+
if cnt == 1 {
21+
this.counter.Remove(old)
22+
} else {
23+
this.counter.Put(old, cnt-1)
24+
}
25+
}
26+
this.mp[timestamp] = price
27+
this.counter.Put(price, getInt(this.counter, price)+1)
28+
}
29+
30+
func (this *StockPrice) Current() int {
31+
return this.mp[this.lastTs]
32+
}
33+
34+
func (this *StockPrice) Maximum() int {
35+
return this.counter.Right().Key.(int)
36+
}
37+
38+
func (this *StockPrice) Minimum() int {
39+
return this.counter.Left().Key.(int)
40+
}
41+
42+
func getInt(rbt *redblacktree.Tree, key int) int {
43+
val, found := rbt.Get(key)
44+
if !found {
45+
return 0
46+
}
47+
return val.(int)
48+
}
49+
50+
/**
51+
* Your StockPrice object will be instantiated and called as such:
52+
* obj := Constructor();
53+
* obj.Update(timestamp,price);
54+
* param_2 := obj.Current();
55+
* param_3 := obj.Maximum();
56+
* param_4 := obj.Minimum();
57+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /