commit 726c43dd49b4408de500d11ea32b8202f891ceaa parent 1e4f4a018cf65c0b3be3cfe4539f2186e591a6e5 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 2 May 2023 08:33:01 -0500 Completed sign of the product using dart in linear time Diffstat:
| A | sign-of-product-of-array/sign-of-productV2.dart | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/sign-of-product-of-array/sign-of-productV2.dart b/sign-of-product-of-array/sign-of-productV2.dart @@ -0,0 +1,26 @@ +//Find the sign of the product of a list. +//To do this I checked if values were negative. If they are +//then multiple the "total" variable that tracks the sign of the list. +//If the list contains a 0 then it automatically returns 0. +//The time complexity of this code is O(n) where n is the length of the list. +//Time: 237ms Beats: 100% +//Memory: 141.6MB Beats: 100% + +class Solution { + int arraySign(List<int> nums) { + int total = 1; + for(int i = 0 ; i < nums.length ; ++i){ + + if(nums[i] < 0){ + total *= -1; + } + else if(nums[i] == 0){ + return 0; + } + } + if(total == total.abs()){ + return 1; + } + return -1; + } +}