Code Signal_练习题_matrixElementsSum
After they became famous, the CodeBots all decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms. Each cell in the matrix contains an integer that represents the price of the room. Some rooms are free(their cost is 0), but that's probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots to live in.
Help the bots calculate the total price of all the rooms that are suitable for them.
Example
- For
matrix = [[0, 1, 1, 2],
[0, 5, 0, 0],
[2, 0, 3, 3]]
the output should bematrixElementsSum(matrix) = 9.
Here's the rooms matrix with unsuitable rooms marked with 'x':
[[x, 1, 1, 2],
[x, 5, x, x],
[x, x, x, x]]
Thus, the answer is 1 + 5 + 1 + 2 = 9.
- For
matrix = [[1, 1, 1, 0],
[0, 5, 0, 1],
[2, 1, 3, 10]]
the output should bematrixElementsSum(matrix) = 9.
Here's the rooms matrix with unsuitable rooms marked with 'x':
[[1, 1, 1, x],
[x, 5, x, x],
[x, 1, x, x]]
Note that the free room in the first row make the full column unsuitable for bots.
Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.
我的解答:
def matrixElementsSum(matrix):
num = 0
j = 0
while j<len(matrix):
num = num + sum(matrix[j])
for i in range(len(matrix[j])):
if matrix[j][i] == 0:
for x in range(j+1,len(matrix)):
matrix[x][i] = 0
j += 1
return num
膜拜大佬:
def matrixElementsSum(m):
r = len(m)
c = len(m[0])
total=0
for j in range(c):
for i in range(r):
if m[i][j]!=0:
total+=m[i][j]
else:
break
return total
Code Signal_练习题_matrixElementsSum的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- 微信Netting-QRLJacking分析利用-扫我二维码获取你的账号权限
首先我们来看一下QRLJacking的实际原理:.攻击者首先进行客户端QR会话,并将登录QR码复制到网络钓鱼网站.“现在,一个精心制作的网络钓鱼页面有一个有效和定期更新的QR码可以被发送给受害者.” ...
- 前端ajax传数据成功发送,但后端接收不到
前几天遇到这样的问题,找了好久,是在ajax contentType属性设置的问题. contentType默认是application/x-www-form-urlencoded 但是 ...
- Apache TraceEnable关闭与测试方法
系统环境:OS: RHEL5.6_x64Apache: httpd-2.2.11 关闭方法:在主配置文件httpd.conf中添加配置:TraceEnable off可以直接配置在ServerRoot ...
- 访问www.baidu.com后会发生什么(一次完整的网络通讯过程)
1.在浏览器中输入www.baidu.com 这意味着浏览器要向百度发送一个网页数据包,要发送数据包,需要知道对方的IP地址,这里我们只知道网址为www.baidu.com,却不知道IP地址,此时应用 ...
- spring cloud ribbon源码解析(二)
在上一篇文章中主要梳理了ribbon的执行过程,这篇主要讲讲ribbon的负载均衡,ribbon的负载均衡是通过ILoadBalancer来实现的,对ILoadBalancer有以下几个类 1.Abs ...
- (原创)确保JAVA线程安全的4种常用方法
在Java中可以有很多方法来保证线程安全,比如使用同步方法.同步块,使用原子类(atomic concurrent classes),实现并发锁,使用volatile关键字,使用不变类和线程安全类. ...
- 下载apache-tomcat-9.0.17-windows-x64及安装以及用途
首先我们先去这个网站下载http://www.apache.org/,进入Tomcat,点击Tomcat9 下载64-bit Windows_zip 当我们下载好了之后解压,把多余的文件删除掉,也可以 ...
- Eclipse无法编译 build无效 没有class文件
问题原因: 我遇到这个问题的原因是: maven 插件引起的,maven clean或maven build后,经常无法自动编译class(虽然project自动编译了,但是只有包文件夹名,而没有cl ...
- Postman—脚本介绍
前言 Postman包含一个基于Node.js的强大的运行时环境,它允许我们为请求和集合添加动态行为.这使的我们可以编写测试用例,构建可包含动态参数的请求,在请求之间传递数据等等. 我们可以在流程中以 ...
- win10下用Linux搭建python&nodejs开发环境
Win10下用自带Linux系统搭建开发环境 Win10下用自带Linux系统搭建开发环境启用Linux老版本(win10 1709之前):新版本(win10 1709之后)卸载linux老版本新版本 ...