Skip to content

Instantly share code, notes, and snippets.

@bwnyasse
Last active June 2, 2023 13:43
Show Gist options
  • Save bwnyasse/7945621f5778232c0712c86e608e2180 to your computer and use it in GitHub Desktop.
Save bwnyasse/7945621f5778232c0712c86e608e2180 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Design Pattern Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SlidingWindowScreen(),
);
}
}
class SlidingWindowScreen extends StatefulWidget {
const SlidingWindowScreen({Key? key}) : super(key: key);
@override
SlidingWindowScreenState createState() => SlidingWindowScreenState();
}
class SlidingWindowScreenState extends State<SlidingWindowScreen>
with SingleTickerProviderStateMixin {
List<int> numbers = [];
final int k = 3;
late AnimationController _animationController;
late Animation<Offset> _animation;
List<int> maxSumElements = [];
@override
void initState() {
super.initState();
generateNumbers();
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 500),
);
_animation = Tween<Offset>(begin: Offset.zero, end: const Offset(0.0, 1.0))
.animate(_animationController);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
void generateNumbers() {
final random = Random();
numbers = List.generate(10, (_) => random.nextInt(10));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sliding Window - bwnyasse.net'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Numbers:',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 10),
buildNumberList(),
const SizedBox(height: 20),
SlideTransition(
position: _animation,
child: Text(
'Max Sum of $k Consecutive Elements: ${findMaxSum()}',
style:
const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
generateNumbers();
maxSumElements.clear();
});
},
child: const Icon(Icons.play_arrow),
),
);
}
Widget buildNumberList() {
return SizedBox(
height: 40,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: numbers.length,
itemBuilder: (BuildContext context, int index) {
final color =
maxSumElements.contains(index) ? Colors.green : Colors.blue;
return Container(
width: 40,
height: 40,
margin: const EdgeInsets.symmetric(horizontal: 4),
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
numbers[index].toString(),
style: const TextStyle(color: Colors.white),
),
),
);
},
),
);
}
int findMaxSum() {
int maxSum = 0;
int currentSum = 0;
int maxSumIndex = 0;
// Calculate sum of first 'k' elements
for (int i = 0; i < k; i++) {
currentSum += numbers[i];
}
maxSum = currentSum;
// Slide the window by one element at a time
for (int i = k; i < numbers.length; i++) {
currentSum = currentSum - numbers[i - k] + numbers[i];
if (currentSum > maxSum) {
maxSum = currentSum;
maxSumIndex = i - k + 1;
}
}
maxSumElements = List<int>.generate(k, (index) => maxSumIndex + index);
return maxSum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment